Skip to content

Commit 7f54266

Browse files
authored
[flang] Fix module file generation when generic shadows derived type (llvm#78618)
Fortran allows the name of a generic interface to be the same as the name of a derived type or specific procedure. When this happens, it causes the code in module file generation to miss the symbol of a derived type when scanning for symbols in initialization expressions that need to be imported. Fix.
1 parent a3bbe62 commit 7f54266

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

flang/lib/Semantics/mod-file.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@ static void HarvestInitializerSymbols(
183183
if (symbol->scope()) {
184184
HarvestInitializerSymbols(set, *symbol->scope());
185185
}
186+
} else if (const auto &generic{symbol->detailsIf<GenericDetails>()};
187+
generic && generic->derivedType()) {
188+
const Symbol &dtSym{*generic->derivedType()};
189+
CHECK(dtSym.has<DerivedTypeDetails>());
190+
if (dtSym.scope()) {
191+
HarvestInitializerSymbols(set, *dtSym.scope());
192+
}
186193
} else if (IsNamedConstant(*symbol) || scope.IsDerivedType()) {
187194
if (const auto *object{symbol->detailsIf<ObjectEntityDetails>()}) {
188195
if (object->init()) {

flang/test/Semantics/modfile62.f90

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
! RUN: %python %S/test_modfile.py %s %flang_fc1
2+
module m
3+
use iso_c_binding, only: c_ptr, c_null_ptr
4+
type foo
5+
type(c_ptr) :: p = c_null_ptr
6+
end type
7+
interface foo ! same name as derived type
8+
procedure f
9+
end interface
10+
contains
11+
type(foo) function f()
12+
end
13+
end
14+
15+
!Expect: m.mod
16+
!module m
17+
!use,intrinsic::__fortran_builtins,only:__builtin_c_ptr
18+
!use,intrinsic::iso_c_binding,only:c_ptr
19+
!use,intrinsic::iso_c_binding,only:c_null_ptr
20+
!private::__builtin_c_ptr
21+
!type::foo
22+
!type(c_ptr)::p=__builtin_c_ptr(__address=0_8)
23+
!end type
24+
!interface foo
25+
!procedure::f
26+
!end interface
27+
!contains
28+
!function f()
29+
!type(foo)::f
30+
!end
31+
!end

0 commit comments

Comments
 (0)