Skip to content

Commit 823094e

Browse files
authored
[flang][semantics] fix IsConstantExpr for intrinsic with optional argument (#161915)
fixes #161694 Exposes that some sequences of duplicate messages are being printed, which is fixed in #161916 .
1 parent ad7334b commit 823094e

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

flang/lib/Evaluate/check-expression.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,22 @@ bool IsConstantExprHelper<INVARIANT>::operator()(
135135
} else if (proc.IsPure()) {
136136
std::size_t j{0};
137137
for (const auto &arg : call.arguments()) {
138-
if (const auto *dataDummy{j < proc.dummyArguments.size()
139-
? std::get_if<characteristics::DummyDataObject>(
140-
&proc.dummyArguments[j].u)
141-
: nullptr};
142-
dataDummy &&
138+
const auto *dataDummy{j < proc.dummyArguments.size()
139+
? std::get_if<characteristics::DummyDataObject>(
140+
&proc.dummyArguments[j].u)
141+
: nullptr};
142+
if (dataDummy &&
143143
dataDummy->attrs.test(
144144
characteristics::DummyDataObject::Attr::OnlyIntrinsicInquiry)) {
145145
// The value of the argument doesn't matter
146146
} else if (!arg) {
147-
return false;
147+
if (dataDummy &&
148+
dataDummy->attrs.test(
149+
characteristics::DummyDataObject::Attr::Optional)) {
150+
// Missing optional arguments are okay.
151+
} else {
152+
return false;
153+
}
148154
} else if (const auto *expr{arg->UnwrapExpr()};
149155
!expr || !(*this)(*expr)) {
150156
return false;

flang/test/Semantics/intrinsics03.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,6 @@ subroutine ichar_tests()
129129
!Without -Wportability, the warning isn't emitted and the parameter is constant.
130130
integer, parameter :: a2 = ichar('B ')
131131
!ERROR: Character in intrinsic function ichar must have length one
132-
!ERROR: Must be a constant value
132+
!ERROR: Value of named constant 'a3' (ichar("")) cannot be computed as a constant value
133133
integer, parameter :: a3 = ichar('')
134134
end subroutine

flang/test/Semantics/intrinsics04.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ subroutine ichar_tests()
2929
!WARNING: Character in intrinsic function ichar should have length one [-Wportability]
3030
integer, parameter :: a2 = ichar('B ')
3131
!ERROR: Character in intrinsic function ichar must have length one
32-
!ERROR: Must be a constant value
32+
!ERROR: Value of named constant 'a3' (ichar("")) cannot be computed as a constant value
3333
integer, parameter :: a3 = ichar('')
3434
end subroutine
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
! RUN: %python %S/test_errors.py %s %flang_fc1
2+
type A (p, r)
3+
integer, kind :: p, r
4+
!ERROR: KIND parameter expression (int(selected_real_kind(six,twenty_three),kind=8)) of intrinsic type REAL did not resolve to a constant value
5+
real (selected_real_kind(p, r)) :: data
6+
end type
7+
integer :: six = 6, twenty_three = 23
8+
type(a(6,23)) :: a1
9+
!ERROR: Value of KIND type parameter 'p' must be constant
10+
!ERROR: Value of KIND type parameter 'r' must be constant
11+
!WARNING: specification expression refers to local object 'six' (initialized and saved) [-Wsaved-local-in-spec-expr]
12+
!WARNING: specification expression refers to local object 'twenty_three' (initialized and saved) [-Wsaved-local-in-spec-expr]
13+
type(a(six, twenty_three)) :: a2
14+
print *, a1%data%kind
15+
end

0 commit comments

Comments
 (0)