[flang-commits] [flang] [flang][Semantics] Adding check integer kind on STAT= specifier #230463 (PR #231103)

Jean-Didier PAILLEUX via flang-commits flang-commits at lists.llvm.org
Thu Oct 22 07:48:30 PDT 2026


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

>From 7d535b73bac51a8343445152c55c6c978ab2ec54 Mon Sep 17 00:00:00 2001
From: Jean-Didier PAILLEUX <jean-didier.pailleux at sipearl.com>
Date: Fri, 9 Oct 2026 18:59:58 +0200
Subject: [PATCH 1/5] [flang][Semantics] Adding -pedantic warning to check
 integer kind on STAT= specifier

---
 .../include/flang/Support/Fortran-features.h  |  2 +-
 flang/lib/Semantics/check-coarray.cpp         | 26 ++++++++++++++++++
 flang/test/Semantics/check-stat.f90           | 27 +++++++++++++++++++
 3 files changed, 54 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Semantics/check-stat.f90

diff --git a/flang/include/flang/Support/Fortran-features.h b/flang/include/flang/Support/Fortran-features.h
index 587221ba5dd6d6..f26c0e70fc8d19 100644
--- a/flang/include/flang/Support/Fortran-features.h
+++ b/flang/include/flang/Support/Fortran-features.h
@@ -63,7 +63,7 @@ ENUM_CLASS(LanguageFeature, BackslashEscapes, OldDebugLines,
     CUDAImplicitDataAttrSpelling, OpenAccDefaultNoneScalarsStrict,
     OpenACCMultipleNamesInRoutine, EnumerationType, CUDAInit,
     PreferIntrinsicModuleUseAssociation, MultipleCommonBlockInit,
-    OutOfBoundsSubscripts, ImplicitModulePrefix)
+    OutOfBoundsSubscripts, ImplicitModulePrefix, StatKind)
 
 // Portability and suspicious usage warnings
 ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
diff --git a/flang/lib/Semantics/check-coarray.cpp b/flang/lib/Semantics/check-coarray.cpp
index 6e427a8c573b82..cd6decb1b64a46 100644
--- a/flang/lib/Semantics/check-coarray.cpp
+++ b/flang/lib/Semantics/check-coarray.cpp
@@ -131,6 +131,13 @@ static void CheckTeamStat(
               sym ? sym->name().ToString() : var.GetSource().ToString())
           .Attach(std::move(*whyNot));
     }
+    if (auto type{expr->GetType()}; type &&
+        type->category() == TypeCategory::Integer && type->kind() < 2 &&
+        context.ShouldWarn(
+            common::LanguageFeature::StatKind)) { // F2023 9.7.4 p1
+      context.Say(at,
+          "The stat-variable must have an integer kind of at least 16-bit"_warn_en_US);
+    }
   }
 }
 
@@ -157,6 +164,15 @@ static void CheckSyncStat(SemanticsContext &context,
               context.Say( // C1172
                   "The stat-variable in a sync-stat-list may not be repeated"_err_en_US);
             }
