-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[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
Conversation
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.
@llvm/pr-subscribers-flang-semantics Author: Sebastian Pop (sebpop) Changeswhen 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] 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:
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);
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use braced initialization; otherwise LGTM, and thanks.
flang/lib/Semantics/expression.cpp
Outdated
@@ -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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use braced initialization here.
Abandoning this patch as the code has changed and flang finishes build without errors on my system. |
when compiling with g++ 13.3.0 flang build on arm64-linux ubuntu 24.04 machine 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.