Skip to content

Commit 35a67cb

Browse files
committed
[flang] Fix crash from fuzzy test.
Fixes #122002.
1 parent 2c2accb commit 35a67cb

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

flang/lib/Evaluate/shape.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ class GetLowerBoundHelper
254254
if (dimension_ < rank) {
255255
const semantics::ShapeSpec &shapeSpec{object->shape()[dimension_]};
256256
if (shapeSpec.lbound().isExplicit()) {
257-
if (const auto &lbound{shapeSpec.lbound().GetExplicit()}) {
257+
if (const auto &lbound{shapeSpec.lbound().GetExplicit()};
258+
lbound && lbound->Rank() == 0) {
258259
if constexpr (LBOUND_SEMANTICS) {
259260
bool ok{false};
260261
auto lbValue{ToInt64(*lbound)};
@@ -266,7 +267,8 @@ class GetLowerBoundHelper
266267
} else if (lbValue.value_or(0) == 1) {
267268
// Lower bound is 1, regardless of extent
268269
ok = true;
269-
} else if (const auto &ubound{shapeSpec.ubound().GetExplicit()}) {
270+
} else if (const auto &ubound{shapeSpec.ubound().GetExplicit()};
271+
ubound && ubound->Rank() == 0) {
270272
// If we can't prove that the dimension is nonempty,
271273
// we must be conservative.
272274
// TODO: simple symbolic math in expression rewriting to
@@ -459,7 +461,7 @@ static MaybeExtentExpr GetNonNegativeExtent(
459461
} else {
460462
return ExtentExpr{*uval - *lval + 1};
461463
}
462-
} else if (lbound && ubound &&
464+
} else if (lbound && ubound && lbound->Rank() == 0 && ubound->Rank() == 0 &&
463465
(!invariantOnly ||
464466
(IsScopeInvariantExpr(*lbound) && IsScopeInvariantExpr(*ubound)))) {
465467
// Apply effective IDIM (MAX calculation with 0) so thet the
@@ -608,7 +610,8 @@ MaybeExtentExpr GetRawUpperBound(
608610
int rank{details->shape().Rank()};
609611
if (dimension < rank) {
610612
const auto &bound{details->shape()[dimension].ubound().GetExplicit()};
611-
if (bound && (!invariantOnly || IsScopeInvariantExpr(*bound))) {
613+
if (bound && bound->Rank() == 0 &&
614+
(!invariantOnly || IsScopeInvariantExpr(*bound))) {
612615
return *bound;
613616
} else if (semantics::IsAssumedSizeArray(symbol) &&
614617
dimension + 1 == symbol.Rank()) {
@@ -640,7 +643,8 @@ MaybeExtentExpr GetRawUpperBound(FoldingContext &context,
640643
static MaybeExtentExpr GetExplicitUBOUND(FoldingContext *context,
641644
const semantics::ShapeSpec &shapeSpec, bool invariantOnly) {
642645
const auto &ubound{shapeSpec.ubound().GetExplicit()};
643-
if (ubound && (!invariantOnly || IsScopeInvariantExpr(*ubound))) {
646+
if (ubound && ubound->Rank() == 0 &&
647+
(!invariantOnly || IsScopeInvariantExpr(*ubound))) {
644648
if (auto extent{GetNonNegativeExtent(shapeSpec, invariantOnly)}) {
645649
if (auto cstExtent{ToInt64(
646650
context ? Fold(*context, std::move(*extent)) : *extent)}) {
@@ -731,7 +735,8 @@ MaybeExtentExpr GetLCOBOUND(
731735
if (dimension < corank) {
732736
const semantics::ShapeSpec &shapeSpec{object->coshape()[dimension]};
733737
if (const auto &lcobound{shapeSpec.lbound().GetExplicit()}) {
734-
if (!invariantOnly || IsScopeInvariantExpr(*lcobound)) {
738+
if (lcobound->Rank() == 0 &&
739+
(!invariantOnly || IsScopeInvariantExpr(*lcobound))) {
735740
return *lcobound;
736741
}
737742
}
@@ -748,7 +753,8 @@ MaybeExtentExpr GetUCOBOUND(
748753
if (dimension < corank - 1) {
749754
const semantics::ShapeSpec &shapeSpec{object->coshape()[dimension]};
750755
if (const auto ucobound{shapeSpec.ubound().GetExplicit()}) {
751-
if (!invariantOnly || IsScopeInvariantExpr(*ucobound)) {
756+
if (ucobound->Rank() == 0 &&
757+
(!invariantOnly || IsScopeInvariantExpr(*ucobound))) {
752758
return *ucobound;
753759
}
754760
}

flang/test/Semantics/bug122002.f90

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
! RUN: %python %S/test_errors.py %s %flang_fc1
2+
! ERROR: Missing initialization for parameter 'n'
3+
! ERROR: Must be a scalar value, but is a rank-1 array
4+
integer, parameter :: n(n)
5+
end

0 commit comments

Comments
 (0)