[clang] [clang] Respect the lifetimebound in assignment operator. (PR #106997)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 2 08:24:21 PDT 2024


================
@@ -287,3 +287,18 @@ std::span<int> test2() {
   return abc; // expected-warning {{address of stack memory associated with local variable}}
 }
 } // namespace ctor_cases
+
+namespace GH106372 {
+class [[gsl::Owner]] Foo {};
+class [[gsl::Pointer]] FooView {};
+
+template <typename T>
+struct StatusOr {
+  template <typename U = T>
+  StatusOr& operator=(U&& v [[clang::lifetimebound]]);
+};
+
+void test(StatusOr<FooView> foo) {
+  foo = Foo(); // expected-warning {{object backing the pointer foo will be destroyed at the end}}
----------------
Xazax-hun wrote:

I am actually a bit split on this code snippet. I think this is probably fine in most cases, but I wonder if this is prone to false positives. We only know that `FooView` is a pointer, but strictly speaking we have no idea what `StatusOr` is doing. We could have an explicit specialization for `StatusOr<FooView>` that actually stores a `Foo` inside instead of `FooView`. Is that good practice? Absolutely not. I am just wondering if this is justified enough for an on by default warning. (Same goes for the initialization case, not just the assignment.) 

https://github.com/llvm/llvm-project/pull/106997


More information about the cfe-commits mailing list