[clang] [LifetimeSafety] Collect lifetime-annotated origin types from the CFG (PR #246623)
Gábor Horváth via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 27 04:29:56 PST 2026
================
@@ -47,3 +47,25 @@ struct SafeLiteral {
std::string_view v = "literal";
};
void use_safe() { SafeLiteral c; (void)c; }
+
+// A type that has origins only because a lifetimebound function returns it,
+// returned by such a call in a default member initializer.
+struct MyObj {};
+struct View {
+ const MyObj *p;
+};
+View makeView(const MyObj &obj [[clang::lifetimebound]]);
+
+struct ImplicitCtorView {
+ View view{makeView(MyObj{})}; // expected-warning {{stack memory associated with temporary object escapes to the field 'view' which will dangle}}
+ // expected-note at -1 {{result of call to 'makeView' aliases the storage of temporary object because parameter 'obj' is marked as lifetimebound}}
+ // expected-note at -2 {{this field dangles}}
+};
+void use_implicit_view() { ImplicitCtorView c; (void)c; } // perfunc-note {{in implicit default constructor for 'ImplicitCtorView' first required here}}
+
+struct UserCtorView {
+ View view{makeView(MyObj{})}; // expected-warning {{stack memory associated with temporary object escapes to the field 'view' which will dangle}}
+ // expected-note at -1 {{result of call to 'makeView' aliases the storage of temporary object because parameter 'obj' is marked as lifetimebound}}
+ // expected-note at -2 {{this field dangles}}
+ UserCtorView() {}
+};
----------------
Xazax-hun wrote:
Yes, this is absolutely a difference. But I would argue this is a difference that we actually want :) Do you want me to add a test?
https://github.com/llvm/llvm-project/pull/246623
More information about the cfe-commits
mailing list