[flang-commits] [flang] [Flang] Fix statement-function shadowing to avoid false unresolved-symbol internal error (PR #189360)
CHANDRA GHALE via flang-commits
flang-commits at lists.llvm.org
Wed Apr 15 04:49:34 PDT 2026
https://github.com/chandraghale updated https://github.com/llvm/llvm-project/pull/189360
>From 8d920eb9856542ab51950e9dafe339813eea7d50 Mon Sep 17 00:00:00 2001
From: Chandra Ghale <ghale at pe34genoa.hpc.amslabs.hpecorp.net>
Date: Mon, 30 Mar 2026 06:22:44 -0500
Subject: [PATCH 1/3] Fix statement-function shadowing to avoid false
unresolved-symbol internal error
---
flang/lib/Semantics/resolve-names.cpp | 1 +
flang/test/Semantics/stmt-func02.f90 | 2 ++
flang/test/Semantics/stmt-func03.f90 | 28 +++++++++++++++++++++++++++
3 files changed, 31 insertions(+)
create mode 100644 flang/test/Semantics/stmt-func03.f90
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 164a9bedcc393..d2c4fde6f6c1a 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -4657,6 +4657,7 @@ 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.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..c12ae5fc5439b
--- /dev/null
+++ b/flang/test/Semantics/stmt-func03.f90
@@ -0,0 +1,28 @@
+! RUN: %flang_fc1 -fsyntax-only -pedantic %s 2>&1 | FileCheck %s
+! CHECK-NOT: error:
+! CHECK-NOT: Internal:
+program main
+ integer :: passed, failed
+ passed = 0
+ failed = 0
+ call internal_sub()
+ call exit(failed)
+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)
+ if (result .eq. 2) then
+ passed = passed + 1
+ else
+ failed = failed + 1
+ end if
+ end subroutine internal_sub
+ integer function stmt_function(arg)
+ integer :: arg
+ stmt_function = arg * 3
+ end function stmt_function
+end program main
>From 91b37a488430ebf62419b1d48cf90a6682daf4c7 Mon Sep 17 00:00:00 2001
From: Chandra Ghale <ghale at pe34genoa.hpc.amslabs.hpecorp.net>
Date: Wed, 8 Apr 2026 05:26:31 -0500
Subject: [PATCH 2/3] updated the lit tst and comments
---
flang/lib/Semantics/resolve-names.cpp | 3 +++
flang/test/Semantics/stmt-func03.f90 | 10 ----------
2 files changed, 3 insertions(+), 10 deletions(-)
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index d2c4fde6f6c1a..7a078904264b7 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -4657,6 +4657,9 @@ 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>()) {
diff --git a/flang/test/Semantics/stmt-func03.f90 b/flang/test/Semantics/stmt-func03.f90
index c12ae5fc5439b..111a6111f720a 100644
--- a/flang/test/Semantics/stmt-func03.f90
+++ b/flang/test/Semantics/stmt-func03.f90
@@ -2,11 +2,6 @@
! CHECK-NOT: error:
! CHECK-NOT: Internal:
program main
- integer :: passed, failed
- passed = 0
- failed = 0
- call internal_sub()
- call exit(failed)
contains
subroutine internal_sub()
integer :: i, result
@@ -15,11 +10,6 @@ subroutine internal_sub()
stmt_function(i) = i * 2
i = 1
result = stmt_function(i)
- if (result .eq. 2) then
- passed = passed + 1
- else
- failed = failed + 1
- end if
end subroutine internal_sub
integer function stmt_function(arg)
integer :: arg
>From 7802bef5166795cf2752039e2e875b4007701a53 Mon Sep 17 00:00:00 2001
From: Chandra Ghale <ghale at pe34genoa.hpc.amslabs.hpecorp.net>
Date: Wed, 15 Apr 2026 06:48:38 -0500
Subject: [PATCH 3/3] Nit : removing extra space
---
flang/lib/Semantics/resolve-names.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 7a078904264b7..1e2241b4be984 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -4659,7 +4659,7 @@ bool SubprogramVisitor::HandleStmtFunction(const parser::StmtFunctionStmt &x) {
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.
+ // re-resolves to the new local SubprogramDetails.
name.symbol = nullptr;
} else if (auto *entity{ultimate.detailsIf<EntityDetails>()};
entity && !ultimate.has<ProcEntityDetails>()) {
More information about the flang-commits
mailing list