+            if (const auto *expr{GetExpr(context, stat)})
+              if (auto type{expr->GetType()}; type &&
+                  type->category() == TypeCategory::Integer &&
+                  type->kind() < 2 &&
+                  context.ShouldWarn(
+                      common::LanguageFeature::StatKind)) { // F2023 9.7.4 p1
+                context.Say(
+                    "The stat-variable must have an integer kind of at least 16-bit"_warn_en_US);
+              }
             gotStat = true;
           },
           [&](const parser::MsgVariable &var) {
@@ -270,6 +286,16 @@ static void CheckEventWaitSpecList(SemanticsContext &context,
                           context.Say( // C1178
                               "A stat-variable in a event-wait-spec-list may not be repeated"_err_en_US);
                         }
+                        if (const auto *expr{GetExpr(context, stat)})
+                          if (auto type{expr->GetType()}; type &&
+                              type->category() == TypeCategory::Integer &&
+                              type->kind() < 2 &&
+                              context.ShouldWarn(common::LanguageFeature::
+                                      StatKind)) { // F2023 9.7.4
+                                                   // p1
+                            context.Say(
+                                "The stat-variable must have an integer kind of at least 16-bit"_warn_en_US);
+                          }
                         gotStat = true;
                       },
                       [&](const parser::MsgVariable &var) {
diff --git a/flang/test/Semantics/check-stat.f90 b/flang/test/Semantics/check-stat.f90
new file mode 100644
index 00000000000000..a975679635cbc3
--- /dev/null
+++ b/flang/test/Semantics/check-stat.f90
@@ -0,0 +1,27 @@
+! RUN: %flang -fsyntax-only -pedantic %s 2>&1 | FileCheck %s
+
+program stat_test
+  use iso_c_binding
+  use iso_fortran_env
+  integer :: coarray[*]
+  type(event_type) :: e[*]
+  integer(C_INT8_T) :: myint8
+  integer(C_INT16_T) :: myint16
+ 
+  ! CHECK: warning:  The stat-variable must have an integer kind of at least 16-bit
+  coarray[1, stat=myint8] = 0 ! violates F2023 9.7.4
+  
+  ! CHECK: warning: The stat-variable must have an integer kind of at least 16-bit
+  sync all(stat=myint8) ! violates F2023 9.7.4
+  
+  ! CHECK: warning: The stat-variable must have an integer kind of at least 16-bit
+  event wait(e, stat=myint8) ! violates F2023 9.7.4
+  
+  ! CHECK-NOT: warning: The stat-variable must have an integer kind of at least 16-bit
+  coarray[1, stat=myint16] = 0 ! ok
+  
+  sync all(stat=myint16) ! ok
+  
+  event wait(e, stat=myint16) ! ok
+
+end program

>From dc1b5fe99e2851e97c174eacd68936fe14e84c1a Mon Sep 17 00:00:00 2001
From: Jean-Didier PAILLEUX <jean-didier.pailleux at sipearl.com>
Date: Tue, 13 Oct 2026 14:55:52 +0200
Subject: [PATCH 2/5] Update ReleaseNotes.md

---
 flang/docs/ReleaseNotes.md | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/flang/docs/ReleaseNotes.md b/flang/docs/ReleaseNotes.md
index 8db73e956bb61b..5547e69eb17120 100644
--- a/flang/docs/ReleaseNotes.md
+++ b/flang/docs/ReleaseNotes.md
@@ -137,6 +137,12 @@ page](https://llvm.org/releases/).
   errors. Use `-fno-out-of-bounds-subscripts` to get an error again, or
   `-Wno-out-of-bounds-subscripts` to silence the warning.
 
+- Flang now provides an opt-in warning, enabled with `-Wstat-kind`, when the
+  `STAT=` variable has a decimal exponent range smaller than four (for
+  example, an 8-bit integer). Fortran 2023 (9.7.4 p1) only recommends this
+  range, so conforming code is flagged and the warning is disabled by
+  default.
+
 ## New Compiler Flags
 - Added `-fno-out-of-bounds-subscripts`, which restores the previous behavior of
   rejecting an out-of-range constant subscript with an error. See the entry above

>From 8ae0717ad845b2a6b6bb34001b3ffd9774ee84c1 Mon Sep 17 00:00:00 2001
From: Jean-Didier PAILLEUX <jean-didier.pailleux at sipearl.com>
Date: Tue, 13 Oct 2026 15:06:26 +0200
Subject: [PATCH 3/5] Update warning message + code factorization + update
 tests

---
 .../include/flang/Support/Fortran-features.h  |  4 +-
 flang/lib/Semantics/check-allocate.cpp        |  8 ++
 flang/lib/Semantics/check-coarray.cpp         | 80 ++++++-------------
 flang/lib/Semantics/check-deallocate.cpp      |  8 ++
 flang/test/Semantics/check-stat.f90           | 31 ++++---
 flang/test/Semantics/event02b.f90             | 16 ++--
 flang/test/Semantics/lockstmt03.f90           | 28 +++----
 flang/test/Semantics/notifywait03.f90         | 16 ++--
 8 files changed, 94 insertions(+), 97 deletions(-)

diff --git a/flang/include/flang/Support/Fortran-features.h b/flang/include/flang/Support/Fortran-features.h
index f26c0e70fc8d19..6d3771ea2153a5 100644
--- a/flang/include/flang/Support/Fortran-features.h
+++ b/flang/include/flang/Support/Fortran-features.h
@@ -63,7 +63,7 @@ ENUM_CLASS(LanguageFeature, BackslashEscapes, OldDebugLines,
     CUDAImplicitDataAttrSpelling, OpenAccDefaultNoneScalarsStrict,
     OpenACCMultipleNamesInRoutine, EnumerationType, CUDAInit,
     PreferIntrinsicModuleUseAssociation, MultipleCommonBlockInit,
-    OutOfBoundsSubscripts, ImplicitModulePrefix, StatKind)
+    OutOfBoundsSubscripts, ImplicitModulePrefix)
 
 // Portability and suspicious usage warnings
 ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
@@ -91,7 +91,7 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
     MisplacedIgnoreTKR, NamelistParameter, ImpureFinalInPure,
     IgnoredNoReallocateLHS, ExperimentalOption, IoImpliedDoIndexConflict,
     BOZLiteralTruncation, IntentInActualForDefaultIntent, BindCArrayDescriptor,
-    OpenMPDeprecated, OpenMPFuture)
+    OpenMPDeprecated, OpenMPFuture, StatKind)
 
 using LanguageFeatures = EnumSet<LanguageFeature, LanguageFeature_enumSize>;
 using UsageWarnings = EnumSet<UsageWarning, UsageWarning_enumSize>;
