[clang] 84c14da - [LifetimeSafety] Add negative-test for `lifetime_capture_by_this` on constructors (#227251)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 29 03:59:14 PDT 2026
Author: Utkarsh Saxena
Date: 2026-09-29T10:59:07Z
New Revision: 84c14dac5c730f5b888b42193d252eb41cc77247
URL: https://github.com/llvm/llvm-project/commit/84c14dac5c730f5b888b42193d252eb41cc77247
DIFF: https://github.com/llvm/llvm-project/commit/84c14dac5c730f5b888b42193d252eb41cc77247.diff
LOG: [LifetimeSafety] Add negative-test for `lifetime_capture_by_this` on constructors (#227251)
Added:
Modified:
clang/test/Sema/LifetimeSafety/safety.cpp
Removed:
################################################################################
diff --git a/clang/test/Sema/LifetimeSafety/safety.cpp b/clang/test/Sema/LifetimeSafety/safety.cpp
index fb91fb30713db..cd16a4d723f84 100644
--- a/clang/test/Sema/LifetimeSafety/safety.cpp
+++ b/clang/test/Sema/LifetimeSafety/safety.cpp
@@ -4099,6 +4099,28 @@ struct Helper {
};
} // namespace off_by_one_crash
+// FIXME(GH227246): Support capture_by_this on constructors without inference of lifetimebound.
+namespace constructor_test {
+struct Item {};
+
+struct Container {
+ const Item* saved;
+ Container() = default;
+ Container(const Item& item [[clang::lifetime_capture_by_this]]) {
+ saved = &item;
+ }
+};
+
+void test() {
+ Container c;
+ {
+ Item item;
+ c = Container(item); // tu-warning {{local variable 'item' does not live long enough}}
+ } // tu-note {{local variable 'item' is destroyed here}}
+ use(c); // tu-note {{later used here}}
+}
+} // namespace constructor_test
+
struct [[gsl::Pointer()]] PtrWithInt { int x; };
PtrWithInt f() {
return PtrWithInt{10};
More information about the cfe-commits
mailing list