Skip to content

Commit 10b0a07

Browse files
authored
[flang] Fold KIND= arguments in intrinsic function references (llvm#124666)
KIND= arguments in e.g. ACHAR(..., KIND=...) intrinsic function references must be compilation-time constant expressions. The compiler was failing to evaluate those expressions if they were not actually literaly constant values. Fixes llvm#124618.
1 parent 36caa8f commit 10b0a07

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2364,7 +2364,7 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
23642364
if (kindArg) {
23652365
if (auto *expr{kindArg->UnwrapExpr()}) {
23662366
CHECK(expr->Rank() == 0);
2367-
if (auto code{ToInt64(*expr)}) {
2367+
if (auto code{ToInt64(Fold(context, common::Clone(*expr)))}) {
23682368
if (context.targetCharacteristics().IsTypeEnabled(
23692369
*category, *code)) {
23702370
if (*category == TypeCategory::Character) { // ACHAR & CHAR
@@ -2376,9 +2376,8 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
23762376
}
23772377
}
23782378
}
2379-
messages.Say("'kind=' argument must be a constant scalar integer "
2380-
"whose value is a supported kind for the "
2381-
"intrinsic result type"_err_en_US);
2379+
messages.Say(
2380+
"'kind=' argument must be a constant scalar integer whose value is a supported kind for the intrinsic result type"_err_en_US);
23822381
// use default kind below for error recovery
23832382
} else if (kindDummyArg->flags.test(ArgFlag::defaultsToSameKind)) {
23842383
CHECK(sameArg);

flang/test/Evaluate/bug124618.f90

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
! RUN: %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck --allow-empty %s
2+
!CHECK-NOT: error:
3+
real x
4+
print *, char(48, kind=size([x])) ! folds down to 1
5+
end

0 commit comments

Comments
 (0)