by btown · 1w ago · 0 comments
So the “fix” was to let the author mark individual contracts as always enforced at runtime, with pre<enforced>. That, in turn, means a library’s key safety checks (bounds, null, etc.) can’t be silently dropped in a “loose” build configuration, which pushes things down a safer path.
Clunky IMO, but rather reasonable as a response to Rust!
It brings things closer to a world where: if you use these “safe” libraries, your program will stop rather than run unsafely, no matter how it was built.
Perhaps, a good way to describe the difference here is to clarify that contracts do not innately make C++ as a language more memory safe, but have plenty to do with the memory safety of C++ applications.
If I call safe_library::some_low_level_op(some_offset), the library can guarantee a bad offset terminates the program before any out-of-bounds access, and no build flag on my end can drop that check. That’s the same “safe API over unsafe internals, enforced at runtime” pattern Rust’s own standard library relies on for bounds.
What C++ still can’t do is stop me from bypassing that API with a raw pointer, or help at all with lifetimes. But it's useful in practice, not just in theory.
Now if the contract was instead, it must be proved at compile time I'd be for that. I don't know if theorem checkers are good enough to pull that off.
If you know it will never happen, what’s the point of the feature?
If statements can do guaranteed enforcement of bounds och null checks.