Second-Class Borrows
Rust's borrow checker is famously powerful, and its lifetime annotations ('a, 'b: 'a, HRTBs) are famously where newcomers — and AI code generators — spend the most friction.
Clever keeps Rust's core safety guarantee (no aliasing mutable references, no use-after-free, zero GC) while eliminating lifetimes altogether through one structural rule:
Borrows (
&Tand&mut T) are second-class: they can only be passed down as function parameters. They can never be stored in adatarecord,enum,Buf, or returned from a function.
How Second-Class Borrows Work
Because a borrow never outlives the call it was passed into, its lifetime is always strictly the duration of that call frame:
Caller frame owns `buf: Buf<U8>`
│
├── pass `&buf` or `&mut buf` DOWN to callee(...)
│ │
│ └── callee may re-borrow further DOWN to helper(...)
│ (cannot store `&buf` in a struct, cannot return `&buf`)
│
└── call returns -> caller immediately regains full ownership of `buf`At call sites, borrows are implicit — you pass a place (var for &mut T; any place, constant, or temporary for &T), and Clever checks exclusivity statically:
fun pushHeader(out: &mut Buf<U8>, tag: U8, len: U16) {
out.push(tag)
out.push((len >> 8).truncU8())
out.push(len.truncU8())
}
fun checksum(bytes: &Bytes): U64 {
var acc = 0u64
for (i in 0u64 until bytes.len) {
acc = acc *% 31u64 +% bytes[i].toU64()
}
acc
}
export fun buildFrame(payload: &Bytes): U64 {
var frame = Bytes()
pushHeader(frame, 0x01u8, payload.len.truncU16())
frame.append(payload)
checksum(frame)
}What Clever Rejects
Because &mut is exclusive, passing the same variable to two conflicting borrows in one call is a compile error:
fun addInto(dst: &mut U64, src: &U64) {
dst = dst +% src
}
export fun badAlias(x: U64): U64 {
var a = x
addInto(a, a) // ERROR: `a` borrowed both mutably and immutably in the same call
a
}And returning a borrow is rejected syntactically and semantically — borrows are parameter modes, not first-class types:
fun badReturn(a: &Array<U64, 4>): &U64 = a[0]Moving a heap value (Buf, Bytes, String, Map) transfers ownership; using it afterward is a compile error:
fun consume(b: Bytes): U64 = b.len
export fun useAfterMove(): U64 {
val b = b"hello"
val n = consume(b)
n + b.len // ERROR: `b` was already moved
}Copy-on-Write (rc == 1) and Explicit .share()
Heap values (Buf<T>, Bytes, String, Map<K, V>, Set<K>, recursive heap enums) are affine by default: assigning or passing them by value moves them with zero atomic or reference-count overhead.
When you actually want a second logical copy, call .share() explicitly:
export fun cowDemo(): U64 {
var a = Bytes()
a.push(1u8)
a.push(2u8)
val b = a.share() // O(1): increments refcount (rc = 2), no bytes copied
// Mutating `a` sees rc > 1, so it allocates a fresh copy for `a` while `b` stays [1, 2].
a.push(3u8)
// Now `a` has rc == 1 again: further pushes mutate `a` in place!
a.push(4u8)
a.len * 10u64 + b.len // 4 * 10 + 2 = 42
}Why this matters in practice:
- Unshared mutation (
rc == 1): mutates in place at C speed — zero extra allocation. - Snapshots / rollback:
.share()isO(1)and only copies if mutated later. - No accidental hidden clones: because moves are the default and
.share()is explicit, expensive sharing is always visible in code review.
Why Second-Class Borrows Make Verification Easy
In compiled C, &mut T compiles to a non-aliasing (restrict) pointer updated in place — zero copies.
In Clever's logic and Lean 4 model, every &mut parameter elaborates cleanly to pure state passing:
Clever source: fun bump(x: &mut U64, k: U64): U64
Compiled C: uint64_t bump(uint64_t *restrict x, uint64_t k) // in-place pointer
Proof model: bump : U64 -> U64 -> (U64, U64) // pure (result, new_x)Because borrows cannot alias and cannot escape, there is no heap-graph aliasing problem for the prover. You can write imperative, in-place &mut code and prove laws about it directly in Clever's kernel:
fun bump(x: &mut U64, k: U64): U64 {
x = x +% k
k
}
fun bumpTwice(a: U64): U64 {
var y = a
val r1 = bump(y, 3u64)
val r2 = bump(y, 5u64)
y +% r1 +% r2
}
law bumpTwice_spec(a: U64): bumpTwice(a) == a +% 16u64
proof by simpIdiomatic Patterns Without Lifetimes
When porting code from Rust that returned a &'a T slice or reference, Clever uses three simple patterns:
| Rust pattern | Idiomatic Clever pattern |
|---|---|
Returning &'a [u8] slice into an input buffer |
Return a (offset: U64, len: U64) span |
Returning &'a mut Entry from a container |
Pass a key/index or write a method taking &mut this that updates in place |
| Borrowing disjoint elements of a buffer in parallel | par for (i, chunk in buf.chunksExact(N)) — disjoint chunks verified statically |
| Borrowing a struct field to mutate it | Pass var buffers (Buf<U64>) or call &mut this methods on the owning type |