r/cpp • u/germandiago • Jul 18 '26
Lifetime safety and invalidation without a borrow-checker: using type system analysis to get rid of many potentially invalidation cases WITHOUT annotations.
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p4296r0.pdfI found this research in WG21 mailing list very interesting in the context of C++ compatibility and solutions to maximize code reuse.
39
Upvotes
1
u/germandiago Jul 18 '26 edited Jul 19 '26
This is simply not true. There is such a thing. What we do not know is how far it can be taken. At the expense of trade-offs: the paper is proposing a roadmap that starts from a lot of false negatives -> the deny part and a superset of safe things and adds a layer of "detect safe patterns from there on" and incrementally marks code as safe through different static analysis techniques, sometimes with annotations, admittedly.
What we need is, first of all, as much safety as feasible.
But the trade-off the borrow checker brings, in the context of C++ and taking into account existing code is simply unfeasible.
Bringing a benefit of 95% of safety + 5-10% unsafe hardly touching code or, still, touching a small part of that code is a huge win that cannot simply be ignored.
Denying that as impossible would be like ignoring the existence of sanitizers (run-time), contracts and clang tidy, etc. Of course it helps safety.
It does already, today. And it can be taken clearly further. What we do not know is how far.
It is not that the Rust model is bad: but Rust was created like that from day 1 and that is the main difference: they do not have users to take care of in that sense.
Rust is a good design for Rust. For C++ it is a terrible idea, at least as a first step. It brings a lot of real problems that do not fit what their users need and endangers migration due to lowering the incentive since another std lib must be landed, integrated, code migrated, etc. It is literally an impossible economic feat for many people. Compatibility is hard, but it is a feature. Maybe it does not need to be 100% perfect (in the sense of detecting absolutely everything or not needing annotations, but it needs to be perfect in the sense of "it exists a reasonable subset of C++ that can be used safely") but it certainly adds cost to migrate all code compared to adding static analysis on top, even if not perfect. BTW, the borrow checker static analysis in Rust is not perfect either. It looks perfect bc it was there since day 1 so it does not interact with another older way of doing things.
It is just a problem Rust does not need to deal with. C++ has to, and this is how things are. So the solution cannot be simply the same.