[flang-commits] [flang] [Flang] Handle self-referencing KIND selectors (PR #216668)

via flang-commits flang-commits at lists.llvm.org
Mon Aug 17 04:48:50 PDT 2026


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

>From d5936fa377be4597e18b3cd07421ddc5a04e3ef2 Mon Sep 17 00:00:00 2001
From: yixiao <yixiao at hygon.cn>
Date: Mon, 17 Aug 2026 16:28:15 +0800
Subject: [PATCH] [Flang] Handle self-referencing KIND selectors

EarlyDummyTypeDeclaration may implicitly type a name referenced by a
KIND selector when that name is also declared by the same type
declaration statement. This can lead to an inconsistent semantic
state and a compiler crash.

Return early when the KIND selector refers to an entity being declared
by the same statement.

Add a regression test for this case.
---
 flang/lib/Semantics/resolve-names.cpp         | 24 +++++++++++++++++++
 ...-selector-name-same-as-declared-entity.f90 |  7 ++++++
 2 files changed, 31 insertions(+)
 create mode 100644 flang/test/Semantics/kind-selector-name-same-as-declared-entity.f90

diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 27c4e96d269aa..7b201d9cfe70d 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -10580,6 +10580,26 @@ void ResolveNamesVisitor::PreSpecificationConstruct(
       spec.u);
 }
 
+static bool IsCallOfDeclaredEntity(
+    const parser::Call &call, const std::list<parser::EntityDecl> &entities) {
+  // Pure query: do not resolve names or modify symbols/scopes.
+  const auto &procDesignator{std::get<parser::ProcedureDesignator>(call.t)};
+
+  const auto *name{std::get_if<parser::Name>(&procDesignator.u)};
+  if (!name) {
+    return false;
+  }
+
+  for (const auto &ent : entities) {
+    const auto &objName{std::get<parser::ObjectName>(ent.t)};
+    if (name->source == objName.source) {
+      return true;
+    }
+  }
+
+  return false;
+}
+
 void ResolveNamesVisitor::EarlyDummyTypeDeclaration(
     const parser::Statement<common::Indirection<parser::TypeDeclarationStmt>>
         &stmt) {
@@ -10597,6 +10617,10 @@ void ResolveNamesVisitor::EarlyDummyTypeDeclaration(
             // an explicit declaration of 'n' would be useful.)
             return;
           }
+          if (IsCallOfDeclaredEntity(*call, entities)) {
+            // Avoid implicitly typing the entity referenced by the KIND call.
+            return;
+          }
         } else if (!parser::Unwrap<parser::KindSelector::StarSize>(*kind) &&
             !parser::Unwrap<parser::IntLiteralConstant>(*kind)) {
           return;
diff --git a/flang/test/Semantics/kind-selector-name-same-as-declared-entity.f90 b/flang/test/Semantics/kind-selector-name-same-as-declared-entity.f90
new file mode 100644
index 0000000000000..e514528a44d52
--- /dev/null
+++ b/flang/test/Semantics/kind-selector-name-same-as-declared-entity.f90
@@ -0,0 +1,7 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+
+subroutine s(a, n)
+  ! ERROR: Must be a constant value
+  integer(N()) n  
+end
+



More information about the flang-commits mailing list