Valen's borrow checker walks functions backwards to catch use-after-free
A new way to think about borrow checking (in reverse)
Valen, an experimental language that makes shared-xor-mutable optional and allows memory-safe calls into Rust, checks memory safety in reverse. After parsing, typing, and static grouping, its fourth phase scans each function from the last statement upward, tracking which references will be used later and flagging any mutation that invalidates them. The author argues the backwards walk helps with last-use analysis, loops, and speed, and asks readers to poke holes in the approach.
This is weird because, as far as I know, it's the only borrow checker whose safety check goes backwards.
- Animats
Interesting.
In Hoare-type proof of correctness work, it's normal to work backwards from the proof goal (such as an assert statement) back upwards through the program, adding info about code that affects the variables in the assertion. This approach to borrow checking follows that model. You want some state at some point, and you work backwards to see if the preceding code gets you there. Makes a lot of sense for lifetimes.
The main problem with doing this inside a compiler is performance. You backtrack for each thing you need to prove, going through the same code more than once. I used to think that compilers should use more verification technology, but people start screaming about compile times.
- verdagon
Figuring out how to make this was a pretty wild ride. I had zero clue how to approach this, so I went into the mountains with pencil and paper until I figured something out.
I started with a sort of "compile-time generational references" approach, where every reference would also remember when it was made, at compile-time. Then that evolved into a couple more things until I finally figured out this reverse approach. We'll see if it sticks.
Hope you all enjoy!
- gnarlouse
I have no help to offer, but as a bystander who loves Rust I am very hopeful good things come of this for you @verdagon!