Skip to content

Commit f5f6ca6

Browse files
authored
[flang] Fix crash in UseErrorDetails construction case (#168126)
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.
1 parent d988991 commit f5f6ca6

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

flang/lib/Semantics/resolve-names.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3967,22 +3967,6 @@ void ModuleVisitor::DoAddUse(SourceName location, SourceName localName,
39673967
useProcedure = &useUltimate;
39683968
}
39693969

3970-
// Creates a UseErrorDetails symbol in the current scope for a
3971-
// current UseDetails symbol, but leaves the UseDetails in the
3972-
// scope's name map.
3973-
auto CreateLocalUseError{[&]() {
3974-
EraseSymbol(*localSymbol);
3975-
CHECK(localSymbol->has<UseDetails>());
3976-
UseErrorDetails details{localSymbol->get<UseDetails>()};
3977-
details.add_occurrence(location, useSymbol);
3978-
Symbol *newSymbol{&MakeSymbol(localName, Attrs{}, std::move(details))};
3979-
// Restore *localSymbol in currScope
3980-
auto iter{currScope().find(localName)};
3981-
CHECK(iter != currScope().end() && &*iter->second == newSymbol);
3982-
iter->second = MutableSymbolRef{*localSymbol};
3983-
return newSymbol;
3984-
}};
3985-
39863970
// When two derived types arrived, try to combine them.
39873971
const Symbol *combinedDerivedType{nullptr};
39883972
if (!useDerivedType) {
@@ -4008,8 +3992,19 @@ void ModuleVisitor::DoAddUse(SourceName location, SourceName localName,
40083992
combinedDerivedType = localDerivedType;
40093993
} else {
40103994
// Create a local UseErrorDetails for the ambiguous derived type
4011-
if (localGeneric) {
4012-
combinedDerivedType = CreateLocalUseError();
3995+
if (localSymbol->has<UseDetails>() && localGeneric) {
3996+
// Creates a UseErrorDetails symbol in the current scope for a
3997+
// current UseDetails symbol, but leaves the UseDetails in the
3998+
// scope's name map.
3999+
UseErrorDetails details{localSymbol->get<UseDetails>()};
4000+
EraseSymbol(*localSymbol);
4001+
details.add_occurrence(location, useSymbol);
4002+
Symbol *newSymbol{&MakeSymbol(localName, Attrs{}, std::move(details))};
4003+
// Restore *localSymbol in currScope
4004+
auto iter{currScope().find(localName)};
4005+
CHECK(iter != currScope().end() && &*iter->second == newSymbol);
4006+
iter->second = MutableSymbolRef{*localSymbol};
4007+
combinedDerivedType = newSymbol;
40134008
} else {
40144009
ConvertToUseError(*localSymbol, location, useSymbol);
40154010
localDerivedType = nullptr;

flang/test/Semantics/bug168099.f90

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
!RUN: %python %S/test_errors.py %s %flang_fc1
2+
module m1
3+
type pair
4+
end type
5+
interface pair
6+
module procedure f
7+
end interface
8+
contains
9+
type(pair) function f(n)
10+
integer, intent(in) :: n
11+
f = pair()
12+
end
13+
end
14+
module m2
15+
type pair
16+
end type
17+
end
18+
module m3
19+
type pair
20+
end type
21+
end
22+
program main
23+
use m1
24+
use m2
25+
use m3
26+
!ERROR: Reference to 'pair' is ambiguous
27+
type(pair) error
28+
end

0 commit comments

Comments
 (0)