Skip to content

Commit 72c4e7f

Browse files
authored
Merge pull request #25541 from rintaro/5.1-ide-completion-defaultinit-rdar51037538
[5.1][CodeCompletion] Context type analysis for default argument initializer
2 parents e298121 + 6df3649 commit 72c4e7f

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,31 @@ class ExprContextAnalyzer {
791791
}
792792
}
793793

794+
void analyzeInitializer(Initializer *initDC) {
795+
switch (initDC->getInitializerKind()) {
796+
case swift::InitializerKind::PatternBinding: {
797+
auto initDC = cast<PatternBindingInitializer>(DC);
798+
auto PBD = initDC->getBinding();
799+
if (!PBD)
800+
break;
801+
auto pat = PBD->getPattern(initDC->getBindingIndex());
802+
if (pat->hasType())
803+
recordPossibleType(pat->getType());
804+
break;
805+
}
806+
case InitializerKind::DefaultArgument: {
807+
auto initDC = cast<DefaultArgumentInitializer>(DC);
808+
auto AFD = dyn_cast<AbstractFunctionDecl>(initDC->getParent());
809+
if (!AFD)
810+
return;
811+
auto param = AFD->getParameters()->get(initDC->getIndex());
812+
if (param->hasInterfaceType())
813+
recordPossibleType(AFD->mapTypeIntoContext(param->getInterfaceType()));
814+
break;
815+
}
816+
}
817+
}
818+
794819
/// Whether the given \c BraceStmt, which must be the body of a function or
795820
/// closure, should be treated as a single-expression return for the purposes
796821
/// of code-completion.
@@ -885,13 +910,17 @@ class ExprContextAnalyzer {
885910
return false;
886911
});
887912

888-
// For 'Initializer' context, we need to look into its parent because it
889-
// might constrain the initializer's type.
913+
// For 'Initializer' context, we need to look into its parent.
890914
auto analyzeDC = isa<Initializer>(DC) ? DC->getParent() : DC;
891915
analyzeDC->walkContext(Finder);
892916

893-
if (Finder.Ancestors.empty())
917+
if (Finder.Ancestors.empty()) {
918+
// There's no parent context in DC. But still, the parent of the
919+
// initializer might constrain the initializer's type.
920+
if (auto initDC = dyn_cast<Initializer>(DC))
921+
analyzeInitializer(initDC);
894922
return;
923+
}
895924

896925
auto &P = Finder.Ancestors.back();
897926
if (auto Parent = P.getAsExpr()) {

test/IDE/complete_default_arguments.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
// RUN: %FileCheck %s -check-prefix=NEGATIVE_DEFAULT_ARGS_9 < %t
1919
//
2020
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEFAULT_ARG_INIT_1 | %FileCheck %s -check-prefix=DEFAULT_ARG_INIT
21-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEFAULT_ARG_INIT_2 | %FileCheck %s -check-prefix=DEFAULT_ARG_INIT
21+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEFAULT_ARG_INIT_2 | %FileCheck %s -check-prefix=DEFAULT_ARG_INIT_INTCONTEXT
2222
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEFAULT_ARG_INIT_3 | %FileCheck %s -check-prefix=DEFAULT_ARG_INIT
23-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEFAULT_ARG_INIT_4 | %FileCheck %s -check-prefix=DEFAULT_ARG_INIT
23+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEFAULT_ARG_INIT_4 | %FileCheck %s -check-prefix=DEFAULT_ARG_INIT_INTCONTEXT
2424

2525
func freeFuncWithDefaultArgs1(
2626
_ a: Int, b: Int = 42, file: String = #file, line: Int = #line,
@@ -140,3 +140,7 @@ func testDefaultArgInit4(_ x: Int = #^DEFAULT_ARG_INIT_4^#) { }
140140
// DEFAULT_ARG_INIT: Begin completions
141141
// DEFAULT_ARG_INIT: Decl[GlobalVar]/CurrModule: globalVar[#Int#]{{; name=.+$}}
142142
// DEFAULT_ARG_INIT: End completions
143+
144+
// DEFAULT_ARG_INIT_INTCONTEXT: Begin completions
145+
// DEFAULT_ARG_INIT_INTCONTEXT: Decl[GlobalVar]/CurrModule/TypeRelation[Identical]: globalVar[#Int#]{{; name=.+$}}
146+
// DEFAULT_ARG_INIT_INTCONTEXT: End completions

test/IDE/complete_unresolved_members.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@
103103
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GENERICPARAM_21 | %FileCheck %s -check-prefix=GENERICPARAM_1
104104

105105
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DECL_MEMBER_INIT_1 | %FileCheck %s -check-prefix=UNRESOLVED_3
106+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEFAULT_ARG_1 | %FileCheck %s -check-prefix=UNRESOLVED_3
107+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEFAULT_ARG_2 | %FileCheck %s -check-prefix=UNRESOLVED_3
108+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEFAULT_ARG_3 | %FileCheck %s -check-prefix=UNRESOLVED_3
106109

107110
enum SomeEnum1 {
108111
case South
@@ -683,3 +686,9 @@ func testingGenericParam2<X>(obj: C<X>) {
683686
struct TestingStruct {
684687
var value: SomeEnum1 = .#^DECL_MEMBER_INIT_1^#
685688
}
689+
690+
func testDefaultArgument(arg: SomeEnum1 = .#^DEFAULT_ARG_1^#) {}
691+
class TestDefalutArg {
692+
func method(arg: SomeEnum1 = .#^DEFAULT_ARG_2^#) {}
693+
init(arg: SomeEnum1 = .#^DEFAULT_ARG_3^#) {}
694+
}

0 commit comments

Comments
 (0)