Skip to content

[flang] fix Werror=dangling-reference #138793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

sebpop
Copy link
Contributor

@sebpop sebpop commented May 7, 2025

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.

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.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels May 7, 2025
@llvmbot
Copy link
Member

llvmbot commented May 7, 2025

@llvm/pr-subscribers-flang-semantics

Author: Sebastian Pop (sebpop)

Changes

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants