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