Skip to content

Commit 56c4684

Browse files
authored
[flang] Suppress USEs of non-USE'able names in module files (llvm#124980)
When harvesting and formatting symbols USE'd from other modules, don't emit USE statements to module files for names unless they come from the topmost scope of the module. There was a check to prevent names from derived type scopes from escaping in this way, but it must be made more general to prevent other cases like dummy arguments in interfaces. Fixes llvm#124716.
1 parent c82db77 commit 56c4684

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

flang/lib/Semantics/mod-file.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,10 @@ void ModFileWriter::PrepareRenamings(const Scope &scope) {
306306
// to their names in this scope, creating those new names when needed.
307307
auto &renamings{context_.moduleFileOutputRenamings()};
308308
for (SymbolRef s : symbolsNeeded) {
309-
if (s->owner().kind() == Scope::Kind::DerivedType) {
310-
continue; // component or binding: ok
309+
if (s->owner().kind() != Scope::Kind::Module) {
310+
// Not a USE'able name from a module's top scope;
311+
// component, binding, dummy argument, &c.
312+
continue;
311313
}
312314
const Scope *sMod{FindModuleContaining(s->owner())};
313315
if (!sMod || sMod == &scope) {

flang/test/Semantics/bug124716.f90

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
! RUN: %python %S/test_modfile.py %s %flang_fc1
2+
MODULE m1
3+
INTERFACE
4+
MODULE SUBROUTINE sub1(N, ARR)
5+
INTEGER, INTENT(IN) :: N
6+
INTEGER, DIMENSION(N) :: ARR
7+
END SUBROUTINE
8+
END INTERFACE
9+
END MODULE
10+
SUBMODULE (m1) m1sub
11+
CONTAINS
12+
MODULE SUBROUTINE sub1(N, ARR)
13+
INTEGER, INTENT(IN) :: N
14+
INTEGER, DIMENSION(N) :: ARR
15+
PRINT *, "sub1", N, ARR
16+
END SUBROUTINE
17+
END SUBMODULE
18+
19+
!Expect: m1.mod
20+
!module m1
21+
!interface
22+
!module subroutine sub1(n,arr)
23+
!integer(4),intent(in)::n
24+
!integer(4)::arr(1_8:int(n,kind=8))
25+
!end
26+
!end interface
27+
!end
28+
29+
!Expect: m1-m1sub.mod
30+
!submodule(m1) m1sub
31+
!contains
32+
!module subroutine sub1(n,arr)
33+
!integer(4),intent(in)::n
34+
!integer(4)::arr(1_8:int(n,kind=8))
35+
!end
36+
!end

0 commit comments

Comments
 (0)