Skip to content

[CodeCompletion] Improve context type analysis for overloaded method #20013

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
Oct 30, 2018
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
93 changes: 75 additions & 18 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3887,6 +3887,53 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {

using FunctionParams = ArrayRef<AnyFunctionType::Param>;

static void collectPossibleParamListByQualifiedLookup(
DeclContext &DC, Type baseTy, DeclBaseName name,
SmallVectorImpl<FunctionParams> &candidates) {

SmallVector<ValueDecl *, 2> decls;
auto resolver = DC.getASTContext().getLazyResolver();
if (!DC.lookupQualified(baseTy, name, NL_QualifiedDefault, resolver, decls))
return;

for (auto *VD : decls) {
if (!isa<AbstractFunctionDecl>(VD) ||
shouldHideDeclFromCompletionResults(VD))
continue;
resolver->resolveDeclSignature(VD);
if (!VD->hasInterfaceType())
continue;
Type declaredMemberType = VD->getInterfaceType();
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(VD))
declaredMemberType = AFD->getMethodInterfaceType();

auto fnType =
baseTy->getTypeOfMember(DC.getParentModule(), VD, declaredMemberType);

if (!fnType || fnType->hasError())
continue;
if (auto *AFT = fnType->getAs<AnyFunctionType>()) {
candidates.push_back(AFT->getParams());
}
}
}

static void collectPossibleParamListByQualifiedLookup(
DeclContext &DC, Expr *baseExpr, DeclBaseName name,
SmallVectorImpl<FunctionParams> &candidates) {
ConcreteDeclRef ref = nullptr;
auto baseTyOpt = getTypeOfCompletionContextExpr(
DC.getASTContext(), &DC, CompletionTypeCheckKind::Normal, baseExpr,
ref);
if (!baseTyOpt)
return;
auto baseTy = (*baseTyOpt)->getRValueType()->getMetatypeInstanceType();
if (!baseTy->mayHaveMembers())
return;

collectPossibleParamListByQualifiedLookup(DC, baseTy, name, candidates);
}

static bool
collectPossibleParamLists(DeclContext &DC, ApplyExpr *callExpr,
SmallVectorImpl<FunctionParams> &candidates) {
Expand All @@ -3907,17 +3954,27 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
if (auto *funcType = declType->getAs<AnyFunctionType>())
candidates.push_back(funcType->getParams());
}
} else {
ConcreteDeclRef ref = nullptr;
auto fnType = getTypeOfCompletionContextExpr(DC.getASTContext(),
&DC, CompletionTypeCheckKind::Normal,
fnExpr, ref);
} else if (auto *UDE = dyn_cast<UnresolvedDotExpr>(fnExpr)) {
collectPossibleParamListByQualifiedLookup(
DC, UDE->getBase(), UDE->getName().getBaseName(), candidates);
}

if (candidates.empty()) {
ConcreteDeclRef ref = nullptr;
auto fnType = getTypeOfCompletionContextExpr(
DC.getASTContext(), &DC, CompletionTypeCheckKind::Normal, fnExpr,
ref);
if (!fnType)
return false;

if (auto *AFT = (*fnType)->getAs<AnyFunctionType>())
if (auto *AFT = (*fnType)->getAs<AnyFunctionType>()) {
candidates.push_back(AFT->getParams());
} else if (auto *AMT = (*fnType)->getAs<AnyMetatypeType>()) {
auto baseTy = AMT->getInstanceType();
if (baseTy->mayHaveMembers())
collectPossibleParamListByQualifiedLookup(
DC, baseTy, DeclBaseName::createConstructor(), candidates);
}
}

return !candidates.empty();
Expand Down Expand Up @@ -5165,30 +5222,30 @@ namespace {

public:
llvm::SmallVector<ParentTy, 5> Ancestors;
ParentTy ParentClosest;
ParentTy ParentFarthest;
ExprParentFinder(Expr* ChildExpr,
llvm::function_ref<bool(ParentTy, ParentTy)> Predicate) :
ChildExpr(ChildExpr), Predicate(Predicate) {}

std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
if (E != ChildExpr && Predicate(E, Parent)) {
// Finish if we found the target.
if (E == ChildExpr)
return { false, nullptr };

if (Predicate(E, Parent))
Ancestors.push_back(E);
return { true, E };
}
if (E == ChildExpr || arePositionsSame(E, ChildExpr)) {
if (!Ancestors.empty()) {
ParentClosest = Ancestors.back();
ParentFarthest = Ancestors.front();
}
return {false, nullptr};
}
return { true, E };
}

