Skip to content

Commit ce7debf

Browse files
committed
[flang] Fix crash with USE of hermetic module file
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 e5f3260 commit ce7debf

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

flang/lib/Semantics/mod-file.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,7 @@ Scope *ModFileReader::Read(SourceName name, std::optional<bool> isIntrinsic,
15371537
// created under -fhermetic-module-files? If so, process them first in
15381538
// their own nested scope that will be visible only to USE statements
15391539
// within the module file.
1540+
Scope *previousHermetic{context_.currentHermeticModuleFileScope()};
15401541
if (parseTree.v.size() > 1) {
15411542
parser::Program hermeticModules{std::move(parseTree.v)};
15421543
parseTree.v.emplace_back(std::move(hermeticModules.v.front()));
@@ -1552,7 +1553,7 @@ Scope *ModFileReader::Read(SourceName name, std::optional<bool> isIntrinsic,
15521553
GetModuleDependences(context_.moduleDependences(), sourceFile->content());
15531554
ResolveNames(context_, parseTree, topScope);
15541555
context_.foldingContext().set_moduleFileName(wasModuleFileName);
1555-
context_.set_currentHermeticModuleFileScope(nullptr);
1556+
context_.set_currentHermeticModuleFileScope(previousHermetic);
15561557
if (!moduleSymbol) {
15571558
// Submodule symbols' storage are owned by their parents' scopes,
15581559
// but their names are not in their parents' dictionaries -- we

flang/test/Semantics/modfile75.F90

+17
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)