[flang-commits] [flang] [flang] fix Werror=dangling-reference (PR #138793)

Sebastian Pop via flang-commits flang-commits at lists.llvm.org
Fri May 16 20:06:19 PDT 2025


https://github.com/sebpop updated https://github.com/llvm/llvm-project/pull/138793

>From b0a6935e8439bc5b4f742f55eb3bb090790a8f95 Mon Sep 17 00:00:00 2001
From: Sebastian Pop <spop at nvidia.com>
Date: Wed, 7 May 2025 01:14:49 +0000
Subject: [PATCH 1/2] [flang] fix Werror=dangling-reference
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

when compiling with g++ 13.3.0 flang build fails with:

llvm-project/flang/lib/Semantics/expression.cpp:424:17: error: possibly dangling reference to a temporary [-Werror=dangling-reference]
424 |   const Symbol &coarraySymbol{ref.GetBase().GetLastSymbol()};
    |                 ^~~~~~~~~~~~~
llvm-project/flang/lib/Semantics/expression.cpp:424:58: note: the temporary was destroyed at the end of the full expression ‘Fortran::evaluate::CoarrayRef::GetBase() const().Fortran::evaluate::NamedEntity::GetLastSymbol()’
424 |   const Symbol &coarraySymbol{ref.GetBase().GetLastSymbol()};
    |                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~

Keep the base in a temporary variable to make sure it is not deleted.
---
 flang/lib/Semantics/expression.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index e139bda7e4950..35eb7b61429fb 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -421,7 +421,8 @@ static void CheckSubscripts(
 
 static void CheckSubscripts(
     semantics::SemanticsContext &context, CoarrayRef &ref) {
-  const Symbol &coarraySymbol{ref.GetBase().GetLastSymbol()};
+  const auto &base = ref.GetBase();
+  const Symbol &coarraySymbol{base.GetLastSymbol()};
   Shape lb, ub;
   if (FoldSubscripts(context, coarraySymbol, ref.subscript(), lb, ub)) {
     ValidateSubscripts(context, coarraySymbol, ref.subscript(), lb, ub);

>From 9bbdf2168adde2293e8289b522b389755f57de14 Mon Sep 17 00:00:00 2001
From: Sebastian Pop <spop at nvidia.com>
Date: Sat, 17 May 2025 03:05:56 +0000
Subject: [PATCH 2/2] use braced initialization

---
 flang/lib/Semantics/expression.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index 35eb7b61429fb..e5a9aef96c1c7 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -421,7 +421,7 @@ static void CheckSubscripts(
 
 static void CheckSubscripts(
     semantics::SemanticsContext &context, CoarrayRef &ref) {
-  const auto &base = ref.GetBase();
+  const auto &base{ref.GetBase()};
   const Symbol &coarraySymbol{base.GetLastSymbol()};
   Shape lb, ub;
   if (FoldSubscripts(context, coarraySymbol, ref.subscript(), lb, ub)) {



More information about the flang-commits mailing list