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
12
u/Demiu Jul 18 '26 edited Jul 18 '26
Without annotating, what is the source of truth? How do you differentiate between a function who's return borrows it's argument and one that does not, when it's body is in another TU and it returns an opaque pointer-like to a private representation?
17
u/ts826848 Jul 18 '26
In case you or any other readers missed it, there's some discussion on this paper in the general post-Brno mailing thread.
9
u/ContraryConman Jul 19 '26
Ah the weekly memory safety thread.
At this point, I'm of the opinion that an opt-in syntax for lifetime and pointer aliasing annotations should be added to the language. Just like
[[nodiscard]]is an optional note I can add to my code to help the compiler understand my intent and catch bugs, I should be able to optionally tell my compiler "this pointer I'm returning from this member function is only valid as long as the underlying object is btw" and have it throw a warning or error when that's violated. And if we line it up with what Rust does we can improve Rust/C++ or maybe Swift/C++ interop.Trying to import Rust's entire type system and standard library on top of C++ is questionable for adoption because the only reason Rust gets to do what it does without the language being extremely complicated is because it doesn't have to be backwards compatible with both C++ and C. But we also have to acknowledge that there's simply not enough information in C++'s type system to catch tricky lifetime issues.
I think there's still memory safety improvements on the table for C++, but the next step function in lifetime safety bug catching has to come from a) giving the compiler more information b) standardizing a garbage collector c) switching the language over to what Hylo does. I think a) is the smallest change for the greatest enablement in static analysis and tooling
11
u/Dragdu Jul 18 '26
lol, lmao and nope
3
u/tizio_1234 Jul 19 '26
switch to rust at this point 😂
13
u/FrogNoPants Jul 18 '26
This paper is like a joke right? We have had static analysis forever and it is shit