Fix caching bug in MemoryBufferSerializedModuleLoader

By populating the memory cache before loading the module, we can avoid a cycle
where a module is imported that is an overlay, which then triggers
ClangImporter, which then (redundantly) triggers the import of the overlay
module, which would reimport the module again, since it's import is still
underway and it hasn't been entered into the cache yet.

rdar://118846313
This commit is contained in:
Adrian Prantl
2024-02-14 17:29:39 -08:00
parent 5ce824f042
commit 543a2cd685
14 changed files with 86 additions and 17 deletions

View File

@@ -1527,12 +1527,16 @@ MemoryBufferSerializedModuleLoader::loadModule(SourceLoc importLoc,
auto *M = ModuleDecl::create(moduleID.Item, Ctx);
SWIFT_DEFER { M->setHasResolvedImports(); };
if (AllowMemoryCache)
Ctx.addLoadedModule(M);
auto *file = loadAST(*M, moduleID.Loc, /*moduleInterfacePath=*/"",
/*moduleInterfaceSourcePath=*/"",
std::move(moduleInputBuffer), {}, {}, isFramework);
if (!file)
if (!file) {
Ctx.removeLoadedModule(moduleID.Item);
return nullptr;
}
// The MemoryBuffer loader is used by LLDB during debugging. Modules imported
// from .swift_ast sections are never produced from textual interfaces. By
@@ -1540,8 +1544,6 @@ MemoryBufferSerializedModuleLoader::loadModule(SourceLoc importLoc,
if (BypassResilience)
M->setBypassResilience();
M->addFile(*file);
if (AllowMemoryCache)
Ctx.addLoadedModule(M);
return M;
}