[flang-commits] [flang] [Flang] Diagnose nonvariable STAT= specifiers (PR #219696)

via flang-commits flang-commits at lists.llvm.org
Wed Sep 16 01:00:42 PDT 2026


https://github.com/keepyixiao updated https://github.com/llvm/llvm-project/pull/219696

>From 4ab0a0a1024d3f33cbf3a829f0d8d0e207d527b5 Mon Sep 17 00:00:00 2001
From: yixiao <yixiao at hygon.cn>
Date: Sun, 30 Aug 2026 00:33:15 +0800
Subject: [PATCH] [Flang] Diagnose nonvariable STAT= specifiers

An image selector STAT= operand can be parsed as a variable but later
resolve to a nonpointer function reference. Passing this expression to
CoarrayRef::set_stat() violates its invariant and triggers a CHECK.

Verify that the analyzed STAT= expression is a variable before calling
set_stat(), and emit a semantic error when it is not.

Add a regression test to resolve94.f90 covering an implicitly typed
nonpointer function reference used as a STAT= specifier.
---
 flang/lib/Semantics/expression.cpp |  5 +++++
 flang/test/Semantics/resolve94.f90 | 13 +++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index efbf862a72c51..b2e8419ded40c 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -1620,6 +1620,11 @@ MaybeExpr ExpressionAnalyzer::Analyze(const parser::CoindexedNamedObject &x) {
                           std::get_if<Expr<SomeInteger>>(&expr->u)}) {
                     if (coarrayRef.stat()) {
                       Say("coindexed reference has multiple STAT= specifiers"_err_en_US);
+                    } else if (!IsVariable(*intExpr)) {
+                      // A parser::Variable may resolve to a nonpointer
+                      // function reference.
+                      SayAt(x.v,
+                          "STAT= specifier must be a scalar integer variable"_err_en_US);
                     } else {
                       coarrayRef.set_stat(Expr<SomeInteger>{*intExpr});
                     }
diff --git a/flang/test/Semantics/resolve94.f90 b/flang/test/Semantics/resolve94.f90
index 0c08d4cd0094f..616d91fbaa94b 100644
--- a/flang/test/Semantics/resolve94.f90
+++ b/flang/test/Semantics/resolve94.f90
@@ -11,6 +11,7 @@ subroutine s1()
   real :: rCoarray[10,20,*]
   real :: rVar1, rVar2
   integer :: iVar1, iVar2
+  integer, parameter :: kConst = 1
   integer, dimension(4) :: intArray
   integer :: intScalarCoarray[*]
   integer :: intCoarray[3, 4, *]
@@ -47,6 +48,10 @@ subroutine s1()
   rVar1 = rCoarray[1,2,3,STAT=rVar2]
   !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
+  rVar1 = rCoarray[1,2,3,STAT=MASK(2)]
+  !ERROR: STAT= specifier must be a scalar integer variable
+  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
   rVar1 = rCoarray[1,2,3,STAT=iVar1, STAT=iVar2]
@@ -78,3 +83,11 @@ subroutine s1()
   !ERROR: Image selector STAT variable must not be a coindexed object
   rVar1 = rCoarray[1,2,3,stat=intCoarray[2,3, 4]]
 end subroutine s1
+
+subroutine s()
+  real, save :: c[10,20,*]
+  real :: r
+  !ERROR: STAT= specifier must be a scalar integer variable
+  !ERROR: STAT= specifier must be a scalar integer variable
+  r = c[1,2,3, STAT=MASK(2), STAT=MASK(3)]
+end subroutine s



More information about the flang-commits mailing list