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 (&T and &mut T) are second-class: they can only be passed down as function parameters. They can never be stored in a data record, 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:

Checked
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)
}
buildFrame()

What Clever Rejects

Because &mut is exclusive, passing the same variable to two conflicting borrows in one call is a compile error:

Rejected, as it should be
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
}
The compiler saysline 7:3: `a` is passed as `&mut` to `addInto` and also borrowed in another argument of the same call (in function `badAlias`)

And returning a borrow is rejected syntactically and semantically — borrows are parameter modes, not first-class types:

Rejected, as it should be
fun badReturn(a: &Array<U64, 4>): &U64 = a[0]
The compiler saysline 1:35: borrow types (`&T`, `&mut T`) are only allowed on function parameters

Moving a heap value (Buf, Bytes, String, Map) transfers ownership; using it afterward is a compile error:

Rejected, as it should be
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
}
The compiler saysline 6:7: `b` was moved at 5:11; write `b.share()` before the move to keep a second reference (in function `useAfterMove`)

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:

Checked
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
}
cowDemo()

Why this matters in practice:

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:

Proven
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 simp

Idiomatic 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