Skip to content

Commit 30a9b21

Browse files
committed
[AST] Clear module lookup cache in SourceFile::addTopLevelDecl
Completion unfortunately relies on this to add a top-level decl after the lookup table has been built, make sure the decl gets added to it. We ought to fix completion to avoid needing to mutate the AST like this after-the-fact, but this should work around the issue for now.
1 parent 5755fb2 commit 30a9b21

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

include/swift/AST/Module.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -916,11 +916,9 @@ class ModuleDecl
916916
void lookupTopLevelDeclsByObjCName(SmallVectorImpl<Decl *> &Results,
917917
DeclName name);
918918

919-
/// This is a hack for 'main' file parsing and the integrated REPL.
920-
///
921-
/// FIXME: Refactor main file parsing to not pump the parser incrementally.
922-
/// FIXME: Remove the integrated REPL.
923-
void clearLookupCache();
919+
/// This is a hack for SynthesizedFileUnit and code completion. Do not add new
920+
/// uses of it.
921+
void clearLookupCache(bool includingImports = true);
924922

925923
/// Finds all class members defined in this module.
926924
///

lib/AST/Module.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3239,8 +3239,9 @@ bool SourceFile::shouldCrossImport() const {
32393239
getASTContext().LangOpts.EnableCrossImportOverlays;
32403240
}
32413241

3242-
void ModuleDecl::clearLookupCache() {
3243-
getASTContext().getImportCache().clear();
3242+
void ModuleDecl::clearLookupCache(bool includingImports) {
3243+
if (includingImports)
3244+
getASTContext().getImportCache().clear();
32443245

32453246
setIsObjCNameLookupCachePopulated(false);
32463247
ObjCNameLookupCache.clear();
@@ -3495,6 +3496,8 @@ void SourceFile::addTopLevelDecl(Decl *d) {
34953496
auto &ctx = getASTContext();
34963497
auto *mutableThis = const_cast<SourceFile *>(this);
34973498
ctx.evaluator.clearCachedOutput(ParseTopLevelDeclsRequest{mutableThis});
3499+
3500+
getParentModule()->clearLookupCache(/*includingImports*/isa<ImportDecl>(d));
34983501
}
34993502

35003503
void SourceFile::prependTopLevelDecl(Decl *d) {

test/IDE/pr83369.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %batch-code-completion -module-name main
2+
3+
extension Int {}
4+
5+
// Make sure we can resolve P here.
6+
protocol P<X> where X: main.#^COMPLETE^# {
7+
associatedtype X
8+
}
9+
// COMPLETE: Decl[Protocol]/CurrModule: P[#P#]; name=P

0 commit comments

Comments
 (0)