[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
Thu Oct 8 01:50:40 PDT 2026


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

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


>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] [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



More information about the flang-commits mailing list