Skip to content

Commit 9462ce8

Browse files
authored
[flang] Fix crash when handling benign USE conflict (#121977)
When the same name is used for distinct derived types in two modules, and at least one of those modules also defines a generic interface of the same name, name resolution crashes when both modules are USE'd into the same scope. The crash is due to some pointers into the symbol table becoming invalid when a symbol is replaced with a UseErrorDetails; set them to null. Also allow for extending a UseErrorDetails in place rather than emitting a spurious error message. Fixes #121718.
1 parent 510285c commit 9462ce8

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

flang/lib/Semantics/resolve-names.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3162,6 +3162,10 @@ ModuleVisitor::SymbolRename ModuleVisitor::AddUse(
31623162
// Convert it to a UseError with this additional location.
31633163
static bool ConvertToUseError(
31643164
Symbol &symbol, const SourceName &location, const Scope &module) {
3165+
if (auto *ued{symbol.detailsIf<UseErrorDetails>()}) {
3166+
ued->add_occurrence(location, module);
3167+
return true;
3168+
}
31653169
const auto *useDetails{symbol.detailsIf<UseDetails>()};
31663170
if (!useDetails) {
31673171
if (auto *genericDetails{symbol.detailsIf<GenericDetails>()}) {
@@ -3319,6 +3323,8 @@ void ModuleVisitor::DoAddUse(SourceName location, SourceName localName,
33193323
combinedDerivedType = CreateLocalUseError();
33203324
} else {
33213325
ConvertToUseError(*localSymbol, location, *useModuleScope_);
3326+
localDerivedType = nullptr;
3327+
localGeneric = nullptr;
33223328
combinedDerivedType = localSymbol;
33233329
}
33243330
}

flang/test/Semantics/bug121718.f90

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
! RUN: %flang_fc1 2>&1 | FileCheck %s --allow-empty
2+
! CHECK-NOT: error
3+
! Regression test simplified from LLVM bug 121718.
4+
! Ensure no crash and no spurious error message.
5+
module m1
6+
type foo
7+
integer x
8+
end type
9+
contains
10+
subroutine test
11+
print *, foo(123)
12+
end
13+
end
14+
module m2
15+
interface foo
16+
procedure f
17+
end interface
18+
type foo
19+
real x
20+
end type
21+
contains
22+
complex function f(x)
23+
complex, intent(in) :: x
24+
f = x
25+
end
26+
end
27+
program main
28+
use m1
29+
use m2
30+
call test
31+
end

0 commit comments

Comments
 (0)