Skip to content

[CodeCompletion] Ensure all ExtensionDecl's extended types are computed #33669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/IDE/ExprContextAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ void swift::ide::typeCheckContextAt(DeclContext *DC, SourceLoc Loc) {
while (isa<AbstractClosureExpr>(DC))
DC = DC->getParent();

// Make sure the extension has been bound, in case it is in an inactive #if
// or something weird like that.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code completion never happens inside inactive #if since #33475

// Make sure the extension has been bound.
{
// Even if the extension is invalid (e.g. nested in a function or another
// type), we want to know the "intended nominal" of the extension so that
// we can know the type of 'Self'.
SmallVector<ExtensionDecl *, 1> extensions;
for (auto typeCtx = DC->getInnermostTypeContext(); typeCtx != nullptr;
typeCtx = typeCtx->getParent()->getInnermostTypeContext()) {
Expand All @@ -59,6 +61,20 @@ void swift::ide::typeCheckContextAt(DeclContext *DC, SourceLoc Loc) {
extensions.back()->computeExtendedNominal();
extensions.pop_back();
}

// If the completion happens in the inheritance clause of the extension,
// 'DC' is the parent of the extension. We need to iterate the top level
// decls to find it. In theory, we don't need the extended nominal in the
// inheritance clause, but ASTScope lookup requires that. We don't care
// unless 'DC' is not 'SourceFile' because non-toplevel extensions are
// 'canNeverBeBound()' anyway.
if (auto *SF = dyn_cast<SourceFile>(DC)) {
auto &SM = DC->getASTContext().SourceMgr;
for (auto *decl : SF->getTopLevelDecls())
if (auto *ext = dyn_cast<ExtensionDecl>(decl))
if (SM.rangeContainsTokenLoc(ext->getSourceRange(), Loc))
ext->computeExtendedNominal();
}
}

// Type-check this context.
Expand Down
14 changes: 14 additions & 0 deletions test/IDE/complete_type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -765,3 +765,17 @@ func testUnbound2(x: OuterStruct<Int>.Inner.#^UNBOUND_DOT_3^#) {}
// UNBOUND_DOT_3: Begin completions
// UNBOUND_DOT_3-DAG: Keyword/None: Type[#OuterStruct<Int>.Inner.Type#]; name=Type
// UNBOUND_DOT_3: End completions

// rdar://problem/67102794
struct HasProtoAlias {
typealias ProtoAlias = FooProtocol
}
extension FooStruct: HasProtoAlias.#^EXTENSION_INHERITANCE_1?check=EXTENSION_INHERITANCE^# {}

struct ContainExtension {
extension FooStruct: HasProtoAlias.#^EXTENSION_INHERITANCE_2?check=EXTENSION_INHERITANCE^# {}
}
// EXTENSION_INHERITANCE: Begin completions, 2 items
// EXTENSION_INHERITANCE-DAG: Decl[TypeAlias]/CurrNominal: ProtoAlias[#FooProtocol#];
// EXTENSION_INHERITANCE-DAG: Keyword/None: Type[#HasProtoAlias.Type#];
// EXTENSION_INHERITANCE: End completions