Skip to content

Conversation

@klausler
Copy link
Contributor

When a derived type has the same name as a generic function, and is use-associated into a scope along with other distinct derived types of the same name, we crash. Don't crash.

Fixes #168099.

When a derived type has the same name as a generic function,
and is use-associated into a scope along with other distinct
derived types of the same name, we crash.  Don't crash.

Fixes llvm#168099.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Nov 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 14, 2025

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

When a derived type has the same name as a generic function, and is use-associated into a scope along with other distinct derived types of the same name, we crash. Don't crash.

Fixes #168099.


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

2 Files Affected:

  • (modified) flang/lib/Semantics/resolve-names.cpp (+13-18)
  • (added) flang/test/Semantics/bug168099.f90 (+28)
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 82e872870c6b1..2a487a6d39d51 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -3967,22 +3967,6 @@ void ModuleVisitor::DoAddUse(SourceName location, SourceName localName,
     useProcedure = &useUltimate;
   }
 
-  // Creates a UseErrorDetails symbol in the current scope for a
-  // current UseDetails symbol, but leaves the UseDetails in the
-  // scope's name map.
-  auto CreateLocalUseError{[&]() {
-    EraseSymbol(*localSymbol);
-    CHECK(localSymbol->has<UseDetails>());
-    UseErrorDetails details{localSymbol->get<UseDetails>()};
-    details.add_occurrence(location, useSymbol);
-    Symbol *newSymbol{&MakeSymbol(localName, Attrs{}, std::move(details))};
-    // Restore *localSymbol in currScope
-    auto iter{currScope().find(localName)};
-    CHECK(iter != currScope().end() && &*iter->second == newSymbol);
-    iter->second = MutableSymbolRef{*localSymbol};
-    return newSymbol;
-  }};
-
   // When two derived types arrived, try to combine them.
   const Symbol *combinedDerivedType{nullptr};
   if (!useDerivedType) {
@@ -4008,8 +3992,19 @@ void ModuleVisitor::DoAddUse(SourceName location, SourceName localName,
       combinedDerivedType = localDerivedType;
     } else {
       // Create a local UseErrorDetails for the ambiguous derived type
-      if (localGeneric) {
-        combinedDerivedType = CreateLocalUseError();
+      if (localSymbol->has<UseDetails>() && localGeneric) {
+        // Creates a UseErrorDetails symbol in the current scope for a
+        // current UseDetails symbol, but leaves the UseDetails in the
+        // scope's name map.
+        UseErrorDetails details{localSymbol->get<UseDetails>()};
+        EraseSymbol(*localSymbol);
+        details.add_occurrence(location, useSymbol);
+        Symbol *newSymbol{&MakeSymbol(localName, Attrs{}, std::move(details))};
+        // Restore *localSymbol in currScope
+        auto iter{currScope().find(localName)};
+        CHECK(iter != currScope().end() && &*iter->second == newSymbol);
+        iter->second = MutableSymbolRef{*localSymbol};
+        combinedDerivedType = newSymbol;
       } else {
         ConvertToUseError(*localSymbol, location, useSymbol);
         localDerivedType = nullptr;
diff --git a/flang/test/Semantics/bug168099.f90 b/flang/test/Semantics/bug168099.f90
new file mode 100644
index 0000000000000..bc08933d7a1a6
--- /dev/null
+++ b/flang/test/Semantics/bug168099.f90
@@ -0,0 +1,28 @@
+!RUN: %python %S/test_errors.py %s %flang_fc1
+module m1
+  type pair
+  end type
+  interface pair
+    module procedure f
+  end interface
+ contains
+  type(pair) function f(n)
+    integer, intent(in) :: n
+    f = pair()
+  end
+end
+module m2
+  type pair
+  end type
+end
+module m3
+  type pair
+  end type
+end
+program main
+  use m1
+  use m2
+  use m3
+  !ERROR: Reference to 'pair' is ambiguous
+  type(pair) error
+end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:semantics flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[flang] fatal internal error: CHECK(localSymbol->has<UseDetails>())

3 participants