Expr *walkToExprPost(Expr *E) override {
if (Predicate(E, Parent))
Ancestors.pop_back();

// 'ChildExpr' might have been replaced with typechecked expression. In
// that case, find deepest expression that position is the same as the
// target.
if (arePositionsSame(E, ChildExpr))
return nullptr;

return E;
}

Expand Down
6 changes: 2 additions & 4 deletions test/IDE/complete_call_arg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,9 @@ struct EmptyOverload {
init(foo: Int) {}
}
_ = EmptyOverload(foo: #^EMPTY_OVERLOAD_2^#)
// FIXME: we should have a TypeRelation[Identical] here for Ints. For now just
// check it's not empty.
// EMPTY_OVERLOAD: Begin completions
// EMPTY_OVERLOAD-DAG: Decl[GlobalVar]/Local{{.*}}: i2[#Int#];
// EMPTY_OVERLOAD-DAG: Decl[GlobalVar]/Local{{.*}}: i1[#Int#];
// EMPTY_OVERLOAD-DAG: Decl[GlobalVar]/Local/TypeRelation[Identical]: i2[#Int#];
// EMPTY_OVERLOAD-DAG: Decl[GlobalVar]/Local/TypeRelation[Identical]: i1[#Int#];
// EMPTY_OVERLOAD: End completions

public func fopen() -> TestBoundGeneric1! { fatalError() }
Expand Down
26 changes: 26 additions & 0 deletions test/IDE/complete_unresolved_members.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@

// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=STATIC_CLOSURE_1 | %FileCheck %s -check-prefix=STATIC_CLOSURE_1

// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OVERLOADED_METHOD_1 | %FileCheck %s -check-prefix=OVERLOADED_METHOD_1
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OVERLOADED_INIT_1 | %FileCheck %s -check-prefix=OVERLOADED_METHOD_1
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OVERLOADED_INIT_2 | %FileCheck %s -check-prefix=OVERLOADED_METHOD_1

enum SomeEnum1 {
case South
case North
Expand Down Expand Up @@ -494,3 +498,25 @@ func testHasStaticClosure() {
// STATIC_CLOSURE_1-DAG: Decl[StaticVar]/CurrNominal: create[#() -> HasCreator#];
// STATIC_CLOSURE_1-NOT: create_curried
// STATIC_CLOSURE_1: End completions

struct HasOverloaded {
init(e: SomeEnum1) {}
init(e: SomeEnum2) {}
func takeEnum(_ e: SomeEnum1) -> Int { return 0 }
func takeEnum(_ e: SomeEnum2) -> Int { return 0 }
}
func testOverload(val: HasOverloaded) {
let _ = val.takeEnum(.#^OVERLOADED_METHOD_1^#)
// OVERLOADED_METHOD_1: Begin completions, 4 items
// OVERLOADED_METHOD_1-DAG: Decl[EnumElement]/ExprSpecific: South[#SomeEnum1#]; name=South
// OVERLOADED_METHOD_1-DAG: Decl[EnumElement]/ExprSpecific: North[#SomeEnum1#]; name=North
// OVERLOADED_METHOD_1-DAG: Decl[EnumElement]/ExprSpecific: East[#SomeEnum2#]; name=East
// OVERLOADED_METHOD_1-DAG: Decl[EnumElement]/ExprSpecific: West[#SomeEnum2#]; name=West
// OVERLOADED_METHOD_1: End completions

let _ = HasOverloaded.init(e: .#^OVERLOADED_INIT_1^#)
// Same as OVERLOADED_METHOD_1.

let _ = HasOverloaded(e: .#^OVERLOADED_INIT_2^#)
// Same as OVERLOADED_METHOD_1.
}
2 changes: 1 addition & 1 deletion test/IDE/complete_value_expr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ func testInsideFunctionCall4() {
func testInsideFunctionCall5() {
FooStruct().instanceFunc2(42, #^INSIDE_FUNCTION_CALL_5^#
// INSIDE_FUNCTION_CALL_5: Begin completions
// INSIDE_FUNCTION_CALL_5-DAG: Decl[GlobalVar]/CurrModule: fooObject[#FooStruct#]{{; name=.+$}}
// INSIDE_FUNCTION_CALL_5-DAG: Keyword/ExprSpecific: b: [#Argument name#]; name=b:
// INSIDE_FUNCTION_CALL_5: End completions
}

Expand Down