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()); - UseErrorDetails details{localSymbol->get()}; - 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() && 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()}; + 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