Skip to content

Commit 63d1ba3

Browse files
committed
[CodeCompletion] Handle metatype in callee analysis
To return accurate type of the expression. i.e. MyType.foo(#^COMPLETE^# // -> (MyType) -> (args...) -> Result MyType().foo(#^COMPLETE^# // -> (args...) -> Result
1 parent 5e25550 commit 63d1ba3

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ void collectPossibleCalleesByQualifiedLookup(
304304

305305
SmallVector<ValueDecl *, 2> decls;
306306
auto resolver = DC.getASTContext().getLazyResolver();
307-
if (!DC.lookupQualified(baseTy, name, NL_QualifiedDefault, resolver, decls))
307+
if (!DC.lookupQualified(baseTy->getMetatypeInstanceType(), name,
308+
NL_QualifiedDefault, resolver, decls))
308309
return;
309310

310311
for (auto *VD : decls) {
@@ -315,13 +316,22 @@ void collectPossibleCalleesByQualifiedLookup(
315316
if (!VD->hasInterfaceType())
316317
continue;
317318
Type declaredMemberType = VD->getInterfaceType();
318-
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(VD))
319-
if (AFD->getDeclContext()->isTypeContext())
319+
if (VD->getDeclContext()->isTypeContext()) {
320+
if (auto *FD = dyn_cast<FuncDecl>(VD)) {
321+
if (!baseTy->is<AnyMetatypeType>())
322+
declaredMemberType =
323+
declaredMemberType->castTo<AnyFunctionType>()->getResult();
324+
}
325+
if (auto *CD = dyn_cast<ConstructorDecl>(VD)) {
326+
if (!baseTy->is<AnyMetatypeType>())
327+
continue;
320328
declaredMemberType =
321329
declaredMemberType->castTo<AnyFunctionType>()->getResult();
330+
}
331+
}
322332

323-
auto fnType =
324-
baseTy->getTypeOfMember(DC.getParentModule(), VD, declaredMemberType);
333+
auto fnType = baseTy->getMetatypeInstanceType()->getTypeOfMember(
334+
DC.getParentModule(), VD, declaredMemberType);
325335

326336
if (!fnType)
327337
continue;
@@ -341,8 +351,8 @@ void collectPossibleCalleesByQualifiedLookup(
341351
DC.getASTContext(), &DC, CompletionTypeCheckKind::Normal, baseExpr, ref);
342352
if (!baseTyOpt)
343353
return;
344-
auto baseTy = (*baseTyOpt)->getRValueType()->getMetatypeInstanceType();
345-
if (!baseTy->mayHaveMembers())
354+
auto baseTy = (*baseTyOpt)->getRValueType();
355+
if (!baseTy->getMetatypeInstanceType()->mayHaveMembers())
346356
return;
347357

348358
collectPossibleCalleesByQualifiedLookup(DC, baseTy, name, candidates);
@@ -387,7 +397,7 @@ bool collectPossibleCalleesForApply(
387397
auto baseTy = AMT->getInstanceType();
388398
if (baseTy->mayHaveMembers())
389399
collectPossibleCalleesByQualifiedLookup(
390-
DC, baseTy, DeclBaseName::createConstructor(), candidates);
400+
DC, AMT, DeclBaseName::createConstructor(), candidates);
391401
}
392402
}
393403

test/IDE/complete_call_arg.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,13 +596,16 @@ func testNestedContext() {
596596

597597
class TestImplicitlyCurriedSelf {
598598
func foo(x: Int) { }
599+
func foo(arg: Int, optArg: Int) { }
600+
599601
static func test() {
600602
foo(#^CURRIED_SELF_1^#
601603
self.foo(#^CURRIED_SELF_2^#
602604
TestImplicitlyCurriedSelf.foo(#^CURRIED_SELF_3^#
603605

604-
// CURRIED_SELF_1: Begin completions, 1 items
605-
// CURRIED_SELF_1-DAG: Pattern/CurrModule: ['(']{#(self): TestImplicitlyCurriedSelf#}[')'][#(Int) -> ()#]{{; name=.+$}}
606+
// CURRIED_SELF_1: Begin completions, 2 items
607+
// CURRIED_SELF_1-DAG: Pattern/CurrModule: ['(']{#(self): TestImplicitlyCurriedSelf#}[')'][#(Int) -> ()#]{{; name=.+$}}
608+
// CURRIED_SELF_1-DAG: Pattern/CurrModule: ['(']{#(self): TestImplicitlyCurriedSelf#}[')'][#(Int, Int) -> ()#]{{; name=.+$}}
606609
// CURRIED_SELF_1: End completions
607610
}
608611
}

0 commit comments

Comments
 (0)