Skip to content

← All updates

session

Update 13: The borrow checker

Region inference already prevented use-after-free. This update adds the other half of memory soundness: aliasing discipline, enforced at compile time, deliberately without lifetime parameters, variance, or Send/Sync traits.

Three rules, checked in a dedicated pass after typechecking:

  1. One &mut borrow at a time.
  2. Any number of & borrows, but not while a &mut exists.
  3. No mutation of a value while it is borrowed.

What makes it pleasant rather than painful:

  • Field-wise borrows: &s.x and &mut s.y coexist; borrowing one field does not lock the whole struct.
  • Two-phase borrows: call arguments reserve first and activate later, so the common v.push(v.len()) pattern just works.
  • Non-lexical lifetimes (NLL): a borrow ends at its last use, not at the end of the scope, so val r = &v; print(*r); v = 20; compiles.
  • No annotations: there are no 'a parameters anywhere in the language. Regions stay inferred.
  • Controlled by --borrowck=off|warn|on.

Also in this update: val immutability is now a hard type error on reassignment and on struct-field mutation, and @send/@sync checks landed for thread-safety annotations.