Skip to content

Commit 1d20392

Browse files
committed
[Completion] Fix trailing closure index check
`Res.FirstTrailingClosureIndex` is an index into the argument list, so comparing it against a parameter index is wrong. Instead, compare it against the argument index of the completion token, which is what we want to be checking. This ensures we don't try and offer argument completions for non-function default arguments. rdar://127760308
1 parent 8f433ee commit 1d20392

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

lib/IDE/ArgumentCompletion.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ bool ArgumentTypeCheckCompletionCallback::addPossibleParams(
5050
const AnyFunctionType::Param *TypeParam = &ParamsToPass[Idx];
5151
bool Required = !Res.DeclParamIsOptional[Idx];
5252

53-
if (Res.FirstTrailingClosureIndex && Idx > *Res.FirstTrailingClosureIndex &&
53+
if (Res.FirstTrailingClosureIndex &&
54+
Res.ArgIdx > *Res.FirstTrailingClosureIndex &&
5455
!TypeParam->getPlainType()
5556
->lookThroughAllOptionalTypes()
5657
->is<AnyFunctionType>()) {

test/IDE/complete_multiple_trailingclosure.swift

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,40 @@ func testGlobalFunc() {
1818
// GLOBALFUNC_AFTERLABEL-NOT: Begin completions
1919
}
2020

21+
struct S {
22+
func member() {}
23+
}
24+
func globalFunc2(x: Int = 0, _ fn: () -> Void) -> S {}
25+
func globalFunc3(x: Int, _ fn: () -> Void) -> S {}
26+
27+
func testNonFuncArg() {
28+
do {
29+
// Don't complete for a non-function default argument.
30+
globalFunc2 {} #^GLOBALFUNC2^#
31+
// GLOBALFUNC2: Begin completions, 2 items
32+
// GLOBALFUNC2-DAG: Decl[InstanceMethod]/CurrNominal: .member()[#Void#]; name=member()
33+
// GLOBALFUNC2-DAG: Keyword[self]/CurrNominal: .self[#S#]; name=self
34+
35+
globalFunc2 {}
36+
.#^GLOBALFUNC2_DOT^#
37+
// GLOBALFUNC2_DOT: Begin completions, 2 items
38+
// GLOBALFUNC2_DOT-DAG: Decl[InstanceMethod]/CurrNominal: member()[#Void#]; name=member()
39+
// GLOBALFUNC2_DOT-DAG: Keyword[self]/CurrNominal: self[#S#]; name=self
40+
}
41+
do {
42+
globalFunc3 {} #^GLOBALFUNC3^#
43+
// GLOBALFUNC3: Begin completions, 2 items
44+
// GLOBALFUNC3-DAG: Decl[InstanceMethod]/CurrNominal: .member()[#Void#]; name=member()
45+
// GLOBALFUNC3-DAG: Keyword[self]/CurrNominal: .self[#S#]; name=self
46+
47+
globalFunc3 {}
48+
.#^GLOBALFUNC3_DOT^#
49+
// GLOBALFUNC3_DOT: Begin completions, 2 items
50+
// GLOBALFUNC3_DOT-DAG: Decl[InstanceMethod]/CurrNominal: member()[#Void#]; name=member()
51+
// GLOBALFUNC3_DOT-DAG: Keyword[self]/CurrNominal: self[#S#]; name=self
52+
}
53+
}
54+
2155
struct SimpleEnum {
2256
case foo, bar
2357

@@ -182,10 +216,9 @@ func testFallbackPostfix() {
182216
let _ = MyStruct4 {
183217
1
184218
} #^INIT_FALLBACK_1^#
185-
// INIT_FALLBACK_1: Begin completions, 3 items
219+
// INIT_FALLBACK_1: Begin completions, 2 items
186220
// INIT_FALLBACK_1-DAG: Keyword[self]/CurrNominal: .self[#MyStruct4<Int>#]; name=self
187221
// INIT_FALLBACK_1-DAG: Decl[InstanceMethod]/CurrNominal/TypeRelation[Invalid]: .testStructMethod()[#Void#]; name=testStructMethod()
188-
// INIT_FALLBACK_1-DAG: Pattern/Local/Flair[ArgLabels]: {#arg1: Int#}[#Int#]; name=arg1:
189222
let _ = MyStruct4(name: "test") {
190223
""
191224
} arg3: {

0 commit comments

Comments
 (0)