Skip to content

Commit 016557f

Browse files
authored
[flang] Better error handling for PDT constant expression (#130637)
We currently don't consider an integer division to be a constant expression unless its divisor is known and nonzero. This produces a weird result in the linked test; do better. Fixes #130534.
1 parent af62a6f commit 016557f

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

flang/lib/Evaluate/check-expression.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,14 @@ class IsConstantExprHelper
6969
bool operator()(const Component &component) const {
7070
return (*this)(component.base());
7171
}
72-
// Forbid integer division by zero in constants.
72+
// Prevent integer division by known zeroes in constant expressions.
7373
template <int KIND>
7474
bool operator()(
7575
const Divide<Type<TypeCategory::Integer, KIND>> &division) const {
7676
using T = Type<TypeCategory::Integer, KIND>;
77-
if (const auto divisor{GetScalarConstantValue<T>(division.right())}) {
78-
return !divisor->IsZero() && (*this)(division.left());
77+
if ((*this)(division.left()) && (*this)(division.right())) {
78+
const auto divisor{GetScalarConstantValue<T>(division.right())};
79+
return !divisor || !divisor->IsZero();
7980
} else {
8081
return false;
8182
}

flang/lib/Semantics/type.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,18 @@ const DeclTypeSpec &InstantiateHelper::InstantiateIntrinsicType(
567567
kind = *value;
568568
} else {
569569
foldingContext().messages().Say(symbolName,
570-
"KIND parameter value (%jd) of intrinsic type %s "
571-
"did not resolve to a supported value"_err_en_US,
570+
"KIND parameter value (%jd) of intrinsic type %s did not resolve to a supported value"_err_en_US,
572571
*value,
573572
parser::ToUpperCaseLetters(EnumToString(intrinsic.category())));
574573
}
574+
} else {
575+
std::string exprString;
576+
llvm::raw_string_ostream sstream(exprString);
577+
copy.AsFortran(sstream);
578+
foldingContext().messages().Say(symbolName,
579+
"KIND parameter expression (%s) of intrinsic type %s did not resolve to a constant value"_err_en_US,
580+
exprString,
581+
parser::ToUpperCaseLetters(EnumToString(intrinsic.category())));
575582
}
576583
switch (spec.category()) {
577584
case DeclTypeSpec::Numeric:

flang/test/Semantics/pdt04.f90

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
!RUN: not %flang_fc1 %s 2>&1 | FileCheck %s
2+
!CHECK: error: KIND parameter expression (int(1_4/0_4,kind=8)) of intrinsic type CHARACTER did not resolve to a constant value
3+
!CHECK: in the context: instantiation of parameterized derived type 'ty(j=1_4,k=0_4)'
4+
!CHECK: warning: INTEGER(4) division by zero
5+
program main
6+
type ty(j,k)
7+
integer, kind :: j, k
8+
character(kind=j/k) a
9+
end type
10+
type(ty(1,0)) x
11+
end

flang/test/Semantics/resolve105.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
! RUN: %python %S/test_errors.py %s %flang_fc1
22
! Test instantiation of components that are procedure pointers.
3-
!
43
program test
54
type dtype(kindParam)
65
integer, kind :: kindParam = 4
6+
!ERROR: KIND parameter expression (kindparam) of intrinsic type REAL did not resolve to a constant value
77
!ERROR: KIND parameter value (66) of intrinsic type REAL did not resolve to a supported value
88
!ERROR: KIND parameter value (55) of intrinsic type REAL did not resolve to a supported value
99
procedure (real(kindParam)), pointer, nopass :: field => null()

0 commit comments

Comments
 (0)