Skip to content

Commit 98522b0

Browse files
authored
Merge pull request #32067 from hamishknight/a-delayed-filing
2 parents b148e24 + 439c148 commit 98522b0

File tree

10 files changed

+52
-56
lines changed

10 files changed

+52
-56
lines changed

include/swift/AST/FileUnit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@ class LoadedFile : public FileUnit {
384384

385385
virtual bool isSystemModule() const { return false; }
386386

387+
/// Checks whether an error was encountered while loading the file.
388+
virtual bool hadLoadError() const { return false; }
389+
387390
/// Retrieve the set of generic signatures stored within this module.
388391
///
389392
/// \returns \c true if this module file supports retrieving all of the

include/swift/AST/Module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ class ModuleDecl : public DeclContext, public TypeDecl {
363363
ArrayRef<ImplicitImport> getImplicitImports() const;
364364

365365
ArrayRef<FileUnit *> getFiles() {
366+
assert(!Files.empty() || failedToLoad());
366367
return Files;
367368
}
368369
ArrayRef<const FileUnit *> getFiles() const {
@@ -371,7 +372,6 @@ class ModuleDecl : public DeclContext, public TypeDecl {
371372

372373
bool isClangModule() const;
373374
void addFile(FileUnit &newFile);
374-
void removeFile(FileUnit &existingFile);
375375

376376
/// Creates a map from \c #filePath strings to corresponding \c #file
377377
/// strings, diagnosing any conflicts.

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,13 @@ class SerializedModuleLoaderBase : public ModuleLoader {
149149
///
150150
/// If the AST cannot be loaded and \p diagLoc is present, a diagnostic is
151151
/// printed. (Note that \p diagLoc is allowed to be invalid.)
152-
FileUnit *loadAST(ModuleDecl &M, Optional<SourceLoc> diagLoc,
153-
StringRef moduleInterfacePath,
154-
std::unique_ptr<llvm::MemoryBuffer> moduleInputBuffer,
155-
std::unique_ptr<llvm::MemoryBuffer> moduleDocInputBuffer,
156-
std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer,
157-
bool isFramework, bool treatAsPartialModule);
152+
FileUnit *
153+
loadAST(ModuleDecl &M, Optional<SourceLoc> diagLoc,
154+
StringRef moduleInterfacePath,
155+
std::unique_ptr<llvm::MemoryBuffer> moduleInputBuffer,
156+
std::unique_ptr<llvm::MemoryBuffer> moduleDocInputBuffer,
157+
std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer,
158+
bool isFramework);
158159

159160
/// Check whether the module with a given name can be imported without
160161
/// importing it.
@@ -321,6 +322,8 @@ class SerializedASTFile final : public LoadedFile {
321322
/// file.
322323
const version::Version &getLanguageVersionBuiltWith() const;
323324

325+
virtual bool hadLoadError() const override;
326+
324327
virtual bool isSystemModule() const override;
325328

326329
virtual void lookupValue(DeclName name, NLKind lookupKind,

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,11 +1515,6 @@ void ASTContext::verifyAllLoadedModules() const {
15151515
FrontendStatsTracer tracer(Stats, "verify-all-loaded-modules");
15161516
for (auto &loader : getImpl().ModuleLoaders)
15171517
loader->verifyAllModules();
1518-
1519-
for (auto &topLevelModulePair : LoadedModules) {
1520-
ModuleDecl *M = topLevelModulePair.second;
1521-
assert(!M->getFiles().empty() || M->failedToLoad());
1522-
}
15231518
#endif
15241519
}
15251520

lib/AST/Module.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,10 @@ bool ModuleDecl::isClangModule() const {
498498
}
499499

500500
void ModuleDecl::addFile(FileUnit &newFile) {
501+
// If this is a LoadedFile, make sure it loaded without error.
502+
assert(!(isa<LoadedFile>(newFile) &&
503+
cast<LoadedFile>(newFile).hadLoadError()));
504+
501505
// Require Main and REPL files to be the first file added.
502506
assert(Files.empty() ||
503507
!isa<SourceFile>(newFile) ||
@@ -507,19 +511,6 @@ void ModuleDecl::addFile(FileUnit &newFile) {
507511
clearLookupCache();
508512
}
509513

510-
void ModuleDecl::removeFile(FileUnit &existingFile) {
511-
// Do a reverse search; usually the file to be deleted will be at the end.
512-
std::reverse_iterator<decltype(Files)::iterator> I(Files.end()),
513-
E(Files.begin());
514-
I = std::find(I, E, &existingFile);
515-
assert(I != E);
516-
517-
// Adjust for the std::reverse_iterator offset.
518-
++I;
519-
Files.erase(I.base());
520-
clearLookupCache();
521-
}
522-
523514
#define FORWARD(name, args) \
524515
for (const FileUnit *file : getFiles()) \
525516
file->name args;

lib/Frontend/Frontend.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -845,11 +845,16 @@ bool CompilerInstance::loadPartialModulesAndImplicitImports() {
845845
// Parse all the partial modules first.
846846
for (auto &PM : PartialModules) {
847847
assert(PM.ModuleBuffer);
848-
if (!SML->loadAST(*MainModule, SourceLoc(), /*moduleInterfacePath*/"",
849-
std::move(PM.ModuleBuffer), std::move(PM.ModuleDocBuffer),
850-
std::move(PM.ModuleSourceInfoBuffer), /*isFramework*/false,
851-
/*treatAsPartialModule*/true))
848+
auto *file =
849+
SML->loadAST(*MainModule, SourceLoc(), /*moduleInterfacePath*/ "",
850+
std::move(PM.ModuleBuffer), std::move(PM.ModuleDocBuffer),
851+
std::move(PM.ModuleSourceInfoBuffer),
852+
/*isFramework*/ false);
853+
if (file) {
854+
MainModule->addFile(*file);
855+
} else {
852856
hadLoadError = true;
857+
}
853858
}
854859
return hadLoadError;
855860
}

lib/Serialization/ModuleFile.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,9 +1977,7 @@ ModuleFile::ModuleFile(
19771977
}
19781978
}
19791979

1980-
Status ModuleFile::associateWithFileContext(FileUnit *file,
1981-
SourceLoc diagLoc,
1982-
bool treatAsPartialModule) {
1980+
Status ModuleFile::associateWithFileContext(FileUnit *file, SourceLoc diagLoc) {
19831981
PrettyStackTraceModuleFile stackEntry(*this);
19841982

19851983
assert(!hasError() && "error already detected; should not call this");
@@ -2033,8 +2031,12 @@ Status ModuleFile::associateWithFileContext(FileUnit *file,
20332031
continue;
20342032
}
20352033

2034+
// If this module file is being installed into the main module, it's treated
2035+
// as a partial module.
2036+
auto isPartialModule = M->isMainModule();
2037+
20362038
if (dependency.isImplementationOnly() &&
2037-
!(treatAsPartialModule || ctx.LangOpts.DebuggerSupport)) {
2039+
!(isPartialModule || ctx.LangOpts.DebuggerSupport)) {
20382040
// When building normally (and not merging partial modules), we don't
20392041
// want to bring in the implementation-only module, because that might
20402042
// change the set of visible declarations. However, when debugging we

lib/Serialization/ModuleFile.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -705,14 +705,10 @@ class ModuleFile
705705
/// This does not include diagnostics about \e this file failing to load,
706706
/// but rather other things that might be imported as part of bringing the
707707
/// file into the AST.
708-
/// \param treatAsPartialModule If true, processes implementation-only
709-
/// information instead of assuming the client won't need it and shouldn't
710-
/// see it.
711708
///
712709
/// \returns any error that occurred during association, such as being
713710
/// compiled for a different OS.
714-
Status associateWithFileContext(FileUnit *file, SourceLoc diagLoc,
715-
bool treatAsPartialModule);
711+
Status associateWithFileContext(FileUnit *file, SourceLoc diagLoc);
716712

717713
/// Transfers ownership of a buffer that might contain source code where
718714
/// other parts of the compiler could have emitted diagnostics, to keep them

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ FileUnit *SerializedModuleLoaderBase::loadAST(
651651
std::unique_ptr<llvm::MemoryBuffer> moduleInputBuffer,
652652
std::unique_ptr<llvm::MemoryBuffer> moduleDocInputBuffer,
653653
std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer,
654-
bool isFramework, bool treatAsPartialModule) {
654+
bool isFramework) {
655655
assert(moduleInputBuffer);
656656

657657
StringRef moduleBufferID = moduleInputBuffer->getBufferIdentifier();
@@ -680,16 +680,14 @@ FileUnit *SerializedModuleLoaderBase::loadAST(
680680

681681
// We've loaded the file. Now try to bring it into the AST.
682682
auto fileUnit = new (Ctx) SerializedASTFile(M, *loadedModuleFile);
683-
M.addFile(*fileUnit);
684683
if (extendedInfo.isTestable())
685684
M.setTestingEnabled();
686685
if (extendedInfo.arePrivateImportsEnabled())
687686
M.setPrivateImportsEnabled();
688687

689688
auto diagLocOrInvalid = diagLoc.getValueOr(SourceLoc());
690689
loadInfo.status =
691-
loadedModuleFile->associateWithFileContext(fileUnit, diagLocOrInvalid,
692-
treatAsPartialModule);
690+
loadedModuleFile->associateWithFileContext(fileUnit, diagLocOrInvalid);
693691

694692
// FIXME: This seems wrong. Overlay for system Clang module doesn't
695693
// necessarily mean it's "system" module. User can make their own overlay
@@ -706,8 +704,6 @@ FileUnit *SerializedModuleLoaderBase::loadAST(
706704
findOverlayFiles(diagLoc.getValueOr(SourceLoc()), &M, fileUnit);
707705
return fileUnit;
708706
}
709-
710-
M.removeFile(*fileUnit);
711707
}
712708

713709
// From here on is the failure path.
@@ -961,13 +957,15 @@ SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
961957
StringRef moduleInterfacePathStr =
962958
Ctx.AllocateCopy(moduleInterfacePath.str());
963959

964-
if (!loadAST(*M, moduleID.Loc, moduleInterfacePathStr,
965-
std::move(moduleInputBuffer), std::move(moduleDocInputBuffer),
966-
std::move(moduleSourceInfoInputBuffer),
967-
isFramework, /*treatAsPartialModule*/false)) {
960+
auto *file =
961+
loadAST(*M, moduleID.Loc, moduleInterfacePathStr,
962+
std::move(moduleInputBuffer), std::move(moduleDocInputBuffer),
963+
std::move(moduleSourceInfoInputBuffer), isFramework);
964+
if (file) {
965+
M->addFile(*file);
966+
} else {
968967
M->setFailedToLoad();
969968
}
970-
971969
return M;
972970
}
973971

@@ -990,7 +988,6 @@ MemoryBufferSerializedModuleLoader::loadModule(SourceLoc importLoc,
990988
return nullptr;
991989

992990
bool isFramework = false;
993-
bool treatAsPartialModule = false;
994991
std::unique_ptr<llvm::MemoryBuffer> moduleInputBuffer;
995992
moduleInputBuffer = std::move(bufIter->second);
996993
MemoryBuffers.erase(bufIter);
@@ -999,12 +996,12 @@ MemoryBufferSerializedModuleLoader::loadModule(SourceLoc importLoc,
999996
auto *M = ModuleDecl::create(moduleID.Item, Ctx);
1000997
SWIFT_DEFER { M->setHasResolvedImports(); };
1001998

1002-
if (!loadAST(*M, moduleID.Loc, /*moduleInterfacePath*/ "",
1003-
std::move(moduleInputBuffer), {}, {},
1004-
isFramework, treatAsPartialModule)) {
999+
auto *file = loadAST(*M, moduleID.Loc, /*moduleInterfacePath*/ "",
1000+
std::move(moduleInputBuffer), {}, {}, isFramework);
1001+
if (!file)
10051002
return nullptr;
1006-
}
10071003

1004+
M->addFile(*file);
10081005
Ctx.LoadedModules[moduleID.Item] = M;
10091006
return M;
10101007
}
@@ -1106,6 +1103,10 @@ bool SerializedASTFile::isSIB() const {
11061103
return File.IsSIB;
11071104
}
11081105

1106+
bool SerializedASTFile::hadLoadError() const {
1107+
return File.hasError();
1108+
}
1109+
11091110
bool SerializedASTFile::isSystemModule() const {
11101111
if (auto Mod = File.getUnderlyingModule()) {
11111112
return Mod->isSystemModule();

tools/SourceKit/lib/SwiftLang/SwiftIndexing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ static void indexModule(llvm::MemoryBuffer *Input,
186186
// correct filename here.
187187
auto FUnit = Loader->loadAST(*Mod, None, /*moduleInterfacePath*/"",
188188
std::move(Buf), nullptr, nullptr,
189-
/*isFramework*/false,
190-
/*treatAsPartialModule*/false);
189+
/*isFramework*/false);
191190

192191
// FIXME: Not knowing what went wrong is pretty bad. loadModule() should be
193192
// more modular, rather than emitting diagnostics itself.
@@ -196,6 +195,7 @@ static void indexModule(llvm::MemoryBuffer *Input,
196195
return;
197196
}
198197

198+
Mod->addFile(*FUnit);
199199
Mod->setHasResolvedImports();
200200
}
201201

0 commit comments

Comments
 (0)