[clang] Fix lifetimebound for field access (PR #100197)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 24 01:51:12 PDT 2024


================
@@ -47,6 +47,26 @@ namespace usage_ok {
     q = A(); // expected-warning {{object backing the pointer q will be destroyed at the end of the full-expression}}
     r = A(1); // expected-warning {{object backing the pointer r will be destroyed at the end of the full-expression}}
   }
+
+  struct FieldCheck {
+    struct Set {
+      int a;
+    };
+    struct Pair {
+      const int& a;
+      int b;
+      Set c;
+    };
+    Pair p;  
+    FieldCheck(const int a): p(a){}
+    Pair& getPairR() [[clang::lifetimebound]] { return p; }
+    Pair* getPairP() [[clang::lifetimebound]] { return &p; }
+  };
+  void test_field_access() {
+    const int& a = FieldCheck{0}.getPairR().a; // expected-warning {{temporary bound to local reference 'a' will be destroyed at the end of the full-expression}}
+    const int& b = FieldCheck{0}.getPairP()->b; // expected-warning {{temporary bound to local reference 'b' will be destroyed at the end of the full-expression}}
+    const int& c = FieldCheck{0}.getPairP()->c.a; // expected-warning {{temporary bound to local reference 'c' will be destroyed at the end of the full-expression}}
----------------
usx95 wrote:

Done.

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


More information about the cfe-commits mailing list