[flang-commits] [flang] 7a48740 - [Flang] Fix statement-function shadowing to avoid false unresolved-symbol internal error (#189360)
via flang-commits
flang-commits at lists.llvm.org
Wed Apr 15 06:11:55 PDT 2026
Author: CHANDRA GHALE
Date: 2026-04-15T18:41:49+05:30
New Revision: 7a48740f3c122a9e45172f609ef22335962248e3
URL: https://github.com/llvm/llvm-project/commit/7a48740f3c122a9e45172f609ef22335962248e3
DIFF: https://github.com/llvm/llvm-project/commit/7a48740f3c122a9e45172f609ef22335962248e3.diff
LOG: [Flang] Fix statement-function shadowing to avoid false unresolved-symbol internal error (#189360)
When a statement function shadows a host-associated internal procedure
name, HandleStmtFunction creates a local symbol but leaves name.symbol
pointing to the host SubprogramNameDetails.
Because of that stale pointer, AnalyzeStmtFunctionStmt exits early (it
expects SubprogramDetails), so the RHS is never resolved and flang emits
a false `internal error: "Internal: no symbol found".` Clearing
name.symbol after creating the local shadow symbol lets it re-resolve
correctly and fixes the issue.
---------
Co-authored-by: Chandra Ghale <ghale at pe34genoa.hpc.amslabs.hpecorp.net>
Added:
flang/test/Semantics/stmt-func03.f90
Modified:
flang/lib/Semantics/resolve-names.cpp
flang/test/Semantics/stmt-func02.f90
Removed:
################################################################################
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index b6907cc792d76..a554174b7d30a 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -4687,6 +4687,10 @@ bool SubprogramVisitor::HandleStmtFunction(const parser::StmtFunctionStmt &x) {
"Name '%s' from host scope should have a type declaration before its local statement function definition"_port_en_US,
name.source);
MakeSymbol(name, Attrs{}, UnknownDetails{});
+ // 'name' may still point to a host-associated SubprogramNameDetails
+ // symbol. Reset it so statement-function processing
+ // re-resolves to the new local SubprogramDetails.
+ name.symbol = nullptr;
} else if (auto *entity{ultimate.detailsIf<EntityDetails>()};
entity && !ultimate.has<ProcEntityDetails>()) {
resultType = entity->type();
diff --git a/flang/test/Semantics/stmt-func02.f90 b/flang/test/Semantics/stmt-func02.f90
index 10166a0abf7b1..d2c408b4dbb82 100644
--- a/flang/test/Semantics/stmt-func02.f90
+++ b/flang/test/Semantics/stmt-func02.f90
@@ -25,10 +25,12 @@ subroutine test1
end
subroutine test2
!PORTABILITY: Name 'rf' from host scope should have a type declaration before its local statement function definition [-Wstatement-function-extensions]
+ !PORTABILITY: An implicitly typed statement function should not appear when the same symbol is available in its host scope [-Wstatement-function-extensions]
rf(x) = 1.
end
subroutine test2b
!PORTABILITY: Name 'rf2' from host scope should have a type declaration before its local statement function definition [-Wstatement-function-extensions]
+ !PORTABILITY: An implicitly typed statement function should not appear when the same symbol is available in its host scope [-Wstatement-function-extensions]
rf2(x) = 1.
end
subroutine test3
diff --git a/flang/test/Semantics/stmt-func03.f90 b/flang/test/Semantics/stmt-func03.f90
new file mode 100644
index 0000000000000..111a6111f720a
--- /dev/null
+++ b/flang/test/Semantics/stmt-func03.f90
@@ -0,0 +1,18 @@
+! RUN: %flang_fc1 -fsyntax-only -pedantic %s 2>&1 | FileCheck %s
+! CHECK-NOT: error:
+! CHECK-NOT: Internal:
+program main
+contains
+ subroutine internal_sub()
+ integer :: i, result
+ ! Host-associated sibling internal function name should be shadowed
+ ! by this statement function definition.
+ stmt_function(i) = i * 2
+ i = 1
+ result = stmt_function(i)
+ end subroutine internal_sub
+ integer function stmt_function(arg)
+ integer :: arg
+ stmt_function = arg * 3
+ end function stmt_function
+end program main
More information about the flang-commits
mailing list