Skip to content

Commit d3bfc74

Browse files
committed
[flang] Fold KIND= arguments in intrinsic function references
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 #124618.
1 parent 285009f commit d3bfc74

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,7 +2357,7 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
23572357
if (kindArg) {
23582358
if (auto *expr{kindArg->UnwrapExpr()}) {
23592359
CHECK(expr->Rank() == 0);
2360-
if (auto code{ToInt64(*expr)}) {
2360+
if (auto code{ToInt64(Fold(context, common::Clone(*expr)))}) {
23612361
if (context.targetCharacteristics().IsTypeEnabled(
23622362
*category, *code)) {
23632363
if (*category == TypeCategory::Character) { // ACHAR & CHAR
@@ -2369,9 +2369,8 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
23692369
}
23702370
}
23712371
}
2372-
messages.Say("'kind=' argument must be a constant scalar integer "
2373-
"whose value is a supported kind for the "
2374-
"intrinsic result type"_err_en_US);
2372+
messages.Say(
2373+
"'kind=' argument must be a constant scalar integer whose value is a supported kind for the intrinsic result type"_err_en_US);
23752374
// use default kind below for error recovery
23762375
} else if (kindDummyArg->flags.test(ArgFlag::defaultsToSameKind)) {
23772376
CHECK(sameArg);

flang/test/Evaluate/bug124618.f90

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

0 commit comments

Comments
 (0)