TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Lifetime Annotations for C++

184 pointsby aklingabout 3 years ago

14 comments

verdagonabout 3 years ago
&gt; To summarize, enforcing [Rust&#x27;s] borrowing rule in C++ is unfortunately not so simple because there is a lot of existing code that creates multiple non-const pointers or references to the same object, intentionally violating the borrowing rule. At this point we don’t have a plan of how we could incrementally roll out the borrowing rule to existing C++ code, but it is a very interesting direction for future work.<p>This is actually possible for C++, if we add a concept of pure functions: functions that don&#x27;t modify anything indirectly reachable from arguments (or globals). Basically:<p>* Annotate those parameters as &quot;immutable&quot; or as coming from the same immutable &quot;region&quot;. Anything reached through them would also have that immutable annotation.<p>* Anything created during the call would not have that annotation.<p>The type system, basically a borrow checker, could keep them separate and make sure we don&#x27;t modify anything that came from outside the function.<p>We&#x27;re currently adding this to Vale [0], It&#x27;s a way to blend shared mutability with borrow checking, and it gives us the optimization power and memory safety of borrow checking without all the constraints and learning curve problems.<p>[0]: <a href="https:&#x2F;&#x2F;verdagon.dev&#x2F;blog&#x2F;seamless-fearless-structured-concurrency" rel="nofollow">https:&#x2F;&#x2F;verdagon.dev&#x2F;blog&#x2F;seamless-fearless-structured-concu...</a>
评论 #30890261 未加载
评论 #30890280 未加载
评论 #30894000 未加载
htfy96about 3 years ago
This reminds me of the -Wlifetime proposal, which provides similar checks but requires annotation of ownership at struct level (hence the check only applies to new structs):<p>An example: <a href="https:&#x2F;&#x2F;github.com&#x2F;llvm&#x2F;llvm-project&#x2F;blob&#x2F;main&#x2F;clang&#x2F;test&#x2F;Sema&#x2F;warn-lifetime-analysis-nocfg.cpp" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;llvm&#x2F;llvm-project&#x2F;blob&#x2F;main&#x2F;clang&#x2F;test&#x2F;Se...</a><p>More details can be found at <a href="https:&#x2F;&#x2F;herbsutter.com&#x2F;2018&#x2F;09&#x2F;20&#x2F;lifetime-profile-v1-0-posted&#x2F;" rel="nofollow">https:&#x2F;&#x2F;herbsutter.com&#x2F;2018&#x2F;09&#x2F;20&#x2F;lifetime-profile-v1-0-post...</a><p>Unfortunately it was never upstreamed according to <a href="https:&#x2F;&#x2F;github.com&#x2F;mgehre&#x2F;llvm-project&#x2F;issues&#x2F;98" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mgehre&#x2F;llvm-project&#x2F;issues&#x2F;98</a>
评论 #30890340 未加载
benreesmanabout 3 years ago
I tend to think that the ROI for large legacy codebases is in static analysis and instrumented tooling like the sanitizers.<p>I’m grudgingly coming around to the idea that Rust is probably a rock-solid FFI story away from being a serious part of my kit (pybind11 good, not like anything we have now).<p>But there is this in-between place where first-class linear&#x2F;affine typing could be bootstrapped off of move semantics.
评论 #30890635 未加载
评论 #30891710 未加载
summerlightabout 3 years ago
Looks like at least some of the authors are working for Google (if not everyone), with compiler backgrounds. I wonder if this is a strategical investment from Google to make cxx like approach more robust? Chrome team showed some interests on using Rust but not sure if there&#x27;s any significant code written in Rust from then. Benefit from using Rust beside well isolated library might be quite limited without this kind of lifetime annotation while interop itself is additional cognitive overheads for programmers.
评论 #30890614 未加载
westurnerabout 3 years ago
From <a href="https:&#x2F;&#x2F;discourse.llvm.org&#x2F;t&#x2F;rfc-lifetime-annotations-for-c&#x2F;61377" rel="nofollow">https:&#x2F;&#x2F;discourse.llvm.org&#x2F;t&#x2F;rfc-lifetime-annotations-for-c&#x2F;...</a> :<p>&gt; <i>We are designing, implementing, and evaluating an attribute-based annotation scheme for C++ that describes object lifetime contracts. It allows relatively cheap, scalable, local static analysis to find many common cases of heap-use-after-free and stack-use-after-return bugs. It allows other static analysis algorithms to be less conservative in their modeling of the C++ object graph and potential mutations done to it. Lifetime annotations also enable better C++&#x2F;Rust and C++&#x2F;Swift interoperability.</i><p>&gt; <i>This annotation scheme is inspired by Rust lifetimes, but it is adapted to C++ so that it can be incrementally rolled out to existing C++ codebases. Furthermore, the annotations can be automatically added to an existing codebase by a tool that infers the annotations based on the current behavior of each function’s implementation.</i><p>&gt; <i>Clang has existing features for detecting lifetime bugs</i> [...]
评论 #30889063 未加载
StillBoredabout 3 years ago
I think this is a good idea, mostly because I&#x27;ve been of the opinion that much of the rust &quot;safety&quot; could be done with a linter pass in C++. These annotations should help solve the edge cases that aren&#x27;t deterministic.<p>I&#x27;ve not generally been a big fan of where C++ has been going in the last couple revisions, mostly because it seems the newer features aren&#x27;t fully thought out when it comes to how they interact with other features (reminds me of javascript in that regard) leaving a bunch of new footguns, looks at lambda&#x27;s.<p>I think i&#x27;ve said this here before, that C++ needs its own version of &quot;use strict;&quot; that basically kills off some of the syntactically odd corner cases that lead to those footguns.
c0baltabout 3 years ago
Sounds awesome. After the initial hurdle of getting used to lifetimes in rust it was really an enjoyable time and I would love to see the same feature in c++.
tialaramexabout 3 years ago
Maybe a C++ person can help me out, I am staring at this C++ translation of Rust&#x27;s elision rules:<p>&gt; If there are multiple input lifetimes but one of them applies to the implicit this parameter, that lifetime is assigned to all elided output lifetimes.<p>In Rust we have <i>self</i> rather than <i>this</i> in methods, but importantly we sometimes don&#x27;t take a reference here, and that&#x27;s <i>still</i> a method, and you still have the <i>self</i> parameter, but the lifetime elision rules don&#x27;t apply. They don&#x27;t apply because if you&#x27;ve actually got self, not some reference to self, the lifetime of self is going to end when the function finishes, so returning things with that lifetime is nonsense.<p>This can make sense in Rust for transformative methods. If there&#x27;s a method on a God that turns them into a Mortal, the God doesn&#x27;t exist any more when that method exits, the Mortal is the return type, and if you just drop it then I guess sucks to be them. (In Rust, and I think in C++ you can label something as must-use and cause a warning or error if the programmer forgets to use it).<p>It seems though, as if this C++ rule would hit such methods too. Can you distinguish in C++ between &quot;a reference to me&quot; and &quot;me&quot; when it comes to methods? If you had a method on a God class in C++ that transforms the God into a Mortal and returns the Mortal, what does that look like? Does this elision rule make sense to you for such a scenario?
评论 #30890240 未加载
malkiaabout 3 years ago
April fools, right? Like std::weekend_ptr - holds the ptr only during the weekend....
评论 #30890613 未加载
评论 #30890548 未加载
queuebertabout 3 years ago
Pardon the stupid question, but at this point why not just use Rust? It interoperates with C++ fairly well.
评论 #30895418 未加载
评论 #30894865 未加载
lamp987about 3 years ago
10 years from now, there will be nobody on earth able to say &quot;i know c++&quot; because each codebase will use a completely different set of bazillion optional features making each codebase look like an entirely different language from each other.<p>but maybe thats how we&#x27;ll finally get rid of c++.
评论 #30890542 未加载
评论 #30889694 未加载
评论 #30890005 未加载
评论 #30889561 未加载
评论 #30889707 未加载
评论 #30889879 未加载
评论 #30893684 未加载
评论 #30889600 未加载
评论 #30890586 未加载
aaaaaaaaaaababout 3 years ago
Nooo! If this gets implemented then Rust is basically killed… :(
评论 #30894392 未加载
评论 #30893137 未加载
323about 3 years ago
What happens if you make an annotation mistake? Could the compiler generated code then have a security vulnerability, just like when you use undefined behaviour and the compiler is then allowed to optimize safety checks away?
评论 #30890324 未加载
评论 #30892404 未加载
kjksfabout 3 years ago
It&#x27;s a bad sign that I can&#x27;t tell if someone spent a lot of time on an elaborate April&#x27;s Fool joke or if it&#x27;s a serious C++ proposal.
评论 #30889545 未加载
评论 #30889540 未加载
评论 #30890557 未加载