Skip to content

Commit 5b9bd88

Browse files
authored
[flang] Fix crash with USE of hermetic module file (#138785)
When one hermetic module file uses another, a later compilation may crash in semantics when it itself is used, since the module file reader sets the "current hermetic module file scope" to null after reading one rather than saving and restoring that pointer.
1 parent ea87d7c commit 5b9bd88

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

flang/lib/Semantics/mod-file.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,7 @@ Scope *ModFileReader::Read(SourceName name, std::optional<bool> isIntrinsic,
15481548
// created under -fhermetic-module-files? If so, process them first in
15491549
// their own nested scope that will be visible only to USE statements
15501550
// within the module file.
1551+
Scope *previousHermetic{context_.currentHermeticModuleFileScope()};
15511552
if (parseTree.v.size() > 1) {
15521553
parser::Program hermeticModules{std::move(parseTree.v)};
15531554
parseTree.v.emplace_back(std::move(hermeticModules.v.front()));
@@ -1563,7 +1564,7 @@ Scope *ModFileReader::Read(SourceName name, std::optional<bool> isIntrinsic,
15631564
GetModuleDependences(context_.moduleDependences(), sourceFile->content());
15641565
ResolveNames(context_, parseTree, topScope);
15651566
context_.foldingContext().set_moduleFileName(wasModuleFileName);
1566-
context_.set_currentHermeticModuleFileScope(nullptr);
1567+
context_.set_currentHermeticModuleFileScope(previousHermetic);
15671568
if (!moduleSymbol) {
15681569
// Submodule symbols' storage are owned by their parents' scopes,
15691570
// but their names are not in their parents' dictionaries -- we

flang/test/Semantics/modfile75.F90

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
!RUN: %flang -c -fhermetic-module-files -DWHICH=1 %s && %flang -c -fhermetic-module-files -DWHICH=2 %s && %flang_fc1 -fdebug-unparse %s | FileCheck %s
2+
3+
#if WHICH == 1
4+
module modfile75a
5+
use iso_c_binding
6+
end
7+
#elif WHICH == 2
8+
module modfile75b
9+
use modfile75a
10+
end
11+
#else
12+
program test
13+
use modfile75b
14+
!CHECK: INTEGER(KIND=4_4) n
15+
integer(c_int) n
16+
end
17+
#endif

0 commit comments

Comments
 (0)