Skip to content

Commit 8ccb56c

Browse files
authored
[flang] Fix bad shape analysis of assumed-rank dummy (#92936)
Shape analysis for the results of SHAPE, LBOUND, and UBOUND (without DIM=) needs to account for an assumed-rank dummy argument, and return a shape vector with a single unknown element.
1 parent c24f881 commit 8ccb56c

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

flang/lib/Evaluate/shape.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,8 +885,12 @@ auto GetShapeHelper::operator()(const ProcedureRef &call) const -> Result {
885885
intrinsic->name == "ubound") {
886886
// For LBOUND/UBOUND, these are the array-valued cases (no DIM=)
887887
if (!call.arguments().empty() && call.arguments().front()) {
888-
return Shape{
889-
MaybeExtentExpr{ExtentExpr{call.arguments().front()->Rank()}}};
888+
if (IsAssumedRank(*call.arguments().front())) {
889+
return Shape{MaybeExtentExpr{}};
890+
} else {
891+
return Shape{
892+
MaybeExtentExpr{ExtentExpr{call.arguments().front()->Rank()}}};
893+
}
890894
}
891895
} else if (intrinsic->name == "all" || intrinsic->name == "any" ||
892896
intrinsic->name == "count" || intrinsic->name == "iall" ||

flang/test/Semantics/shape.f90

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
! Test comparisons that use the intrinsic SHAPE() as an operand
33
program testShape
44
contains
5-
subroutine sub1(arrayDummy)
6-
integer :: arrayDummy(:)
5+
subroutine sub1(arrayDummy, assumedRank)
6+
integer :: arrayDummy(:), assumedRank(..)
77
integer, allocatable :: arrayDeferred(:)
88
integer :: arrayLocal(2) = [88, 99]
9+
integer, parameter :: aRrs = rank(shape(assumedRank))
10+
integer(kind=merge(kind(1),-1,aRrs == 1)) :: test_aRrs
911
!ERROR: Dimension 1 of left operand has extent 1, but right operand has extent 0
1012
!ERROR: Dimension 1 of left operand has extent 1, but right operand has extent 0
1113
if (all(shape(arrayDummy)==shape(8))) then
@@ -45,5 +47,9 @@ subroutine sub1(arrayDummy)
4547
if (all(64==shape(arrayLocal))) then
4648
print *, "hello"
4749
end if
50+
! These can't be checked at compilation time
51+
if (any(shape(assumedRank) == [1])) stop
52+
if (any(lbound(assumedRank) == [1,2])) stop
53+
if (any(ubound(assumedRank) == [1,2,3])) stop
4854
end subroutine sub1
4955
end program testShape

0 commit comments

Comments
 (0)