@@ -46,9 +46,11 @@ void swift::ide::typeCheckContextAt(DeclContext *DC, SourceLoc Loc) {
46
46
while (isa<AbstractClosureExpr>(DC))
47
47
DC = DC->getParent ();
48
48
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.
51
50
{
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'.
52
54
SmallVector<ExtensionDecl *, 1 > extensions;
53
55
for (auto typeCtx = DC->getInnermostTypeContext (); typeCtx != nullptr ;
54
56
typeCtx = typeCtx->getParent ()->getInnermostTypeContext ()) {
@@ -59,6 +61,20 @@ void swift::ide::typeCheckContextAt(DeclContext *DC, SourceLoc Loc) {
59
61
extensions.back ()->computeExtendedNominal ();
60
62
extensions.pop_back ();
61
63
}
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
+ }
62
78
}
63
79
64
80
// Type-check this context.
0 commit comments