Perfectly safe Rust code that wouldn't compile until today is now finally compiling! The wait is over.
Polonius: The Next Evolution of Rust's Borrow Checker
Rust's borrow checker keeps our code safe from memory bugs. We all love it, but sometimes the current version, Non-Lexical Lifetimes (NLL), is just a bit too strict. It can throw errors on code that is completely fine, especially when you use conditional branches or work with common data structures.
That is where Polonius comes in. It is the next generation of the borrow checker, and it is now enabled by default on Rust Nightly.
I wanted to see this in action, so I put it to the test with some actual Rust code. I was getting my morning espresso when I ran a very minimalistic reborrow function with an if true statement. I ran the exact same safe code with the two different borrow checkers. With the stable compiler, it threw the classic "cannot borrow as mutable more than once at a time" error. Then I ran it on Nightly with Polonius, and it compiled perfectly. I was genuinely shocked. It honestly looked like witchcraft to see the exact same code fail on one side and pass on the other.
I then moved to a bigger, more realistic example using a HashMap and mutable references with Some and None branches. NLL hates this pattern and complains about multiple mutable borrows. Polonius easily follows the real control flow paths. It understands that the initial reference is no longer alive in the None branch, allowing the code to compile without weird workarounds. I even turned Polonius off while still using Nightly just to prove it was the new borrow checker making the difference.
Of course, Polonius Alpha is still in alpha. It does more precise analysis, which means the compiler has to do a little more work. I took a look at the Rust team's benchmarks across the top 10,000 downloaded crates on crates.io. There are some compile-time performance regressions, but they are typically minimal.
Touching a core part of the language like the borrow checker is a brave move! It shows the Rust project team is going in the direction of correctness and giving more power to developers, rather than just focusing on pure compilation speed. They want this to be the best version of Rust possible.
If you want to read more about the technical details behind this update, check out the official Rust blog post.

Top comments (0)