[clang] [LifetimeSafety] Add negative-test for `lifetime_capture_by_this` on constructors (PR #227251)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 29 03:08:15 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Utkarsh Saxena (usx95)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/227251.diff
1 Files Affected:
- (modified) clang/test/Sema/LifetimeSafety/safety.cpp (+22)
``````````diff
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};
``````````
</details>
https://github.com/llvm/llvm-project/pull/227251
More information about the cfe-commits
mailing list