[flang-commits] [flang] [Flang] Handle self-referencing KIND selectors (PR #216668)
via flang-commits
flang-commits at lists.llvm.org
Mon Aug 17 02:33:05 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-semantics
Author: nudt_yixiao (keepyixiao)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/216668.diff
2 Files Affected:
- (modified) flang/lib/Semantics/resolve-names.cpp (+25)
- (added) flang/test/Semantics/kind-selector-name-same-as-declared-entity.f90 (+6)
``````````diff
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 27c4e96d269aa..74da9ebe303fe 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -10580,6 +10580,27 @@ 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 +10618,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..68eca107f025a
--- /dev/null
+++ b/flang/test/Semantics/kind-selector-name-same-as-declared-entity.f90
@@ -0,0 +1,6 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+
+subroutine s(a, n)
+ ! ERROR: Must be a constant value
+ integer(N()) n
+end
\ No newline at end of file
``````````
</details>
https://github.com/llvm/llvm-project/pull/216668
More information about the flang-commits
mailing list