diff --git a/flang/lib/Semantics/check-allocate.cpp b/flang/lib/Semantics/check-allocate.cpp
index e797b978c4bd33..859f0dc0f16c89 100644
--- a/flang/lib/Semantics/check-allocate.cpp
+++ b/flang/lib/Semantics/check-allocate.cpp
@@ -151,6 +151,14 @@ static std::optional<AllocateCheckerInfo> CheckAllocateOptions(
                           context.Say(
                               "STAT may not be duplicated in a ALLOCATE statement"_err_en_US);
                         }
+                        if (const auto *expr{GetExpr(context, var)}) {
+                          if (auto type{expr->GetType()}; type &&
+                              type->category() == TypeCategory::Integer &&
+                              type->kind() < 2) { // F2023 9.7.4 p1
+                            context.Warn(common::UsageWarning::StatKind,
+                                "The stat-variable should have a decimal exponent range of at least four"_warn_en_US);
+                          }
+                        }
                         info.gotStat = true;
                         info.statVar = GetExpr(context, var);
                         info.statSource =
diff --git a/flang/lib/Semantics/check-coarray.cpp b/flang/lib/Semantics/check-coarray.cpp
index cd6decb1b64a46..11dda381908697 100644
--- a/flang/lib/Semantics/check-coarray.cpp
+++ b/flang/lib/Semantics/check-coarray.cpp
@@ -132,11 +132,10 @@ static void CheckTeamStat(
           .Attach(std::move(*whyNot));
     }
     if (auto type{expr->GetType()}; type &&
-        type->category() == TypeCategory::Integer && type->kind() < 2 &&
-        context.ShouldWarn(
-            common::LanguageFeature::StatKind)) { // F2023 9.7.4 p1
-      context.Say(at,
-          "The stat-variable must have an integer kind of at least 16-bit"_warn_en_US);
+        type->category() == TypeCategory::Integer &&
+        type->kind() < 2) { // F2023 9.7.4 p1
+      context.Warn(common::UsageWarning::StatKind, at,
+          "The stat-variable should have a decimal exponent range of at least four"_warn_en_US);
     }
   }
 }
@@ -155,24 +154,25 @@ static void CheckCoindexedStatOrErrmsg(SemanticsContext &context,
   Fortran::common::visit(CoindexedCheck, statOrErrmsg.u);
 }
 
-static void CheckSyncStat(SemanticsContext &context,
-    const parser::StatOrErrmsg &statOrErrmsg, bool &gotStat, bool &gotMsg) {
+static void CheckStatOrErrmsg(SemanticsContext &context,
+    const parser::StatOrErrmsg &statOrErrmsg, const std::string &listName,
+    bool &gotStat, bool &gotMsg) {
   common::visit(
       common::visitors{
           [&](const parser::StatVariable &stat) {
             if (gotStat) {
               context.Say( // C1172
-                  "The stat-variable in a sync-stat-list may not be repeated"_err_en_US);
+                  "The stat-variable in a %s may not be repeated"_err_en_US,
+                  listName);
             }
-            if (const auto *expr{GetExpr(context, stat)})
+            if (const auto *expr{GetExpr(context, stat)}) {
               if (auto type{expr->GetType()}; type &&
                   type->category() == TypeCategory::Integer &&
-                  type->kind() < 2 &&
-                  context.ShouldWarn(
-                      common::LanguageFeature::StatKind)) { // F2023 9.7.4 p1
-                context.Say(
-                    "The stat-variable must have an integer kind of at least 16-bit"_warn_en_US);
+                  type->kind() < 2) { // F2023 9.7.4 p1
+                context.Warn(common::UsageWarning::StatKind,
+                    "The stat-variable should have a decimal exponent range of at least four"_warn_en_US);
               }
+            }
             gotStat = true;
           },
           [&](const parser::MsgVariable &var) {
@@ -181,21 +181,22 @@ static void CheckSyncStat(SemanticsContext &context,
                 "ERRMSG=");
             if (gotMsg) {
               context.Say( // C1172
-                  "The errmsg-variable in a sync-stat-list may not be repeated"_err_en_US);
+                  "The errmsg-variable in a %s may not be repeated"_err_en_US,
+                  listName);
             }
             gotMsg = true;
           },
       },
       statOrErrmsg.u);
 
-  CheckCoindexedStatOrErrmsg(context, statOrErrmsg, "sync-stat-list");
+  CheckCoindexedStatOrErrmsg(context, statOrErrmsg, listName);
 }
 
 static void CheckSyncStatList(
     SemanticsContext &context, const std::list<parser::StatOrErrmsg> &list) {
   bool gotStat{false}, gotMsg{false};
   for (const parser::StatOrErrmsg &statOrErrmsg : list) {
-    CheckSyncStat(context, statOrErrmsg, gotStat, gotMsg);
+    CheckStatOrErrmsg(context, statOrErrmsg, "sync-stat-list", gotStat, gotMsg);
   }
 }
 
