Skip to content

Commit 60ce308

Browse files
committed
Sema: Only run extension binding once
There's no need to walk all imports of all source files, and bind the extensions defined therein. The only time that a non-main module contains source files is when using -enable-source-import, and we can just explicitly call bindExtensions() in the right place.
1 parent 5b6aa8d commit 60ce308

File tree

7 files changed

+21
-36
lines changed

7 files changed

+21
-36
lines changed

include/swift/Subsystems.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ namespace swift {
140140
void performPCMacro(SourceFile &SF);
141141

142142
/// Bind all 'extension' visible from \p SF to the extended nominal.
143-
void bindExtensions(SourceFile &SF);
143+
void bindExtensions(ModuleDecl &mod);
144144

145145
/// Once import resolution is complete, this walks the AST to resolve types
146146
/// and diagnose problems therein.

lib/Frontend/Frontend.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -806,12 +806,13 @@ void CompilerInstance::performSemaUpTo(SourceFile::ASTStage_t LimitStage) {
806806
}) && "some files have not yet had their imports resolved");
807807
MainModule->setHasResolvedImports();
808808

809-
forEachFileToTypeCheck([&](SourceFile &SF) {
810-
if (LimitStage == SourceFile::ImportsResolved) {
811-
bindExtensions(SF);
812-
return;
813-
}
809+
bindExtensions(*MainModule);
810+
811+
// If the limiting AST stage is import resolution, we're done.
812+
if (LimitStage == SourceFile::ImportsResolved)
813+
return;
814814

815+
forEachFileToTypeCheck([&](SourceFile &SF) {
815816
performTypeChecking(SF);
816817

817818
// Parse the SIL decls if needed.
@@ -822,11 +823,6 @@ void CompilerInstance::performSemaUpTo(SourceFile::ASTStage_t LimitStage) {
822823
}
823824
});
824825

825-
// If the limiting AST stage is import resolution, we're done.
826-
if (LimitStage <= SourceFile::ImportsResolved) {
827-
return;
828-
}
829-
830826
finishTypeChecking();
831827
}
832828

lib/IDE/CompletionInstance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ bool CompletionInstance::performCachedOperationIfPossible(
446446
// Re-process the whole file (parsing will be lazily triggered). Still
447447
// re-use imported modules.
448448
performImportResolution(*newSF);
449-
bindExtensions(*newSF);
449+
bindExtensions(*newM);
450450

451451
CurrentModule = newM;
452452
traceDC = newM;

lib/IDE/REPLCodeCompletion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ doCodeCompletion(SourceFile &SF, StringRef EnteredCode, unsigned *BufferID,
239239
newModule->addFile(newSF);
240240

241241
performImportResolution(newSF);
242-
bindExtensions(newSF);
242+
bindExtensions(*newModule);
243243

244244
performCodeCompletionSecondPass(newSF, *CompletionCallbacksFactory);
245245

lib/Immediate/REPL.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ typeCheckREPLInput(ModuleDecl *MostRecentModule, StringRef Name,
184184
auto &REPLInputFile =
185185
*new (Ctx) SourceFile(*REPLModule, SourceFileKind::REPL, BufferID);
186186
REPLModule->addFile(REPLInputFile);
187+
performImportResolution(REPLInputFile);
188+
bindExtensions(*REPLModule);
187189
performTypeChecking(REPLInputFile);
188190
return REPLModule;
189191
}

lib/Sema/SourceLoader.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ ModuleDecl *SourceLoader::loadModule(SourceLoc importLoc,
131131
importMod->addFile(*importFile);
132132
performImportResolution(*importFile);
133133
importMod->setHasResolvedImports();
134+
bindExtensions(*importMod);
134135
return importMod;
135136
}
136137

lib/Sema/TypeChecker.cpp

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ static void bindExtensionToNominal(ExtensionDecl *ext,
227227
nominal->addExtension(ext);
228228
}
229229

230-
static void bindExtensions(SourceFile &SF) {
230+
void swift::bindExtensions(ModuleDecl &mod) {
231231
// Utility function to try and resolve the extended type without diagnosing.
232232
// If we succeed, we go ahead and bind the extension. Otherwise, return false.
233233
auto tryBindExtension = [&](ExtensionDecl *ext) -> bool {
@@ -245,20 +245,15 @@ static void bindExtensions(SourceFile &SF) {
245245
// resolved to a worklist.
246246
SmallVector<ExtensionDecl *, 8> worklist;
247247

248-
// FIXME: The current source file needs to be handled specially, because of
249-
// private extensions.
250-
for (auto import : namelookup::getAllImports(&SF)) {
251-
// FIXME: Respect the access path?
252-
for (auto file : import.importedModule->getFiles()) {
253-
auto SF = dyn_cast<SourceFile>(file);
254-
if (!SF)
255-
continue;
248+
for (auto file : mod.getFiles()) {
249+
auto *SF = dyn_cast<SourceFile>(file);
250+
if (!SF)
251+
continue;
256252

257-
for (auto D : SF->getTopLevelDecls()) {
258-
if (auto ED = dyn_cast<ExtensionDecl>(D))
259-
if (!tryBindExtension(ED))
260-
worklist.push_back(ED);
261-
}
253+
for (auto D : SF->getTopLevelDecls()) {
254+
if (auto ED = dyn_cast<ExtensionDecl>(D))
255+
if (!tryBindExtension(ED))
256+
worklist.push_back(ED);
262257
}
263258
}
264259

@@ -354,11 +349,6 @@ TypeCheckSourceFileRequest::evaluate(Evaluator &eval, SourceFile *SF) const {
354349
TypeChecker::buildTypeRefinementContextHierarchy(*SF);
355350
}
356351

357-
// Resolve extensions. This has to occur first during type checking,
358-
// because the extensions need to be wired into the AST for name lookup
359-
// to work.
360-
::bindExtensions(*SF);
361-
362352
// Type check the top-level elements of the source file.
363353
for (auto D : SF->getTopLevelDecls()) {
364354
if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(D)) {
@@ -702,10 +692,6 @@ TypeChecker::getDeclTypeCheckingSemantics(ValueDecl *decl) {
702692
return DeclTypeCheckingSemantics::Normal;
703693
}
704694

705-
void swift::bindExtensions(SourceFile &SF) {
706-
::bindExtensions(SF);
707-
}
708-
709695
LookupResult
710696
swift::lookupSemanticMember(DeclContext *DC, Type ty, DeclName name) {
711697
return TypeChecker::lookupMember(DC, ty, DeclNameRef(name), None);

0 commit comments

Comments
 (0)