Rust "unsafe" is not code the compiler doesn't understand
Rust "unsafe" is not code the compiler doesn't understand
Posted Nov 17, 2025 12:21 UTC (Mon) by farnz (subscriber, #17727)In reply to: Memory-safe C code by hunger
Parent article: Rust in Android: move fast and fix things (Google Security Blog)
Thinking about it as "code the compiler doesn't understand" leads you down the wrong path; it's not that the compiler doesn't understand the code (it does, certainly well enough to generate machine code from it), it's that the compiler cannot prove that the code complies with all language invariants (not least because some of those invariants might be affected by code written in another language), and thus that the programmer is on the hook for ensuring that the language rules are obeyed. It may, of course, be impossible for the programmer to ensure that the rules are obeyed, in which case you have a buggy program.
One of the core reasons this is a problem is that we know that non-trivial properties of programs[1] are undecidable in general. This implies that we can only ever sort programs into three categories about a non-trivial line: "property holds", "property does not hold", "property might or might not hold".
This can be made useful by having as few interesting programs as possible fall into the "property might or might not hold" category, and then approximating the true answer by asserting that the "property might or might not hold" category is equivalent to "property holds" or "property does not hold" for all programs we care about. There's just one issue with this: sometimes, the approximation we choose is "wrong" from a human perspective, and we either reject a program that "should obviously" work, or we "miscompile" a program that "should obviously" always generate a compile error.
In turn, that leads to a dilemma; if you go down the route of miscompiling programs that should obviously always generate a compile error, programmers who didn't intend their program to have a bug will accidentally write buggy code that isn't caught by review, while if you choose to reject programs that should obviously work, programmers will get frustrated that they can't find a way to express something that clearly make sense.
unsafe gives Rust a way out of the dilemma; without unsafe, Rust promises that the approximation of any property it checks will result in Rust rejecting the program erroneously, at worst, while with unsafe, the compiler may "miscompile" programs. It's then on the programmer to decide which behaviour they want - do you want the more expressive "Unsafe Rust" dialect, with the increased risk of bugs, or do you want the lower bug risk of the "Safe Rust" dialect, but with the inability to express some things that are perfectly reasonable?
[1] As an example of a non-trivial property, "does this program read from free memory, or only allocated memory" is non-trivial.
The LWN site is currently under high scraper load, so comment display has been suppressed for anonymous users. If you are a human, you may read the comments by clicking the button below:
Note: you can avoid this step in the future by logging into your LWN account.
