@@ -46,9 +46,11 @@ void swift::ide::typeCheckContextAt(DeclContext *DC, SourceLoc Loc) {
4646 while (isa<AbstractClosureExpr>(DC))
4747 DC = DC->getParent ();
4848
49- // Make sure the extension has been bound, in case it is in an inactive #if
50- // or something weird like that.
49+ // Make sure the extension has been bound.
5150 {
51+ // Even if the extension is invalid (e.g. nested in a function or another
52+ // type), we want to know the "intended nominal" of the extension so that
53+ // we can know the type of 'Self'.
5254 SmallVector<ExtensionDecl *, 1 > extensions;
5355 for (auto typeCtx = DC->getInnermostTypeContext (); typeCtx != nullptr ;
5456 typeCtx = typeCtx->getParent ()->getInnermostTypeContext ()) {
@@ -59,6 +61,20 @@ void swift::ide::typeCheckContextAt(DeclContext *DC, SourceLoc Loc) {
5961 extensions.back ()->computeExtendedNominal ();
6062 extensions.pop_back ();
6163 }
64+
65+ // If the completion happens in the inheritance clause of the extension,
66+ // 'DC' is the parent of the extension. We need to iterate the top level
67+ // decls to find it. In theory, we don't need the extended nominal in the
68+ // inheritance clause, but ASTScope lookup requires that. We don't care
69+ // unless 'DC' is not 'SourceFile' because non-toplevel extensions are
70+ // 'canNeverBeBound()' anyway.
71+ if (auto *SF = dyn_cast<SourceFile>(DC)) {
72+ auto &SM = DC->getASTContext ().SourceMgr ;
73+ for (auto *decl : SF->getTopLevelDecls ())
74+ if (auto *ext = dyn_cast<ExtensionDecl>(decl))
75+ if (SM.rangeContainsTokenLoc (ext->getSourceRange (), Loc))
76+ ext->computeExtendedNominal ();
77+ }
6278 }
6379
6480 // Type-check this context.
0 commit comments