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

via flang-commits flang-commits at lists.llvm.org
Fri Oct 16 00:20:13 PDT 2026


Author: Jean-Didier PAILLEUX
Date: 2026-10-16T09:20:05+02:00
New Revision: cca9d7b47fe49330cf78402106ddc67a0681a084

URL: https://github.com/llvm/llvm-project/commit/cca9d7b47fe49330cf78402106ddc67a0681a084
DIFF: https://github.com/llvm/llvm-project/commit/cca9d7b47fe49330cf78402106ddc67a0681a084.diff

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

This MR fixes #224058, where Flang accepts a non-definable
`stat-variable` in an image selector without any diagnostic..

Added: 
    flang/test/Semantics/definable08.f90
    flang/test/Semantics/stat-defined01.f90

Modified: 
    flang/lib/Semantics/check-coarray.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/check-coarray.cpp b/flang/lib/Semantics/check-coarray.cpp
index 00b97162ff0d4..6e427a8c573b8 100644
--- a/flang/lib/Semantics/check-coarray.cpp
+++ b/flang/lib/Semantics/check-coarray.cpp
@@ -115,10 +115,22 @@ 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
+    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)}) {
+    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,
+              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
new file mode 100644
index 0000000000000..367092d442a4d
--- /dev/null
+++ b/flang/test/Semantics/definable08.f90
@@ -0,0 +1,52 @@
+!RUN: %python %S/test_errors.py %s %flang_fc1
+
+subroutine s1(x)
+  integer, intent(in) :: x
+  real, save :: c[*]
+  real :: r
+  !ERROR: STAT variable 'x' is not definable
+  !BECAUSE: 'x' is an INTENT(IN) dummy argument
+  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
+  !ERROR: STAT variable 'pv' is not definable
+  !BECAUSE: 'pv' is protected in this scope
+  rVar1 = rCoarray[1,2,3,STAT=pv]
+end
+
+subroutine s3()
+  real, save :: c[10,20,*]
+  real :: r
+  integer :: i
+  associate (z => i + 1)
+    !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
+
+subroutine s4(a)
+  integer, intent(in) :: a(2)
+  real, save :: c[*]
+  real :: r
+  !ERROR: STAT variable 'a' is not definable
+  !BECAUSE: 'a' is an INTENT(IN) dummy argument
+  r = c[1, stat=a(1)]
+end subroutine
+
+subroutine s5
+  real, save :: c[*]
+  real :: r
+  integer :: i
+  do i = 1, 2
+    !ERROR: Cannot redefine DO variable 'i'
+    r = c[1, stat=i]
+  end do
+end subroutine

diff  --git a/flang/test/Semantics/stat-defined01.f90 b/flang/test/Semantics/stat-defined01.f90
new file mode 100644
index 0000000000000..fb70fdef36aba
--- /dev/null
+++ b/flang/test/Semantics/stat-defined01.f90
@@ -0,0 +1,15 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1 -Werror -Wused-undefined-variable
+
+integer function f()
+  real, save :: c[*] = 0.
+  real :: r
+  r = c[1, stat=f]
+end function
+
+subroutine s
+  real, save :: c[*] = 0.
+  real :: r
+  integer :: st
+  r = c[1, stat=st]
+  print *, r, st
+end subroutine


        


More information about the flang-commits mailing list