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

via flang-commits flang-commits at lists.llvm.org
Tue May 6 18:30:40 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Sebastian Pop (sebpop)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/138793.diff


1 Files Affected:

- (modified) flang/lib/Semantics/expression.cpp (+2-1) 


``````````diff
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);

``````````

</details>


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


More information about the flang-commits mailing list