From 1fa55cdcdc7a11634f0499364681eb7824cbe2c9 Mon Sep 17 00:00:00 2001 From: Peter Klausler Date: Mon, 8 Sep 2025 10:05:30 -0700 Subject: [PATCH] [flang] Translate +x to (x), not x 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. --- flang/lib/Semantics/expression.cpp | 5 ++++- flang/test/Evaluate/bug157379.f90 | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 flang/test/Evaluate/bug157379.f90 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