Skip to content

Commit 39b0433

Browse files
authored
[flang] Extend assumed-size array checking in intrinsic functions (#139339)
The array argument of a reference to the intrinsic functions SHAPE can't be assumed-size; and for SIZE and UBOUND, it can be assumed-size only if DIM= is present. The checks for thes restrictions don't allow for host association, or for associate entities (ASSOCIATE, SELECT TYPE) that are variables. Fixes #138926.
1 parent f600154 commit 39b0433

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,7 +2340,7 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
23402340
if (!knownArg) {
23412341
knownArg = arg;
23422342
}
2343-
if (!dimArg && rank > 0 &&
2343+
if (rank > 0 &&
23442344
(std::strcmp(name, "shape") == 0 ||
23452345
std::strcmp(name, "size") == 0 ||
23462346
std::strcmp(name, "ubound") == 0)) {
@@ -2351,16 +2351,18 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
23512351
// over this one, as this error is caught by the second entry
23522352
// for UBOUND.)
23532353
if (auto named{ExtractNamedEntity(*arg)}) {
2354-
if (semantics::IsAssumedSizeArray(named->GetLastSymbol())) {
2354+
if (semantics::IsAssumedSizeArray(ResolveAssociations(
2355+
named->GetLastSymbol().GetUltimate()))) {
23552356
if (strcmp(name, "shape") == 0) {
23562357
messages.Say(arg->sourceLocation(),
23572358
"The 'source=' argument to the intrinsic function 'shape' may not be assumed-size"_err_en_US);
2358-
} else {
2359+
return std::nullopt;
2360+
} else if (!dimArg) {
23592361
messages.Say(arg->sourceLocation(),
23602362
"A dim= argument is required for '%s' when the array is assumed-size"_err_en_US,
23612363
name);
2364+
return std::nullopt;
23622365
}
2363-
return std::nullopt;
23642366
}
23652367
}
23662368
}

flang/test/Semantics/misc-intrinsics.f90

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,37 @@
33
program test_size
44
real :: scalar
55
real, dimension(5, 5) :: array
6-
call test(array, array)
6+
call test(array, array, array)
77
contains
8-
subroutine test(arg, assumedRank)
8+
subroutine test(arg, assumedRank, poly)
99
real, dimension(5, *) :: arg
1010
real, dimension(..) :: assumedRank
11+
class(*) :: poly(5, *)
1112
!ERROR: A dim= argument is required for 'size' when the array is assumed-size
1213
print *, size(arg)
14+
print *, size(arg, dim=1) ! ok
15+
select type (poly)
16+
type is (real)
17+
!ERROR: A dim= argument is required for 'size' when the array is assumed-size
18+
print *, size(poly)
19+
print *, size(poly, dim=1) ! ok
20+
end select
1321
!ERROR: A dim= argument is required for 'ubound' when the array is assumed-size
1422
print *, ubound(arg)
23+
print *, ubound(arg, dim=1) ! ok
24+
select type (poly)
25+
type is (real)
26+
!ERROR: A dim= argument is required for 'ubound' when the array is assumed-size
27+
print *, ubound(poly)
28+
print *, ubound(poly, dim=1) ! ok
29+
end select
1530
!ERROR: The 'source=' argument to the intrinsic function 'shape' may not be assumed-size
1631
print *, shape(arg)
32+
select type (poly)
33+
type is (real)
34+
!ERROR: The 'source=' argument to the intrinsic function 'shape' may not be assumed-size
35+
print *, shape(poly)
36+
end select
1737
!ERROR: The 'harvest=' argument to the intrinsic procedure 'random_number' may not be assumed-size
1838
call random_number(arg)
1939
!ERROR: 'array=' argument has unacceptable rank 0
@@ -85,5 +105,16 @@ subroutine test(arg, assumedRank)
85105
print *, lbound(assumedRank, dim=2)
86106
print *, ubound(assumedRank, dim=2)
87107
end select
108+
contains
109+
subroutine inner
110+
!ERROR: A dim= argument is required for 'size' when the array is assumed-size
111+
print *, size(arg)
112+
print *, size(arg, dim=1) ! ok
113+
!ERROR: A dim= argument is required for 'ubound' when the array is assumed-size
114+
print *, ubound(arg)
115+
print *, ubound(arg, dim=1) ! ok
116+
!ERROR: The 'source=' argument to the intrinsic function 'shape' may not be assumed-size
117+
print *, shape(arg)
118+
end
88119
end subroutine
89120
end

0 commit comments

Comments
 (0)