[clang] negative-test-capture-by (PR #227251)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 29 03:05:52 PDT 2026


https://github.com/usx95 created https://github.com/llvm/llvm-project/pull/227251

None

>From d1851e9c2892d8b675d0931ac2666f207852a448 Mon Sep 17 00:00:00 2001
From: Utkarsh Saxena <usx at google.com>
Date: Tue, 29 Sep 2026 10:04:01 +0000
Subject: [PATCH] negative-test-capture-by

---
 clang/test/Sema/LifetimeSafety/safety.cpp | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

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