Skip to content

[CodeCompletion] Context type analysis for default argument initializer #24987

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
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
35 changes: 32 additions & 3 deletions lib/IDE/ExprContextAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,31 @@ class ExprContextAnalyzer {
}
}

void analyzeInitializer(Initializer *initDC) {
switch (initDC->getInitializerKind()) {
case swift::InitializerKind::PatternBinding: {
auto initDC = cast<PatternBindingInitializer>(DC);
auto PBD = initDC->getBinding();
if (!PBD)
break;
auto pat = PBD->getPattern(initDC->getBindingIndex());
if (pat->hasType())
recordPossibleType(pat->getType());
break;
}
case InitializerKind::DefaultArgument: {
auto initDC = cast<DefaultArgumentInitializer>(DC);
auto AFD = dyn_cast<AbstractFunctionDecl>(initDC->getParent());
if (!AFD)
return;
auto param = AFD->getParameters()->get(initDC->getIndex());
if (param->hasInterfaceType())
recordPossibleType(AFD->mapTypeIntoContext(param->getInterfaceType()));
break;
}
}
}

/// Whether the given \c BraceStmt, which must be the body of a function or
/// closure, should be treated as a single-expression return for the purposes
/// of code-completion.
Expand Down Expand Up @@ -808,13 +833,17 @@ class ExprContextAnalyzer {
return false;
});

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

if (Finder.Ancestors.empty())
if (Finder.Ancestors.empty()) {
// There's no parent context in DC. But still, the parent of the
// initializer might constrain the initializer's type.
if (auto initDC = dyn_cast<Initializer>(DC))
analyzeInitializer(initDC);
return;
}

auto &P = Finder.Ancestors.back();
if (auto Parent = P.getAsExpr()) {
Expand Down
8 changes: 6 additions & 2 deletions test/IDE/complete_default_arguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
// RUN: %FileCheck %s -check-prefix=NEGATIVE_DEFAULT_ARGS_9 < %t
//
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEFAULT_ARG_INIT_1 | %FileCheck %s -check-prefix=DEFAULT_ARG_INIT
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEFAULT_ARG_INIT_2 | %FileCheck %s -check-prefix=DEFAULT_ARG_INIT
// 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
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEFAULT_ARG_INIT_3 | %FileCheck %s -check-prefix=DEFAULT_ARG_INIT
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEFAULT_ARG_INIT_4 | %FileCheck %s -check-prefix=DEFAULT_ARG_INIT
// 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

func freeFuncWithDefaultArgs1(
_ a: Int, b: Int = 42, file: String = #file, line: Int = #line,
Expand Down Expand Up @@ -140,3 +140,7 @@ func testDefaultArgInit4(_ x: Int = #^DEFAULT_ARG_INIT_4^#) { }
// DEFAULT_ARG_INIT: Begin completions
// DEFAULT_ARG_INIT: Decl[GlobalVar]/CurrModule: globalVar[#Int#]{{; name=.+$}}
// DEFAULT_ARG_INIT: End completions

// DEFAULT_ARG_INIT_INTCONTEXT: Begin completions
// DEFAULT_ARG_INIT_INTCONTEXT: Decl[GlobalVar]/CurrModule/TypeRelation[Identical]: globalVar[#Int#]{{; name=.+$}}
// DEFAULT_ARG_INIT_INTCONTEXT: End completions
9 changes: 9 additions & 0 deletions test/IDE/complete_unresolved_members.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GENERICPARAM_21 | %FileCheck %s -check-prefix=GENERICPARAM_1

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

enum SomeEnum1 {
case South
Expand Down Expand Up @@ -683,3 +686,9 @@ func testingGenericParam2<X>(obj: C<X>) {
struct TestingStruct {
var value: SomeEnum1 = .#^DECL_MEMBER_INIT_1^#
}

func testDefaultArgument(arg: SomeEnum1 = .#^DEFAULT_ARG_1^#) {}
class TestDefalutArg {
func method(arg: SomeEnum1 = .#^DEFAULT_ARG_2^#) {}
init(arg: SomeEnum1 = .#^DEFAULT_ARG_3^#) {}
}