Skip to content

Commit c82db77

Browse files
authored
[flang] Handle indirect USE of ancestor module into submodule (llvm#124969)
A USE statement within a submodule (possibly in a nested scope) is not allowed to USE the submodule's ancestor module directly, but it is permissible to USE that ancestor module indirectly via another unrelated module. Don't emit "already present in scope" errors for this case. Fixes llvm#124731.
1 parent cadc70c commit c82db77

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

flang/lib/Semantics/resolve-names.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3354,6 +3354,15 @@ void ModuleVisitor::DoAddUse(SourceName location, SourceName localName,
33543354
// use-associating the same symbol again -- ok
33553355
return;
33563356
}
3357+
if (useUltimate.owner().IsModule() && localUltimate.owner().IsSubmodule() &&
3358+
DoesScopeContain(&useUltimate.owner(), localUltimate)) {
3359+
// Within a submodule, USE'ing a symbol that comes indirectly
3360+
// from the ancestor module, e.g. foo in:
3361+
// MODULE m1; INTERFACE; MODULE SUBROUTINE foo; END INTERFACE; END
3362+
// MODULE m2; USE m1; END
3363+
// SUBMODULE m1(sm); USE m2; CONTAINS; MODULE PROCEDURE foo; END; END
3364+
return; // ok, ignore it
3365+
}
33573366

33583367
if (localUltimate.name() == useUltimate.name() &&
33593368
localUltimate.owner().IsModule() && useUltimate.owner().IsModule() &&

flang/test/Semantics/bug124731.f90

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
!RUN: %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck %s --allow-empty
2+
!CHECK-NOT: error:
3+
module m1
4+
interface
5+
module subroutine foo
6+
end
7+
end interface
8+
real x
9+
end
10+
module m2
11+
use m1
12+
end
13+
submodule(m1) sm1
14+
use m2 ! ok
15+
contains
16+
module procedure foo
17+
end
18+
end
19+
submodule(m1) sm2
20+
contains
21+
subroutine bar
22+
use m2 ! ok
23+
end
24+
end

flang/test/Semantics/self-use.f90

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ subroutine modsub
1515
contains
1616
module subroutine separate
1717
!ERROR: Module 'm' cannot USE itself from its own submodule 'submod1'
18-
!ERROR: Cannot use-associate 'separate'; it is already declared in this scope
1918
use m
2019
end
2120
end

0 commit comments

Comments
 (0)