Skip to content

[CodeCompletion] nil/[]/[:] default arguments are "interesting" #40079

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
Nov 9, 2021
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
9 changes: 8 additions & 1 deletion lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2739,10 +2739,17 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
for (auto param : *func->getParameters()) {
switch (param->getDefaultArgumentKind()) {
case DefaultArgumentKind::Normal:
case DefaultArgumentKind::NilLiteral:
case DefaultArgumentKind::EmptyArray:
case DefaultArgumentKind::EmptyDictionary:
case DefaultArgumentKind::StoredProperty:
case DefaultArgumentKind::Inherited: // FIXME: include this?
return true;
default:
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice. Hopefully this will help in the future :)


case DefaultArgumentKind::None:
#define MAGIC_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
case DefaultArgumentKind::NAME:
#include "swift/AST/MagicIdentifierKinds.def"
break;
}
}
Expand Down
22 changes: 21 additions & 1 deletion test/IDE/complete_default_arguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEFAULT_ARGS_9 > %t
// RUN: %FileCheck %s -check-prefix=DEFAULT_ARGS_9 < %t
// RUN: %FileCheck %s -check-prefix=NEGATIVE_DEFAULT_ARGS_9 < %t
//

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

// 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_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_INTCONTEXT
Expand All @@ -28,6 +30,9 @@ func freeFuncWithDefaultArgs1(
func freeFuncWithDefaultArgs2(file: String = #file) {}
func freeFuncWithDefaultArgs3(a: Int = 0) {}
func freeFuncWithDefaultArgs4(_ a: Int, b: Int = 0, c: Int = 0) {}
func freeFuncWithDefaultArgs5(a: Int? = nil) {}
func freeFuncWithDefaultArgs6(a: [Int] = []) {}
func freeFuncWithDefaultArgs7(a: [Int:Int] = [:]) {}

struct A {
func methodWithDefaultArgs1(a: Int = 0) {}
Expand Down Expand Up @@ -60,6 +65,12 @@ func testDefaultArgs1() {
// DEFAULT_ARGS_1-DAG: Decl[FreeFunction]/CurrModule: freeFuncWithDefaultArgs3({#a: Int#})[#Void#]{{; name=.+$}}
// DEFAULT_ARGS_1-DAG: Decl[FreeFunction]/CurrModule: freeFuncWithDefaultArgs4({#(a): Int#})[#Void#]{{; name=.+$}}
// DEFAULT_ARGS_1-DAG: Decl[FreeFunction]/CurrModule: freeFuncWithDefaultArgs4({#(a): Int#}, {#b: Int#}, {#c: Int#})[#Void#]{{; name=.+$}}
// DEFAULT_ARGS_1-DAG: Decl[FreeFunction]/CurrModule: freeFuncWithDefaultArgs5()[#Void#]{{; name=.+$}}
// DEFAULT_ARGS_1-DAG: Decl[FreeFunction]/CurrModule: freeFuncWithDefaultArgs5({#a: Int?#})[#Void#]{{; name=.+$}}
// DEFAULT_ARGS_1-DAG: Decl[FreeFunction]/CurrModule: freeFuncWithDefaultArgs6()[#Void#]{{; name=.+$}}
// DEFAULT_ARGS_1-DAG: Decl[FreeFunction]/CurrModule: freeFuncWithDefaultArgs6({#a: [Int]#})[#Void#]{{; name=.+$}}
// DEFAULT_ARGS_1-DAG: Decl[FreeFunction]/CurrModule: freeFuncWithDefaultArgs7()[#Void#]{{; name=.+$}}
// DEFAULT_ARGS_1-DAG: Decl[FreeFunction]/CurrModule: freeFuncWithDefaultArgs7({#a: [Int : Int]#})[#Void#]{{; name=.+$}}
// DEFAULT_ARGS_1: End completions

func testDefaultArgs2() {
Expand Down Expand Up @@ -131,6 +142,15 @@ func testDefaultArgs9(_ x: C2) {
// DEFAULT_ARGS_9: End completions
// NEGATIVE_DEFAULT_ARGS_9-NOT: methodWithDefaultArgs1()

func testDefaultArgs10() {
freeFuncWithDefaultArgs5(#^DEFAULT_ARGS_10^#)
// DEFAULT_ARGS_10: Begin completions
// DEFAULT_ARGS_10-DAG: Decl[FreeFunction]/CurrModule/Flair[ArgLabels]: ['('][')'][#Void#]; name=
// DEFAULT_ARGS_10-DAG: Decl[FreeFunction]/CurrModule/Flair[ArgLabels]: ['(']{#a: Int?#}[')'][#Void#]; name=a:
// DEFAULT_ARGS_10: End completions

}

let globalVar = 1
func testDefaultArgInit1(x = #^DEFAULT_ARG_INIT_1^#) { }
func testDefaultArgInit2(_: Int = #^DEFAULT_ARG_INIT_2^#) { }
Expand Down