@@ -279,41 +280,8 @@ static void CheckEventWaitSpecList(SemanticsContext &context,
               gotUntil = true;
             },
             [&](const parser::StatOrErrmsg &statOrErrmsg) {
-              common::visit(
-                  common::visitors{
-                      [&](const parser::StatVariable &stat) {
-                        if (gotStat) {
-                          context.Say( // C1178
-                              "A stat-variable in a event-wait-spec-list may not be repeated"_err_en_US);
-                        }
-                        if (const auto *expr{GetExpr(context, stat)})
-                          if (auto type{expr->GetType()}; type &&
-                              type->category() == TypeCategory::Integer &&
-                              type->kind() < 2 &&
-                              context.ShouldWarn(common::LanguageFeature::
-                                      StatKind)) { // F2023 9.7.4
-                                                   // p1
-                            context.Say(
-                                "The stat-variable must have an integer kind of at least 16-bit"_warn_en_US);
-                          }
-                        gotStat = true;
-                      },
-                      [&](const parser::MsgVariable &var) {
-                        WarnOnDeferredLengthCharacterScalar(context,
-                            GetExpr(context, var),
-                            parser::UnwrapRef<parser::Variable>(var)
-                                .GetSource(),
-                            "ERRMSG=");
-                        if (gotMsg) {
-                          context.Say( // C1178
-                              "A errmsg-variable in a event-wait-spec-list may not be repeated"_err_en_US);
-                        }
-                        gotMsg = true;
-                      },
-                  },
-                  statOrErrmsg.u);
-              CheckCoindexedStatOrErrmsg(
-                  context, statOrErrmsg, "event-wait-spec-list");
+              CheckStatOrErrmsg(context, statOrErrmsg, "event-wait-spec-list",
+                  gotStat, gotMsg);
             },
 
         },
