Skip to content

Commit

Permalink
[flang] Don't allow CALL RANDOM_NUMBER(assumed-size-array)
Browse files Browse the repository at this point in the history
The extents, if any, of the HARVEST= actual argument must be known
at execution time for the call to be implemented.

Differential Revision: https://reviews.llvm.org/D147391
  • Loading branch information
klausler committed Apr 3, 2023
1 parent cacc003 commit 2107fe3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions flang/docs/Extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ end
* A module name from a `USE` statement can also be used as a
non-global name in the same scope. This is not conforming,
but it is useful and unambiguous.
* The argument to `RANDOM_NUMBER` may not be an assumed-size array.

## Extensions, deletions, and legacy features supported by default

Expand Down
22 changes: 16 additions & 6 deletions flang/lib/Evaluate/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ ENUM_CLASS(ArgFlag, none,
defaultsToSameKind, // for MatchingDefaultKIND
defaultsToSizeKind, // for SizeDefaultKIND
defaultsToDefaultForResult, // for DefaultingKIND
)
notAssumedSize)

struct IntrinsicDummyArgument {
const char *keyword{nullptr};
Expand Down Expand Up @@ -813,8 +813,9 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
Rank::scalar, IntrinsicClass::inquiryFunction},
{"spacing", {{"x", SameReal}}, SameReal},
{"spread",
{{"source", SameType, Rank::known}, RequiredDIM,
{"ncopies", AnyInt, Rank::scalar}},
{{"source", SameType, Rank::known, Optionality::required,
common::Intent::In, {ArgFlag::notAssumedSize}},
RequiredDIM, {"ncopies", AnyInt, Rank::scalar}},
SameType, Rank::rankPlus1, IntrinsicClass::transformationalFunction},
{"sqrt", {{"x", SameFloating}}, SameFloating},
{"stopped_images", {OptionalTEAM, SizeDefaultKIND}, KINDInt, Rank::vector,
Expand Down Expand Up @@ -1366,7 +1367,7 @@ static const IntrinsicInterface intrinsicSubroutine[]{
{}, Rank::elemental, IntrinsicClass::impureSubroutine},
{"random_number",
{{"harvest", AnyReal, Rank::known, Optionality::required,
common::Intent::Out}},
common::Intent::Out, {ArgFlag::notAssumedSize}}},
{}, Rank::elemental, IntrinsicClass::impureSubroutine},
{"random_seed",
{{"size", DefaultInt, Rank::scalar, Optionality::optional,
Expand Down Expand Up @@ -1689,6 +1690,16 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
}
}
}
if (d.flags.test(ArgFlag::notAssumedSize)) {
if (auto named{ExtractNamedEntity(*arg)}) {
if (semantics::IsAssumedSizeArray(named->GetLastSymbol())) {
messages.Say(arg->sourceLocation(),
"The '%s=' argument to the intrinsic procedure '%s' may not be assumed-size"_err_en_US,
d.keyword, name);
return std::nullopt;
}
}
}
if (arg->GetAssumedTypeDummy()) {
// TYPE(*) assumed-type dummy argument forwarded to intrinsic
if (d.typePattern.categorySet == AnyType &&
Expand Down Expand Up @@ -1973,8 +1984,7 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
if (semantics::IsAssumedSizeArray(named->GetLastSymbol())) {
if (strcmp(name, "shape") == 0) {
messages.Say(arg->sourceLocation(),
"The '%s=' argument to the intrinsic function '%s' may not be assumed-size"_err_en_US,
d.keyword, name);
"The 'source=' argument to the intrinsic function 'shape' may not be assumed-size"_err_en_US);
} else {
messages.Say(arg->sourceLocation(),
"A dim= argument is required for '%s' when the array is assumed-size"_err_en_US,
Expand Down
2 changes: 2 additions & 0 deletions flang/test/Semantics/misc-intrinsics.f90
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ subroutine test(arg)
print *, ubound(arg)
!ERROR: The 'source=' argument to the intrinsic function 'shape' may not be assumed-size
print *, shape(arg)
!ERROR: The 'harvest=' argument to the intrinsic procedure 'random_number' may not be assumed-size
call random_number(arg)
!ERROR: missing mandatory 'dim=' argument
print *, lbound(scalar)
!ERROR: 'array=' argument has unacceptable rank 0
Expand Down

0 comments on commit 2107fe3

Please sign in to comment.