[flang-commits] [flang] [flang][semantics] fix IsConstantExpr for intrinsic with optional argument (PR #161915)

Andre Kuhlenschmidt via flang-commits flang-commits at lists.llvm.org
Fri Oct 3 16:06:39 PDT 2025


https://github.com/akuhlens updated https://github.com/llvm/llvm-project/pull/161915

>From 5a52dcaad487535733bac61c362d74a9498c3ae4 Mon Sep 17 00:00:00 2001
From: Andre Kuhlenschmidt <akuhlenschmi at nvidia.com>
Date: Fri, 3 Oct 2025 09:32:18 -0700
Subject: [PATCH 1/3] initial commit

---
 flang/lib/Evaluate/check-expression.cpp | 17 +++++++++++------
 flang/test/Semantics/intrinsics03.f90   |  5 ++++-
 flang/test/Semantics/intrinsics04.f90   |  5 ++++-
 3 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/flang/lib/Evaluate/check-expression.cpp b/flang/lib/Evaluate/check-expression.cpp
index 8931cbe485ac2..012b44e7d3747 100644
--- a/flang/lib/Evaluate/check-expression.cpp
+++ b/flang/lib/Evaluate/check-expression.cpp
@@ -135,16 +135,21 @@ bool IsConstantExprHelper<INVARIANT>::operator()(
     } else if (proc.IsPure()) {
       std::size_t j{0};
       for (const auto &arg : call.arguments()) {
-        if (const auto *dataDummy{j < proc.dummyArguments.size()
-                    ? std::get_if<characteristics::DummyDataObject>(
-                          &proc.dummyArguments[j].u)
-                    : nullptr};
-            dataDummy &&
+        const auto *dataDummy{j < proc.dummyArguments.size()
+                ? std::get_if<characteristics::DummyDataObject>(
+                      &proc.dummyArguments[j].u)
+                : nullptr};
+        if (dataDummy &&
             dataDummy->attrs.test(
                 characteristics::DummyDataObject::Attr::OnlyIntrinsicInquiry)) {
           // The value of the argument doesn't matter
         } else if (!arg) {
-          return false;
+          // Missing optional arguments are constant
+          if (!(dataDummy &&
+                  dataDummy->attrs.test(
+                      characteristics::DummyDataObject::Attr::Optional))) {
+            return false;
+          }
         } else if (const auto *expr{arg->UnwrapExpr()};
             !expr || !(*this)(*expr)) {
           return false;
diff --git a/flang/test/Semantics/intrinsics03.f90 b/flang/test/Semantics/intrinsics03.f90
index a5b13b655cf41..2b07c5ee98eaf 100644
--- a/flang/test/Semantics/intrinsics03.f90
+++ b/flang/test/Semantics/intrinsics03.f90
@@ -129,6 +129,9 @@ subroutine ichar_tests()
   !Without -Wportability, the warning isn't emitted and the parameter is constant.
   integer, parameter :: a2 = ichar('B ')
   !ERROR: Character in intrinsic function ichar must have length one
-  !ERROR: Must be a constant value
+  !ERROR: Value of named constant 'a3' (ichar("")) cannot be computed as a constant value
+  !ERROR: Character in intrinsic function ichar must have length one
+  !ERROR: Value of named constant 'a3' (ichar("")) cannot be computed as a constant value
+  !ERROR: Character in intrinsic function ichar must have length one
   integer, parameter :: a3 = ichar('')
 end subroutine
diff --git a/flang/test/Semantics/intrinsics04.f90 b/flang/test/Semantics/intrinsics04.f90
index abb8fe321a572..8b6b29fed2128 100644
--- a/flang/test/Semantics/intrinsics04.f90
+++ b/flang/test/Semantics/intrinsics04.f90
@@ -29,6 +29,9 @@ subroutine ichar_tests()
   !WARNING: Character in intrinsic function ichar should have length one [-Wportability]
   integer, parameter :: a2 = ichar('B ')
   !ERROR: Character in intrinsic function ichar must have length one
-  !ERROR: Must be a constant value
+  !ERROR: Value of named constant 'a3' (ichar("")) cannot be computed as a constant value
+  !ERROR: Character in intrinsic function ichar must have length one
+  !ERROR: Value of named constant 'a3' (ichar("")) cannot be computed as a constant value
+  !ERROR: Character in intrinsic function ichar must have length one
   integer, parameter :: a3 = ichar('')
 end subroutine

>From 142e0ee846e45f2006b2d4c2ce8cef6ed3f58d77 Mon Sep 17 00:00:00 2001
From: Andre Kuhlenschmidt <akuhlenschmi at nvidia.com>
Date: Fri, 3 Oct 2025 15:37:09 -0700
Subject: [PATCH 2/3] address feedback

---
 flang/lib/Evaluate/check-expression.cpp | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/flang/lib/Evaluate/check-expression.cpp b/flang/lib/Evaluate/check-expression.cpp
index 012b44e7d3747..647eebaaa070b 100644
--- a/flang/lib/Evaluate/check-expression.cpp
+++ b/flang/lib/Evaluate/check-expression.cpp
@@ -144,10 +144,11 @@ bool IsConstantExprHelper<INVARIANT>::operator()(
                 characteristics::DummyDataObject::Attr::OnlyIntrinsicInquiry)) {
           // The value of the argument doesn't matter
         } else if (!arg) {
-          // Missing optional arguments are constant
-          if (!(dataDummy &&
-                  dataDummy->attrs.test(
-                      characteristics::DummyDataObject::Attr::Optional))) {
+          if (dataDummy &&
+              dataDummy->attrs.test(
+                  characteristics::DummyDataObject::Attr::Optional)) {
+            // Missing optional arguments are okay.
+          } else {
             return false;
           }
         } else if (const auto *expr{arg->UnwrapExpr()};

>From d853b5dec39641cb5e79e33785ad25a0e7e0c883 Mon Sep 17 00:00:00 2001
From: Andre Kuhlenschmidt <akuhlenschmi at nvidia.com>
Date: Fri, 3 Oct 2025 16:06:20 -0700
Subject: [PATCH 3/3] add test

---
 flang/test/Semantics/type-parameter-constant.f90 | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
 create mode 100644 flang/test/Semantics/type-parameter-constant.f90

diff --git a/flang/test/Semantics/type-parameter-constant.f90 b/flang/test/Semantics/type-parameter-constant.f90
new file mode 100644
index 0000000000000..376bffd0233ff
--- /dev/null
+++ b/flang/test/Semantics/type-parameter-constant.f90
@@ -0,0 +1,15 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+type A (p, r)
+  integer, kind :: p, r
+  !ERROR: KIND parameter expression (int(selected_real_kind(six,twenty_three),kind=8)) of intrinsic type REAL did not resolve to a constant value
+  real (selected_real_kind(p, r)) :: data
+end type
+   integer :: six = 6, twenty_three = 23
+   type(a(6,23)) :: a1
+   !ERROR: Value of KIND type parameter 'p' must be constant
+   !ERROR: Value of KIND type parameter 'r' must be constant
+   !WARNING: specification expression refers to local object 'six' (initialized and saved) [-Wsaved-local-in-spec-expr]
+   !WARNING: specification expression refers to local object 'twenty_three' (initialized and saved) [-Wsaved-local-in-spec-expr]
+   type(a(six, twenty_three)) :: a2 
+   print *, a1%data%kind
+end
\ No newline at end of file



More information about the flang-commits mailing list