The implementation of the borrow checker


I want to implement a borrow checker myself, so I tried to find some documentation about it. The resources is surprising few. The main introduction point of the implementation is here, but as someone on the irc said, it is “ancient” (1 year seems ancient in the sense of rust …) Is there any plan to release a new version of the doc?


What specifically I don’t understand is the difference of checking mutability and aliasability. Isn’t aliasability equivalent to immutability?


Not quite. You might have interior mutability.


i.e. Cell types?


Atomics are another good example


Seems like the document doesn’t mention them at all …


The difference between aliasability and mutability is about closure captures, not about interior mutability.

Borrow checker knows nothing about interior mutability, IIRC, that’s why interior mutability primitives are implemented with raw pointers.

This comment describes why immutable but not aliasable data is used: As the comment explains, this is not strictly necessary, the language could require mut annotations from users instead, but author of the current scheme (something tells me it was @nikomatsakis) decided that this would be too cruel.


Is the aliasability check not needed without closures? I want to know the minimal design first, and then generalize the scheme further with special kinds of rules for ergonomics.


@AndyShiue I’ve made a quick experiment and removed UniqueImmBorrow from the compiler (replaced with MutBorrow). Here’s what effect it had on the standard library: The code that is penalized most is written in functional style and used in &mut self methods. However it doesn’t affect correctness, only some extra mut annotations are required. So if you are writing a minimal borrow checker, then UniqueImmBorrow can certainly be omitted.


Local variables are never aliasable as they are accessible only within the stack frame.

I still don’t understand aliasability checking. Why does it say local variables aren’t aliasable?