Ante: New Way to Blend Borrow Checking and Reference Counting

62 points by veqq


T6

We used to think that it was impossible to have "shared mutable borrowing", where we could have a borrowed reference to something, even though others can mutate it through their own references. Heck, Rust is basically built on that belief.

Eliminating shared mutable state is not some unfortunate sacrifice Rust had to make to achieve its goals – its a goal of Rust in its own right, as having shared mutable state inhibits local reasoning about code.

"References are like jumps" by withoutboats covers this very well.

If you have in a language the ability to alias two variables so that they refer to the same location in memory, and also the ability to assign values to variables as execution progresses, your ability to locally reason about the behavior of a component of your system becomes badly inhibited. Depriving the user of the ability to mutate aliased state by accident is critical to enabling the user to easily create correctly functioning systems.

Though Rust’s rules around lifetimes are usually framed in the public consciousness as “a way to avoid garbage collection,” the reality is that they are a much deeper and more significant construct than that. They are a way to make tractable programming in a language which allows both mutable state and aliased state, by guaranteeing that state is not aliased while it is being mutated. This is an incredibly powerful tool for understanding the behavior of the system because you can analyze the behavior of your system locally: you never need to worry about “spooky action at a distance.”

Unfortunately, most people seem to have taken the wrong lesson from Rust. They see all of this business with lifetimes and ownership as a dirty mess that Rust has had to adopt because it wanted to avoid garbage collection. But this is completely backwards! Rust adopted rules around shared mutable state and this enabled it to avoid garbage collection. These rules are a good idea regardless.

kornel

This is quite nice!

If I understand correctly, the shared-to-mutable magic comes from it being for types that aren't shared across threads, and uniqueness of Rc is done by acting as if all objects of the same type were borrowed for the same lifetime?

Even if explicit vs seamless syntax is a matter of taste, it shows that the compiler knowing more about a Cell can be more flexible about taking mutable references to it.

And it avoids Rust's misleading terminology of using mut to mean exclusive/unique.