Skip to content

Commit ff567a4

Browse files
authored
[flang] Fix folding of RANK(assumed-type assumed-rank) (#101027)
The code that deals with the special case of RANK(assumed-rank) in intrinsic function folding wasn't handling the even more special case of assumed-type assumed-rank dummy arguments.
1 parent 6f7e715 commit ff567a4

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

flang/lib/Evaluate/fold-integer.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,20 +1212,23 @@ Expr<Type<TypeCategory::Integer, KIND>> FoldIntrinsicFunction(
12121212
cx->u)};
12131213
}
12141214
} else if (name == "rank") {
1215-
if (const auto *array{UnwrapExpr<Expr<SomeType>>(args[0])}) {
1216-
if (auto named{ExtractNamedEntity(*array)}) {
1217-
const Symbol &symbol{named->GetLastSymbol()};
1218-
if (IsAssumedRank(symbol)) {
1219-
// DescriptorInquiry can only be placed in expression of kind
1220-
// DescriptorInquiry::Result::kind.
1221-
return ConvertToType<T>(Expr<
1222-
Type<TypeCategory::Integer, DescriptorInquiry::Result::kind>>{
1223-
DescriptorInquiry{*named, DescriptorInquiry::Field::Rank}});
1224-
}
1215+
if (args[0]) {
1216+
const Symbol *symbol{nullptr};
1217+
if (auto dataRef{ExtractDataRef(args[0])}) {
1218+
symbol = &dataRef->GetLastSymbol();
1219+
} else {
1220+
symbol = args[0]->GetAssumedTypeDummy();
1221+
}
1222+
if (symbol && IsAssumedRank(*symbol)) {
1223+
// DescriptorInquiry can only be placed in expression of kind
1224+
// DescriptorInquiry::Result::kind.
1225+
return ConvertToType<T>(
1226+
Expr<Type<TypeCategory::Integer, DescriptorInquiry::Result::kind>>{
1227+
DescriptorInquiry{
1228+
NamedEntity{*symbol}, DescriptorInquiry::Field::Rank}});
12251229
}
1226-
return Expr<T>{args[0].value().Rank()};
1230+
return Expr<T>{args[0]->Rank()};
12271231
}
1228-
return Expr<T>{args[0].value().Rank()};
12291232
} else if (name == "selected_char_kind") {
12301233
if (const auto *chCon{UnwrapExpr<Constant<TypeOf<std::string>>>(args[0])}) {
12311234
if (std::optional<std::string> value{chCon->GetScalarValue()}) {

flang/lib/Evaluate/variable.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ DescriptorInquiry::DescriptorInquiry(NamedEntity &&base, Field field, int dim)
250250
const Symbol &last{base_.GetLastSymbol()};
251251
CHECK(IsDescriptor(last));
252252
CHECK((field == Field::Len && dim == 0) ||
253-
(field != Field::Len && dim >= 0 && dim < last.Rank()));
253+
(field != Field::Len && dim >= 0 &&
254+
(dim < last.Rank() || IsAssumedRank(last))));
254255
}
255256

256257
// LEN()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
! RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s
2+
subroutine sub3(ar_at)
3+
type(*) :: ar_at(..)
4+
!CHECK: PRINT *, int(int(rank(ar_at),kind=8),kind=4)
5+
print *, rank(ar_at)
6+
end

0 commit comments

Comments
 (0)