[flang-commits] [flang] [flang] Translate +x to (x), not x (PR #157513)
via flang-commits
flang-commits at lists.llvm.org
Mon Sep 8 10:09:17 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-semantics
Author: Peter Klausler (klausler)
<details>
<summary>Changes</summary>
In expression semantics, don't completely delete the unary plus operator, but instead translate it into parentheses. The distinction matters in contexts where the bounds of x are significant or when x must not be misinterpreted as a variable.
Fixes https://github.com/llvm/llvm-project/issues/157379.
---
Full diff: https://github.com/llvm/llvm-project/pull/157513.diff
2 Files Affected:
- (modified) flang/lib/Semantics/expression.cpp (+4-1)
- (added) flang/test/Evaluate/bug157379.f90 (+13)
``````````diff
diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index ccccf60adae5d..3f048ab6f7a4d 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -3700,7 +3700,10 @@ static MaybeExpr NumericUnaryHelper(ExpressionAnalyzer &context,
analyzer.CheckForNullPointer();
analyzer.CheckForAssumedRank();
if (opr == NumericOperator::Add) {
- return analyzer.MoveExpr(0);
+ // +x -> (x), not a bare x, because the bounds of the argument must
+ // not be exposed to allocatable assignments or structure constructor
+ // components.
+ return Parenthesize(analyzer.MoveExpr(0));
} else {
return Negation(context.GetContextualMessages(), analyzer.MoveExpr(0));
}
diff --git a/flang/test/Evaluate/bug157379.f90 b/flang/test/Evaluate/bug157379.f90
new file mode 100644
index 0000000000000..53aac4c29d3e4
--- /dev/null
+++ b/flang/test/Evaluate/bug157379.f90
@@ -0,0 +1,13 @@
+! RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s
+program main
+ type t
+ integer, allocatable :: ia(:)
+ end type
+ type(t) x
+ integer, allocatable :: ja(:)
+ allocate(ja(2:2))
+ ja(2) = 2
+ !CHECK: x=t(ia=(ja))
+ x = t(+ja) ! must be t(ia=(ja)), not t(ia=ja)
+ print *, lbound(x%ia) ! must be 1, not 2
+end
``````````
</details>
https://github.com/llvm/llvm-project/pull/157513
More information about the flang-commits
mailing list