[flang-commits] [flang] [flang] Add check for definability for Image-selector STAT= specifier #224058 (PR #230031)

Jean-Didier PAILLEUX via flang-commits flang-commits at lists.llvm.org
Mon Oct 12 00:10:26 PDT 2026


https://github.com/JDPailleux updated https://github.com/llvm/llvm-project/pull/230031

>From ba1bfa66845edf395a3385023e24f5f279d72767 Mon Sep 17 00:00:00 2001
From: Jean-Didier Pailleux <jean-didier.pailleux at sipearl.com>
Date: Thu, 8 Oct 2026 09:05:50 +0200
Subject: [PATCH 1/2] [flang] Add check for definability for Image-selector
 STAT= specifier #224058

---
 flang/lib/Semantics/check-coarray.cpp | 10 +++++++++
 flang/test/Semantics/definable08.f90  | 30 +++++++++++++++++++++++++++
 flang/test/Semantics/resolve94.f90    |  8 +++++++
 3 files changed, 48 insertions(+)
 create mode 100644 flang/test/Semantics/definable08.f90

diff --git a/flang/lib/Semantics/check-coarray.cpp b/flang/lib/Semantics/check-coarray.cpp
index 00b97162ff0d4..183a3d66206c5 100644
--- a/flang/lib/Semantics/check-coarray.cpp
+++ b/flang/lib/Semantics/check-coarray.cpp
@@ -115,10 +115,20 @@ static void CheckTeamType(
 static void CheckTeamStat(
     SemanticsContext &context, const parser::ImageSelectorSpec::Stat &stat) {
   const auto &var{parser::UnwrapRef<parser::Variable>(stat)};
+  auto at{parser::FindSourceLocation(var)};
   if (parser::GetCoindexedNamedObject(var)) {
     context.Say(parser::FindSourceLocation(var), // C931
         "Image selector STAT variable must not be a coindexed "
         "object"_err_en_US);
+  } else if (const auto *expr{GetExpr(context, var)}) {
+    if (auto whyNot{WhyNotDefinable(at, context.FindScope(at),
+            {DefinabilityFlag::DoNotNoteDefinition}, *expr)}) {
+      whyNot->set_severity(parser::Severity::Because);
+      context
+          .Say(at, "STAT variable '%s' is not definable"_err_en_US,
+              var.GetSource().ToString())
+          .Attach(std::move(*whyNot));
+    }
   }
 }
 
diff --git a/flang/test/Semantics/definable08.f90 b/flang/test/Semantics/definable08.f90
new file mode 100644
index 0000000000000..0cdf0327bdedb
--- /dev/null
+++ b/flang/test/Semantics/definable08.f90
@@ -0,0 +1,30 @@
+!RUN: not %flang_fc1 -fsyntax-only -pedantic %s 2>&1 | FileCheck %s
+
+subroutine s1(k)
+  integer, intent(in) :: x 
+  real, save :: c[*]
+  real :: r
+! CHECK: error: STAT variable 'x' is not definable
+  r = c[1, STAT=x]
+end subroutine
+
+module m
+  integer, protected :: pv = 0
+end module
+subroutine s2
+  use m
+  real, save :: rCoarray[1,2,*]
+  real :: rVar1
+! CHECK: error: STAT variable 'pv' is not definable
+  rVar1 = rCoarray[1,2,3,STAT=pv]
+end
+
+subroutine s3()
+  real, save :: c[10,20,*]
+  real :: r
+  integer :: i
+  associate (z => i + 1)
+! CHECK: error: STAT variable 'z' is not definable
+    r = c[1,2,3, STAT=z]
+  end associate
+end subroutine
diff --git a/flang/test/Semantics/resolve94.f90 b/flang/test/Semantics/resolve94.f90
index 616d91fbaa94b..bd8c40eacb079 100644
--- a/flang/test/Semantics/resolve94.f90
+++ b/flang/test/Semantics/resolve94.f90
@@ -49,8 +49,12 @@ subroutine s1()
   !ERROR: Must be a scalar value, but is a rank-1 array
   rVar1 = rCoarray[1,2,3,STAT=intArray]
   !ERROR: STAT= specifier must be a scalar integer variable
+  !ERROR: STAT variable 'mask(2)' is not definable
+  !BECAUSE: 'mask(2_4)' is not a variable or pointer
   rVar1 = rCoarray[1,2,3,STAT=MASK(2)]
   !ERROR: STAT= specifier must be a scalar integer variable
+  !ERROR: STAT variable 'kconst' is not definable
+  !BECAUSE: '1_4' is not a variable or pointer
   rVar1 = rCoarray[1,2,3,STAT=kConst] ! named constant: F'2023 C901
   ! Error on C929, no specifier can appear more than once
   !ERROR: coindexed reference has multiple STAT= specifiers
@@ -89,5 +93,9 @@ subroutine s()
   real :: r
   !ERROR: STAT= specifier must be a scalar integer variable
   !ERROR: STAT= specifier must be a scalar integer variable
+  !ERROR: STAT variable 'mask(2)' is not definable
+  !BECAUSE: 'mask(2_4)' is not a variable or pointer
+  !ERROR: STAT variable 'mask(3)' is not definable
+  !BECAUSE: 'mask(3_4)' is not a variable or pointer
   r = c[1,2,3, STAT=MASK(2), STAT=MASK(3)]
 end subroutine s

>From fee78915375be0ddc380764dc4b35c807c8124a2 Mon Sep 17 00:00:00 2001
From: Jean-Didier Pailleux <jean-didier.pailleux at sipearl.com>
Date: Mon, 12 Oct 2026 09:09:58 +0200
Subject: [PATCH 2/2] Adding checks IsVariable and CheckIndexVarRedefine +
 updating tests

---
 flang/lib/Semantics/check-coarray.cpp | 10 ++++++----
 flang/test/Semantics/definable08.f90  | 13 ++++++++-----
 flang/test/Semantics/resolve94.f90    |  8 --------
 3 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/flang/lib/Semantics/check-coarray.cpp b/flang/lib/Semantics/check-coarray.cpp
index 183a3d66206c5..6e427a8c573b8 100644
--- a/flang/lib/Semantics/check-coarray.cpp
+++ b/flang/lib/Semantics/check-coarray.cpp
@@ -117,16 +117,18 @@ static void CheckTeamStat(
   const auto &var{parser::UnwrapRef<parser::Variable>(stat)};
   auto at{parser::FindSourceLocation(var)};
   if (parser::GetCoindexedNamedObject(var)) {
-    context.Say(parser::FindSourceLocation(var), // C931
+    context.Say(at, // C931
         "Image selector STAT variable must not be a coindexed "
         "object"_err_en_US);
   } else if (const auto *expr{GetExpr(context, var)}) {
-    if (auto whyNot{WhyNotDefinable(at, context.FindScope(at),
-            {DefinabilityFlag::DoNotNoteDefinition}, *expr)}) {
+    context.CheckIndexVarRedefine(var);
+    auto whyNot{WhyNotDefinable(at, context.FindScope(at), {}, *expr)};
+    if (whyNot && IsVariable(*expr)) {
       whyNot->set_severity(parser::Severity::Because);
+      const Symbol *sym{evaluate::GetFirstSymbol(*expr)};
       context
           .Say(at, "STAT variable '%s' is not definable"_err_en_US,
-              var.GetSource().ToString())
+              sym ? sym->name().ToString() : var.GetSource().ToString())
           .Attach(std::move(*whyNot));
     }
   }
diff --git a/flang/test/Semantics/definable08.f90 b/flang/test/Semantics/definable08.f90
index 0cdf0327bdedb..5cd557aeda411 100644
--- a/flang/test/Semantics/definable08.f90
+++ b/flang/test/Semantics/definable08.f90
@@ -1,10 +1,11 @@
-!RUN: not %flang_fc1 -fsyntax-only -pedantic %s 2>&1 | FileCheck %s
+! RUN: %python %S/test_errors.py %s %flang_fc1
 
-subroutine s1(k)
+subroutine s1(x)
   integer, intent(in) :: x 
   real, save :: c[*]
   real :: r
-! CHECK: error: STAT variable 'x' is not definable
+  ! ERROR: STAT variable 'x' is not definable
+  ! BECAUSE: 'x' is an INTENT(IN) dummy argument
   r = c[1, STAT=x]
 end subroutine
 
@@ -15,7 +16,8 @@ subroutine s2
   use m
   real, save :: rCoarray[1,2,*]
   real :: rVar1
-! CHECK: error: STAT variable 'pv' is not definable
+  ! ERROR: STAT variable 'pv' is not definable
+  ! BECAUSE: 'pv' is protected in this scope
   rVar1 = rCoarray[1,2,3,STAT=pv]
 end
 
@@ -24,7 +26,8 @@ subroutine s3()
   real :: r
   integer :: i
   associate (z => i + 1)
-! CHECK: error: STAT variable 'z' is not definable
+    ! ERROR: STAT variable 'z' is not definable
+    ! BECAUSE: 'z' is construct associated with an expression
     r = c[1,2,3, STAT=z]
   end associate
 end subroutine
diff --git a/flang/test/Semantics/resolve94.f90 b/flang/test/Semantics/resolve94.f90
index bd8c40eacb079..616d91fbaa94b 100644
--- a/flang/test/Semantics/resolve94.f90
+++ b/flang/test/Semantics/resolve94.f90
@@ -49,12 +49,8 @@ subroutine s1()
   !ERROR: Must be a scalar value, but is a rank-1 array
   rVar1 = rCoarray[1,2,3,STAT=intArray]
   !ERROR: STAT= specifier must be a scalar integer variable
-  !ERROR: STAT variable 'mask(2)' is not definable
-  !BECAUSE: 'mask(2_4)' is not a variable or pointer
   rVar1 = rCoarray[1,2,3,STAT=MASK(2)]
   !ERROR: STAT= specifier must be a scalar integer variable
-  !ERROR: STAT variable 'kconst' is not definable
-  !BECAUSE: '1_4' is not a variable or pointer
   rVar1 = rCoarray[1,2,3,STAT=kConst] ! named constant: F'2023 C901
   ! Error on C929, no specifier can appear more than once
   !ERROR: coindexed reference has multiple STAT= specifiers
@@ -93,9 +89,5 @@ subroutine s()
   real :: r
   !ERROR: STAT= specifier must be a scalar integer variable
   !ERROR: STAT= specifier must be a scalar integer variable
-  !ERROR: STAT variable 'mask(2)' is not definable
-  !BECAUSE: 'mask(2_4)' is not a variable or pointer
-  !ERROR: STAT variable 'mask(3)' is not definable
-  !BECAUSE: 'mask(3_4)' is not a variable or pointer
   r = c[1,2,3, STAT=MASK(2), STAT=MASK(3)]
 end subroutine s



More information about the flang-commits mailing list