Skip to content

Commit a0af6e5

Browse files
committed
Always Make The Top-Level Module Visible
The Clang Importer currently has a ridiculous re-entrant submodule loading path that winds up doing this anyways. Take Darwin as a prime example. import Darwin.C.tgmath In theory, this should load Darwin.C.tgmath and its immediate clang-only dependencies. In practice, namelookup::getAllImports winds up asking for the overlay associated with Darwin.C.tgmath. In order to load the overlay, it walks to the top-level module (Darwin) and tries to load a module with that name. Becuase we're in the middle of loading Darwin while importing tgmath, and because the compiler does not distinguish the act of loading a module from the act of loading an overlay, the Clang Importer would re-enter itself. The net effect is that every submodule import of a clang module *always* loads the top-most module and makes it visible. So import Darwin.C.tgmath actually becomes import Darwin.C.tgmath import Darwin As long as we're here, and because this doesn't seem to actually hurt anything, let's optimize for this behavior. If the top-level module is requested, we need not call finishLoadingClangModule twice, just return the wrapper module it gives back. Eventually, we should tame this implicit behavior with respect to overlay module loading.
1 parent 4dde3ac commit a0af6e5

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,9 +1771,16 @@ ModuleDecl *ClangImporter::Implementation::loadModuleClang(
17711771

17721772
// Now load the top-level module, so that we can check if the submodule
17731773
// exists without triggering a fatal error.
1774+
clangModule = loadModule(clangPath.front(), clang::Module::AllVisible);
17741775
if (!clangModule)
17751776
return nullptr;
17761777

1778+
// If we're asked to import the top-level module then we're done here.
1779+
auto *topSwiftModule = finishLoadingClangModule(clangModule, importLoc);
1780+
if (path.size() == 1) {
1781+
return topSwiftModule;
1782+
}
1783+
17771784
// Verify that the submodule exists.
17781785
clang::Module *submodule = clangModule;
17791786
for (auto &component : path.slice(1)) {

0 commit comments

Comments
 (0)