https://github.com/aeft created https://github.com/llvm/llvm-project/pull/184295
Writing through a reference (e.g., `ref = 10`) does not rebind the reference, so it should not kill the liveness of its underlying origin. Fixes #180187 >From 41d4790af6874a37a3ea371551efcb80c240f889 Mon Sep 17 00:00:00 2001 From: Alex Wang <[email protected]> Date: Mon, 2 Mar 2026 23:34:35 -0800 Subject: [PATCH] [LifetimeSafety] Detect use of a reference type as a use of underlying origin --- clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp | 3 ++- clang/test/Sema/warn-lifetime-safety-invalidations.cpp | 8 +++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp index f39d677758393..6325e63b2388b 100644 --- a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp +++ b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp @@ -347,7 +347,8 @@ void FactsGenerator::handleAssignment(const Expr *LHSExpr, // assigned. RHSList = getRValueOrigins(RHSExpr, RHSList); - if (const auto *DRE_LHS = dyn_cast<DeclRefExpr>(LHSExpr)) + if (const auto *DRE_LHS = dyn_cast<DeclRefExpr>(LHSExpr); + DRE_LHS && !DRE_LHS->getDecl()->getType()->isReferenceType()) markUseAsWrite(DRE_LHS); // Kill the old loans of the destination origin and flow the new loans // from the source origin. diff --git a/clang/test/Sema/warn-lifetime-safety-invalidations.cpp b/clang/test/Sema/warn-lifetime-safety-invalidations.cpp index 65d676cbe8361..60a6be9f0c2a7 100644 --- a/clang/test/Sema/warn-lifetime-safety-invalidations.cpp +++ b/clang/test/Sema/warn-lifetime-safety-invalidations.cpp @@ -259,11 +259,9 @@ namespace ElementReferences { void ReferenceToVectorElement() { std::vector<int> v = {1, 2, 3}; - int& ref = v[0]; - v.push_back(4); - // FIXME: Detect this as a use of 'ref'. - // https://github.com/llvm/llvm-project/issues/180187 - ref = 10; + int& ref = v[0]; // expected-warning {{object whose reference is captured is later invalidated}} + v.push_back(4); // expected-note {{invalidated here}} + ref = 10; // expected-note {{later used here}} (void)ref; } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
