[flang-commits] [flang] [Flang] Fix statement-function shadowing to avoid false unresolved-symbol internal error (PR #189360)

via flang-commits flang-commits at lists.llvm.org
Mon Mar 30 04:28:36 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: CHANDRA GHALE (chandraghale)

<details>
<summary>Changes</summary>

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.


---
Full diff: https://github.com/llvm/llvm-project/pull/189360.diff


3 Files Affected:

- (modified) flang/lib/Semantics/resolve-names.cpp (+1) 
- (modified) flang/test/Semantics/stmt-func02.f90 (+2) 
- (added) flang/test/Semantics/stmt-func03.f90 (+28) 


``````````diff
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

``````````

</details>


https://github.com/llvm/llvm-project/pull/189360


More information about the flang-commits mailing list