Skip to content

Commit 9f0f54a

Browse files
authored
[flang] Improve error messages for module self-USE (#122747)
A module can't USE itself, either directly within the top-level module or from one of its submodules. Add a test for this case (which we already caught), and improve the diagnostic for the more confusing case involving a submodule.
1 parent 874a3ba commit 9f0f54a

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

flang/include/flang/Semantics/tools.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const Scope &GetProgramUnitOrBlockConstructContaining(const Scope &);
4242
const Scope &GetProgramUnitOrBlockConstructContaining(const Symbol &);
4343

4444
const Scope *FindModuleContaining(const Scope &);
45+
const Scope *FindModuleOrSubmoduleContaining(const Scope &);
4546
const Scope *FindModuleFileContaining(const Scope &);
4647
const Scope *FindPureProcedureContaining(const Scope &);
4748
const Scope *FindOpenACCConstructContaining(const Scope *);

flang/lib/Semantics/resolve-names.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3607,13 +3607,23 @@ Scope *ModuleVisitor::FindModule(const parser::Name &name,
36073607
ModFileReader reader{context()};
36083608
Scope *scope{
36093609
reader.Read(name.source, isIntrinsic, ancestor, /*silent=*/false)};
3610-
if (!scope) {
3611-
return nullptr;
3612-
}
3613-
if (DoesScopeContain(scope, currScope())) { // 14.2.2(1)
3614-
Say(name, "Module '%s' cannot USE itself"_err_en_US);
3610+
if (scope) {
3611+
if (DoesScopeContain(scope, currScope())) { // 14.2.2(1)
3612+
std::optional<SourceName> submoduleName;
3613+
if (const Scope * container{FindModuleOrSubmoduleContaining(currScope())};
3614+
container && container->IsSubmodule()) {
3615+
submoduleName = container->GetName();
3616+
}
3617+
if (submoduleName) {
3618+
Say(name.source,
3619+
"Module '%s' cannot USE itself from its own submodule '%s'"_err_en_US,
3620+
name.source, *submoduleName);
3621+
} else {
3622+
Say(name, "Module '%s' cannot USE itself"_err_en_US);
3623+
}
3624+
}
3625+
Resolve(name, scope->symbol());
36153626
}
3616-
Resolve(name, scope->symbol());
36173627
return scope;
36183628
}
36193629

flang/lib/Semantics/tools.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ const Scope *FindModuleContaining(const Scope &start) {
5252
start, [](const Scope &scope) { return scope.IsModule(); });
5353
}
5454

55+
const Scope *FindModuleOrSubmoduleContaining(const Scope &start) {
56+
return FindScopeContaining(start, [](const Scope &scope) {
57+
return scope.IsModule() || scope.IsSubmodule();
58+
});
59+
}
60+
5561
const Scope *FindModuleFileContaining(const Scope &start) {
5662
return FindScopeContaining(
5763
start, [](const Scope &scope) { return scope.IsModuleFile(); });

flang/test/Semantics/self-use.f90

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
! RUN: %python %S/test_errors.py %s %flang_fc1
2+
module m
3+
interface
4+
module subroutine separate
5+
end
6+
end interface
7+
contains
8+
subroutine modsub
9+
!ERROR: Module 'm' cannot USE itself
10+
use m
11+
end
12+
end
13+
14+
submodule(m) submod1
15+
contains
16+
module subroutine separate
17+
!ERROR: Module 'm' cannot USE itself from its own submodule 'submod1'
18+
!ERROR: Cannot use-associate 'separate'; it is already declared in this scope
19+
use m
20+
end
21+
end
22+
23+
submodule(m) submod2
24+
!ERROR: Module 'm' cannot USE itself from its own submodule 'submod2'
25+
use m
26+
end
27+

0 commit comments

Comments
 (0)