@@ -392,7 +360,8 @@ void CoarrayChecker::Leave(const parser::LockStmt &x) {
       std::get<std::list<parser::LockStmt::LockStat>>(x.t)) {
     if (const auto *statOrErrmsg{
             std::get_if<parser::StatOrErrmsg>(&lockStat.u)}) {
-      CheckSyncStat(context_, *statOrErrmsg, gotStat, gotMsg);
+      CheckStatOrErrmsg(
+          context_, *statOrErrmsg, "lock-stat-list", gotStat, gotMsg);
     } else {
       CHECK(std::holds_alternative<
           parser::Scalar<parser::Logical<parser::Variable>>>(lockStat.u));
@@ -428,11 +397,12 @@ void CoarrayChecker::Leave(const parser::ImageSelector &imageSelector) {
 void CoarrayChecker::Leave(const parser::FormTeamStmt &x) {
   CheckTeamType(
       context_, std::get<parser::TeamVariable>(x.t), /*mustBeVariable=*/true);
+  bool gotStat{false}, gotMsg{false};
   for (const auto &spec :
       std::get<std::list<parser::FormTeamStmt::FormTeamSpec>>(x.t)) {
     if (const auto *statOrErrmsg{std::get_if<parser::StatOrErrmsg>(&spec.u)}) {
-      CheckCoindexedStatOrErrmsg(
-          context_, *statOrErrmsg, "form-team-spec-list");
+      CheckStatOrErrmsg(
+          context_, *statOrErrmsg, "form-team-spec-list", gotStat, gotMsg);
     }
   }
 }
diff --git a/flang/lib/Semantics/check-deallocate.cpp b/flang/lib/Semantics/check-deallocate.cpp
index c93b940aa69edb..d9fea51cb11d8b 100644
--- a/flang/lib/Semantics/check-deallocate.cpp
+++ b/flang/lib/Semantics/check-deallocate.cpp
@@ -31,6 +31,14 @@ void DeallocateChecker::Leave(const parser::DeallocateStmt &deallocateStmt) {
                 context_.Say(
                     "STAT may not be duplicated in a DEALLOCATE statement"_err_en_US);
               }
+              if (const auto *expr{GetExpr(context_, var)}) {
+                if (auto type{expr->GetType()}; type &&
+                    type->category() == TypeCategory::Integer &&
+                    type->kind() < 2) { // F2023 9.7.4 p1
+                  context_.Warn(common::UsageWarning::StatKind,
+                      "The stat-variable should have a decimal exponent range of at least four"_warn_en_US);
+                }
+              }
               gotStat = true;
               statVar = GetExpr(context_, var);
               statSource = parser::Unwrap<parser::Variable>(var)->GetSource();
diff --git a/flang/test/Semantics/check-stat.f90 b/flang/test/Semantics/check-stat.f90
index a975679635cbc3..82430b73b6275f 100644
--- a/flang/test/Semantics/check-stat.f90
+++ b/flang/test/Semantics/check-stat.f90
@@ -1,27 +1,38 @@
-! RUN: %flang -fsyntax-only -pedantic %s 2>&1 | FileCheck %s
+!RUN: %python %S/test_errors.py %s %flang_fc1 -Werror -Wstat-kind
 
 program stat_test
   use iso_c_binding
   use iso_fortran_env
   integer :: coarray[*]
+  integer, allocatable :: a(:)
   type(event_type) :: e[*]
+  type(team_type) :: t
   integer(C_INT8_T) :: myint8
   integer(C_INT16_T) :: myint16
  
-  ! CHECK: warning:  The stat-variable must have an integer kind of at least 16-bit
-  coarray[1, stat=myint8] = 0 ! violates F2023 9.7.4
+  !WARNING: The stat-variable should have a decimal exponent range of at least four [-Wstat-kind]
+  coarray[1, stat=myint8] = 0
   
-  ! CHECK: warning: The stat-variable must have an integer kind of at least 16-bit
-  sync all(stat=myint8) ! violates F2023 9.7.4
+  !WARNING: The stat-variable should have a decimal exponent range of at least four [-Wstat-kind]
+  sync all(stat=myint8)
   
-  ! CHECK: warning: The stat-variable must have an integer kind of at least 16-bit
-  event wait(e, stat=myint8) ! violates F2023 9.7.4
+  !WARNING: The stat-variable should have a decimal exponent range of at least four [-Wstat-kind]
+  event wait(e, stat=myint8)
   
-  ! CHECK-NOT: warning: The stat-variable must have an integer kind of at least 16-bit
-  coarray[1, stat=myint16] = 0 ! ok
+  !WARNING: The stat-variable should have a decimal exponent range of at least four [-Wstat-kind]
+  allocate(a(3), stat=myint8)
   
-  sync all(stat=myint16) ! ok
+  !WARNING: The stat-variable should have a decimal exponent range of at least four [-Wstat-kind]
+  deallocate(a, stat=myint8)
+  
+  !WARNING: The stat-variable should have a decimal exponent range of at least four [-Wstat-kind]
+  form team(1, t, stat=myint8)
   
+  coarray[1, stat=myint16] = 0 ! ok
+  sync all(stat=myint16) ! ok
   event wait(e, stat=myint16) ! ok
+  allocate(a(3), stat=myint16) ! ok
+  deallocate(a, stat=myint16) ! ok
+  form team(1, t, stat=myint16) ! ok
 
 end program
diff --git a/flang/test/Semantics/event02b.f90 b/flang/test/Semantics/event02b.f90
index 0cf8c70b78415a..204bb0a2940364 100644
--- a/flang/test/Semantics/event02b.f90
+++ b/flang/test/Semantics/event02b.f90
@@ -86,28 +86,28 @@ program test_event_wait
   !ERROR: Until-spec in a event-wait-spec-list may not be repeated
   event wait(concert, until_count=threshold, stat=sync_status, errmsg=error_message, until_count=indexed(1))
 
-  !ERROR: A stat-variable in a event-wait-spec-list may not be repeated
+  !ERROR: The stat-variable in a event-wait-spec-list may not be repeated
   event wait(concert, stat=sync_status, stat=superfluous_stat)
 
-  !ERROR: A stat-variable in a event-wait-spec-list may not be repeated
+  !ERROR: The stat-variable in a event-wait-spec-list may not be repeated
   event wait(concert, stat=sync_status, until_count=threshold, stat=superfluous_stat)
 
-  !ERROR: A stat-variable in a event-wait-spec-list may not be repeated
+  !ERROR: The stat-variable in a event-wait-spec-list may not be repeated
   event wait(concert, stat=sync_status, errmsg=error_message, stat=superfluous_stat)
 
-  !ERROR: A stat-variable in a event-wait-spec-list may not be repeated
+  !ERROR: The stat-variable in a event-wait-spec-list may not be repeated
   event wait(concert, stat=sync_status, until_count=threshold, errmsg=error_message, stat=superfluous_stat)
 
-  !ERROR: A errmsg-variable in a event-wait-spec-list may not be repeated
+  !ERROR: The errmsg-variable in a event-wait-spec-list may not be repeated
   event wait(concert, errmsg=error_message, errmsg=superfluous_errmsg)
 
-  !ERROR: A errmsg-variable in a event-wait-spec-list may not be repeated
+  !ERROR: The errmsg-variable in a event-wait-spec-list may not be repeated
   event wait(concert, errmsg=error_message, until_count=threshold, errmsg=superfluous_errmsg)
 
-  !ERROR: A errmsg-variable in a event-wait-spec-list may not be repeated
+  !ERROR: The errmsg-variable in a event-wait-spec-list may not be repeated
   event wait(concert, errmsg=error_message, stat=superfluous_stat, errmsg=superfluous_errmsg)
 
-  !ERROR: A errmsg-variable in a event-wait-spec-list may not be repeated
+  !ERROR: The errmsg-variable in a event-wait-spec-list may not be repeated
   event wait(concert, errmsg=error_message, until_count=threshold, stat=superfluous_stat, errmsg=superfluous_errmsg)
 
   !_____________ invalid sync-stat-lists: coindexed stat-variable - C1173 __________________
diff --git a/flang/test/Semantics/lockstmt03.f90 b/flang/test/Semantics/lockstmt03.f90
index 8ebe3de62d7eea..85a2c87ea0ade3 100644
--- a/flang/test/Semantics/lockstmt03.f90
+++ b/flang/test/Semantics/lockstmt03.f90
@@ -50,14 +50,14 @@ program test_lock_stmt
 
 ! C1173 - stat-variable and errmsg-variable shall not be a coindexed object
 
-  !ERROR: The stat-variable or errmsg-variable in a sync-stat-list may not be a coindexed object
+  !ERROR: The stat-variable or errmsg-variable in a lock-stat-list may not be a coindexed object
   lock(lock_var, stat=coindexed_int[1])
 
-  !ERROR: The stat-variable or errmsg-variable in a sync-stat-list may not be a coindexed object
+  !ERROR: The stat-variable or errmsg-variable in a lock-stat-list may not be a coindexed object
   lock(lock_var, errmsg=coindexed_msg[1])
 
-  !ERROR: The stat-variable or errmsg-variable in a sync-stat-list may not be a coindexed object
-  !ERROR: The stat-variable or errmsg-variable in a sync-stat-list may not be a coindexed object
+  !ERROR: The stat-variable or errmsg-variable in a lock-stat-list may not be a coindexed object
+  !ERROR: The stat-variable or errmsg-variable in a lock-stat-list may not be a coindexed object
   lock(lock_var, acquired_lock=coindexed_logical[1], stat=coindexed_int[1], errmsg=coindexed_msg[1])
 
 ! C1181 - No specifier shall appear more than once in a given lock-stat-list
@@ -65,35 +65,35 @@ program test_lock_stmt
   !ERROR: Multiple ACQUIRED_LOCK specifiers
   lock(lock_var, acquired_lock=bool, acquired_lock=repeated_bool)
 
-  !ERROR: The stat-variable in a sync-stat-list may not be repeated
+  !ERROR: The stat-variable in a lock-stat-list may not be repeated
   lock(lock_var, stat=status, stat=repeated_stat)
 
-  !ERROR: The errmsg-variable in a sync-stat-list may not be repeated
+  !ERROR: The errmsg-variable in a lock-stat-list may not be repeated
   lock(lock_var, errmsg=error_message, errmsg=repeated_msg)
 
   !ERROR: Multiple ACQUIRED_LOCK specifiers
   lock(lock_var, acquired_lock=bool, stat=status, errmsg=error_message, acquired_lock=repeated_bool)
 
-  !ERROR: The stat-variable in a sync-stat-list may not be repeated
+  !ERROR: The stat-variable in a lock-stat-list may not be repeated
   lock(lock_var, acquired_lock=bool, stat=status, errmsg=error_message, stat=repeated_stat)
 
-  !ERROR: The errmsg-variable in a sync-stat-list may not be repeated
+  !ERROR: The errmsg-variable in a lock-stat-list may not be repeated
   lock(lock_var, acquired_lock=bool, stat=status, errmsg=error_message, errmsg=repeated_msg)
 
-  !ERROR: The stat-variable in a sync-stat-list may not be repeated
+  !ERROR: The stat-variable in a lock-stat-list may not be repeated
   !ERROR: Multiple ACQUIRED_LOCK specifiers
   lock(lock_var, acquired_lock=bool, stat=status, errmsg=error_message, acquired_lock=repeated_bool, stat=repeated_stat)
 
-  !ERROR: The errmsg-variable in a sync-stat-list may not be repeated
+  !ERROR: The errmsg-variable in a lock-stat-list may not be repeated
   !ERROR: Multiple ACQUIRED_LOCK specifiers
   lock(lock_var, acquired_lock=bool, stat=status, errmsg=error_message, acquired_lock=repeated_bool, errmsg=repeated_msg)
 
-  !ERROR: The stat-variable in a sync-stat-list may not be repeated
-  !ERROR: The errmsg-variable in a sync-stat-list may not be repeated
+  !ERROR: The stat-variable in a lock-stat-list may not be repeated
+  !ERROR: The errmsg-variable in a lock-stat-list may not be repeated
   lock(lock_var, acquired_lock=bool, stat=status, errmsg=error_message, stat=repeated_stat, errmsg=repeated_msg)
 
-  !ERROR: The stat-variable in a sync-stat-list may not be repeated
-  !ERROR: The errmsg-variable in a sync-stat-list may not be repeated
+  !ERROR: The stat-variable in a lock-stat-list may not be repeated
+  !ERROR: The errmsg-variable in a lock-stat-list may not be repeated
   !ERROR: Multiple ACQUIRED_LOCK specifiers
   lock(lock_var, acquired_lock=bool, stat=status, errmsg=error_message, acquired_lock=repeated_bool, stat=repeated_stat, errmsg=repeated_msg)
 
diff --git a/flang/test/Semantics/notifywait03.f90 b/flang/test/Semantics/notifywait03.f90
index a336a7a67669a7..9f90e26cfd5f65 100644
--- a/flang/test/Semantics/notifywait03.f90
+++ b/flang/test/Semantics/notifywait03.f90
@@ -75,28 +75,28 @@ program test_notify_wait
   !ERROR: Until-spec in a event-wait-spec-list may not be repeated
   notify wait(notify_var, until_count=count, stat=sync_status, errmsg=error_message, until_count=count_array(1))
 
-  !ERROR: A stat-variable in a event-wait-spec-list may not be repeated
+  !ERROR: The stat-variable in a event-wait-spec-list may not be repeated
   notify wait(notify_var, stat=sync_status, stat=superfluous_stat)
 
-  !ERROR: A stat-variable in a event-wait-spec-list may not be repeated
+  !ERROR: The stat-variable in a event-wait-spec-list may not be repeated
   notify wait(notify_var, stat=sync_status, until_count=count, stat=superfluous_stat)
 
-  !ERROR: A stat-variable in a event-wait-spec-list may not be repeated
+  !ERROR: The stat-variable in a event-wait-spec-list may not be repeated
   notify wait(notify_var, stat=sync_status, errmsg=error_message, stat=superfluous_stat)
 
-  !ERROR: A stat-variable in a event-wait-spec-list may not be repeated
+  !ERROR: The stat-variable in a event-wait-spec-list may not be repeated
   notify wait(notify_var, stat=sync_status, until_count=count, errmsg=error_message, stat=superfluous_stat)
 
-  !ERROR: A errmsg-variable in a event-wait-spec-list may not be repeated
+  !ERROR: The errmsg-variable in a event-wait-spec-list may not be repeated
   notify wait(notify_var, errmsg=error_message, errmsg=superfluous_errmsg)
 
-  !ERROR: A errmsg-variable in a event-wait-spec-list may not be repeated
+  !ERROR: The errmsg-variable in a event-wait-spec-list may not be repeated
   notify wait(notify_var, errmsg=error_message, until_count=count, errmsg=superfluous_errmsg)
 
-  !ERROR: A errmsg-variable in a event-wait-spec-list may not be repeated
+  !ERROR: The errmsg-variable in a event-wait-spec-list may not be repeated
   notify wait(notify_var, errmsg=error_message, stat=superfluous_stat, errmsg=superfluous_errmsg)
 
-  !ERROR: A errmsg-variable in a event-wait-spec-list may not be repeated
+  !ERROR: The errmsg-variable in a event-wait-spec-list may not be repeated
   notify wait(notify_var, errmsg=error_message, until_count=count, stat=superfluous_stat, errmsg=superfluous_errmsg)
 
   !_____________ invalid sync-stat-lists: coindexed stat-variable - C1173 __________________

>From 60f6545bf9749aee272ae4be93d3e427db256a08 Mon Sep 17 00:00:00 2001
From: Jean-Didier PAILLEUX <jean-didier.pailleux at sipearl.com>
Date: Tue, 13 Oct 2026 15:29:23 +0200
Subject: [PATCH 4/5] Remove useless var

---
 flang/lib/Semantics/check-coarray.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/flang/lib/Semantics/check-coarray.cpp b/flang/lib/Semantics/check-coarray.cpp
index 11dda381908697..7d1081f062ca2f 100644
--- a/flang/lib/Semantics/check-coarray.cpp
+++ b/flang/lib/Semantics/check-coarray.cpp
@@ -115,7 +115,6 @@ 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(at, // C931
         "Image selector STAT variable must not be a coindexed "

>From 8afcac3c882e0daf3a9360aae9b7dbc19e1809e7 Mon Sep 17 00:00:00 2001
From: Jean-Didier PAILLEUX <jean-didier.pailleux at sipearl.com>
Date: Thu, 22 Oct 2026 15:44:40 +0200
Subject: [PATCH 5/5] Adding missing location in context.Warn + missing tests

---
 flang/lib/Semantics/check-coarray.cpp    |  1 +
 flang/lib/Semantics/check-deallocate.cpp |  4 ++--
 flang/test/Semantics/check-stat.f90      | 10 ++++++++--
 flang/test/Semantics/form_team01.f90     | 10 ++++++----
 4 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/flang/lib/Semantics/check-coarray.cpp b/flang/lib/Semantics/check-coarray.cpp
index 7d1081f062ca2f..c43f352ac162d5 100644
--- a/flang/lib/Semantics/check-coarray.cpp
+++ b/flang/lib/Semantics/check-coarray.cpp
@@ -169,6 +169,7 @@ static void CheckStatOrErrmsg(SemanticsContext &context,
                   type->category() == TypeCategory::Integer &&
                   type->kind() < 2) { // F2023 9.7.4 p1
                 context.Warn(common::UsageWarning::StatKind,
+                    parser::UnwrapRef<parser::Variable>(stat).GetSource(),
                     "The stat-variable should have a decimal exponent range of at least four"_warn_en_US);
               }
             }
diff --git a/flang/lib/Semantics/check-deallocate.cpp b/flang/lib/Semantics/check-deallocate.cpp
index d9fea51cb11d8b..8320ea54bd223d 100644
--- a/flang/lib/Semantics/check-deallocate.cpp
+++ b/flang/lib/Semantics/check-deallocate.cpp
@@ -31,17 +31,17 @@ void DeallocateChecker::Leave(const parser::DeallocateStmt &deallocateStmt) {
                 context_.Say(
                     "STAT may not be duplicated in a DEALLOCATE statement"_err_en_US);
               }
+              statSource = parser::Unwrap<parser::Variable>(var)->GetSource();
               if (const auto *expr{GetExpr(context_, var)}) {
                 if (auto type{expr->GetType()}; type &&
                     type->category() == TypeCategory::Integer &&
                     type->kind() < 2) { // F2023 9.7.4 p1
-                  context_.Warn(common::UsageWarning::StatKind,
+                  context_.Warn(common::UsageWarning::StatKind, *statSource,
                       "The stat-variable should have a decimal exponent range of at least four"_warn_en_US);
                 }
               }
               gotStat = true;
               statVar = GetExpr(context_, var);
-              statSource = parser::Unwrap<parser::Variable>(var)->GetSource();
             },
             [&](const parser::MsgVariable &var) {
               WarnOnDeferredLengthCharacterScalar(context_,
diff --git a/flang/test/Semantics/check-stat.f90 b/flang/test/Semantics/check-stat.f90
index 82430b73b6275f..5e784f462dd294 100644
--- a/flang/test/Semantics/check-stat.f90
+++ b/flang/test/Semantics/check-stat.f90
@@ -1,9 +1,9 @@
-!RUN: %python %S/test_errors.py %s %flang_fc1 -Werror -Wstat-kind
+!RUN: %python %S/test_errors.py %s %flang_fc1 -fopenmp -Werror -Wstat-kind
 
 program stat_test
   use iso_c_binding
   use iso_fortran_env
-  integer :: coarray[*]
+  integer :: i, coarray[*]
   integer, allocatable :: a(:)
   type(event_type) :: e[*]
   type(team_type) :: t
@@ -27,6 +27,12 @@ program stat_test
   
   !WARNING: The stat-variable should have a decimal exponent range of at least four [-Wstat-kind]
   form team(1, t, stat=myint8)
+
+  !WARNING: The stat-variable should have a decimal exponent range of at least four [-Wstat-kind]
+  !$omp parallel do if(coarray[1, stat=myint8] > 0)
+  do i = 1, 2
+  end do
+  !$omp end parallel do
   
   coarray[1, stat=myint16] = 0 ! ok
   sync all(stat=myint16) ! ok
diff --git a/flang/test/Semantics/form_team01.f90 b/flang/test/Semantics/form_team01.f90
index 1510a8bb98f74b..3184b9c21e5561 100644
--- a/flang/test/Semantics/form_team01.f90
+++ b/flang/test/Semantics/form_team01.f90
@@ -6,8 +6,8 @@ subroutine test
   type(team_type) :: team
   integer :: team_number
   integer :: team_index
-  integer :: statvar
-  character(len=50) :: errvar
+  integer :: statvar, statvar2
+  character(len=50) :: errvar, errvar2
   integer, codimension[*], save :: co_team_number, co_team_index
   type(team_type), dimension(1) :: array_team
   integer, dimension(1) :: array_team_number
@@ -61,6 +61,8 @@ subroutine test
   FORM TEAM (team_number, team, NEW_INDEX=array_team_index)
   !ERROR: Must be a scalar value, but is a rank-1 array
   FORM TEAM (team_number, team, STAT=array_statvar)
-  !ERROR: Must be a scalar value, but is a rank-1 array
-  FORM TEAM (team_number, team, ERRMSG=array_errvar)
+  !ERROR: The errmsg-variable in a form-team-spec-list may not be repeated
+  FORM TEAM (team_number, team, ERRMSG=errvar, ERRMSG=errvar2)
+  !ERROR: The stat-variable in a form-team-spec-list may not be repeated
+  FORM TEAM (team_number, team, STAT=statvar, STAT=statvar2)
 end subroutine



More information about the flang-commits mailing list