Skip to content

Commit 35b141f

Browse files
authored
[flang] Translate +x to (x), not x (#157513)
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 #157379.
1 parent 7f007b5 commit 35b141f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

flang/lib/Semantics/expression.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3700,7 +3700,10 @@ static MaybeExpr NumericUnaryHelper(ExpressionAnalyzer &context,
37003700
analyzer.CheckForNullPointer();
37013701
analyzer.CheckForAssumedRank();
37023702
if (opr == NumericOperator::Add) {
3703-
return analyzer.MoveExpr(0);
3703+
// +x -> (x), not a bare x, because the bounds of the argument must
3704+
// not be exposed to allocatable assignments or structure constructor
3705+
// components.
3706+
return Parenthesize(analyzer.MoveExpr(0));
37043707
} else {
37053708
return Negation(context.GetContextualMessages(), analyzer.MoveExpr(0));
37063709
}

flang/test/Evaluate/bug157379.f90

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
! RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s
2+
program main
3+
type t
4+
integer, allocatable :: ia(:)
5+
end type
6+
type(t) x
7+
integer, allocatable :: ja(:)
8+
allocate(ja(2:2))
9+
ja(2) = 2
10+
!CHECK: x=t(ia=(ja))
11+
x = t(+ja) ! must be t(ia=(ja)), not t(ia=ja)
12+
print *, lbound(x%ia) ! must be 1, not 2
13+
end

0 commit comments

Comments
 (0)