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

via flang-commits flang-commits at lists.llvm.org
Fri Aug 28 05:48:44 PDT 2026


Author: nudt_yixiao
Date: 2026-08-28T08:48:39-04:00
New Revision: 5cb8d1af161d2d77b5cc7255a1c11b5a8ed42e8b

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

LOG: [Flang] Handle self-referencing KIND selectors (#216668)

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.

Fixes https://github.com/llvm/llvm-project/issues/209505

Co-authored-by: yixiao <yixiao at hygon.cn>

Added: 
    

Modified: 
    flang/lib/Semantics/resolve-names.cpp
    flang/test/Semantics/resolve37.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 6a67f93bbfc70..17abba65bdae5 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -10710,6 +10710,21 @@ 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)};
+  if (const auto *name{std::get_if<parser::Name>(&procDesignator.u)}) {
+    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) {
@@ -10727,6 +10742,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/resolve37.f90 b/flang/test/Semantics/resolve37.f90
index d35174e62d64a..4d7207ebbbb16 100644
--- a/flang/test/Semantics/resolve37.f90
+++ b/flang/test/Semantics/resolve37.f90
@@ -47,3 +47,8 @@
 !ERROR: Must be a constant value
 integer, parameter :: snok(*)=[1,2]/[1,0]
 end
+
+subroutine kind_selector_name_same_as_declared_entity(a, n)
+  !ERROR: Must be a constant value
+  integer(n()) n
+end


        


More information about the flang-commits mailing list