Index: flang/lib/Semantics/expression.cpp =================================================================== --- flang/lib/Semantics/expression.cpp +++ flang/lib/Semantics/expression.cpp @@ -2669,11 +2669,15 @@ } MaybeExpr ExpressionAnalyzer::Analyze(const parser::Expr::Negate &x) { - if (const auto *litConst{ - std::get_if(&x.v.value().u)}) { - if (const auto *intConst{ - std::get_if(&litConst->u)}) { - return Analyze(*intConst, true); + const auto &scope{ + this->context().FindScope(this->GetContextualMessages().at())}; + if (scope.IsModule() || scope.IsSubmodule()) { + if (const auto *litConst{ + std::get_if(&x.v.value().u)}) { + if (const auto *intConst{ + std::get_if(&litConst->u)}) { + return Analyze(*intConst, true); + } } } return NumericUnaryHelper(*this, NumericOperator::Subtract, x); Index: flang/test/Semantics/negate-analyze.f90 =================================================================== --- /dev/null +++ flang/test/Semantics/negate-analyze.f90 @@ -0,0 +1,61 @@ +! RUN: %flang_fc1 -fdebug-dump-parse-tree %s 2>&1 | FileCheck %s +! Test the analyze of negate expression. + +! CHECK: Name = 'i' +! CHECK: Initialization -> Constant -> Expr = '-2147483648_4' +! CHECK: | Subtract +! CHECK: | | Expr = '-2147483647_4' +! CHECK: | | | Negate -> Expr -> LiteralConstant -> IntLiteralConstant = '2147483647' +! CHECK: | | Expr = '1_4' +! CHECK: | | | LiteralConstant -> IntLiteralConstant = '1' + +! CHECK: Name = 'j' +! CHECK: Initialization -> Constant -> Expr = '-2147483648_4' +! CHECK: | Subtract +! CHECK: | | Expr = '-2147483647_4' +! CHECK: | | | Negate -> Expr = '2147483647_4' +! CHECK: | | | | LiteralConstant -> IntLiteralConstant = '2147483647' +! CHECK: | | Expr = '1_4' +! CHECK: | | | LiteralConstant -> IntLiteralConstant = '1' + +! CHECK: Name = 'k' +! CHECK: Initialization -> Constant -> Expr = '-2147483648_4' +! CHECK: | Subtract +! CHECK: | | Expr = '-2147483647_4' +! CHECK: | | | Negate -> Expr -> LiteralConstant -> IntLiteralConstant = '2147483647' +! CHECK: | | Expr = '1_4' +! CHECK: | | | LiteralConstant -> IntLiteralConstant = '1' + +! CHECK: Variable = 'x' +! CHECK: | Designator -> DataRef -> Name = 'x' +! CHECK: Expr = '-1_4' +! CHECK: | Negate -> Expr = '1_4' +! CHECK: | | LiteralConstant -> IntLiteralConstant = '1' + +! CHECK: Variable = 'x' +! CHECK: | Designator -> DataRef -> Name = 'x' +! CHECK: Expr = '-2147483648_4' +! CHECK: | Subtract +! CHECK: | | Expr = '-2147483647_4' +! CHECK: | | | Negate -> Expr = '2147483647_4' +! CHECK: | | | | LiteralConstant -> IntLiteralConstant = '2147483647' +! CHECK: | | Expr = '1_4' +! CHECK: | | | LiteralConstant -> IntLiteralConstant = '1' + +module m + integer, parameter :: i = -2147483647-1 +contains + subroutine sub() + integer, parameter :: j = -2147483647-1 + end +end + +submodule(m) m2 + integer, parameter :: k = -2147483647-1 +end + +program main + integer :: x + x = -1 + x = -2147483647-1 +end