Skip to content

Commit 97d7111

Browse files
committed
[CodeCompletion] Clean-up result build for method/function all
Don't emit duplicate results for implicitly curried instance methods with defaulted parameters.
1 parent 489a538 commit 97d7111

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,36 +2315,27 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
23152315
if (FD->getName().empty())
23162316
return;
23172317
foundFunction(FD);
2318-
bool IsImplicitlyCurriedInstanceMethod =
2319-
isImplicitlyCurriedInstanceMethod(FD);
23202318

23212319
StringRef Name = FD->getName().get();
23222320
assert(!Name.empty() && "name should not be empty");
23232321

23242322
Type FunctionType = getTypeOfMember(FD);
23252323
assert(FunctionType);
23262324

2327-
unsigned NumParamLists;
2325+
auto AFT = FunctionType->getAs<AnyFunctionType>();
2326+
2327+
bool IsImplicitlyCurriedInstanceMethod = false;
23282328
if (FD->hasImplicitSelfDecl()) {
2329-
if (IsImplicitlyCurriedInstanceMethod)
2330-
NumParamLists = 2;
2331-
else {
2332-
NumParamLists = 1;
2329+
IsImplicitlyCurriedInstanceMethod = isImplicitlyCurriedInstanceMethod(FD);
23332330

2334-
// Strip off 'self'
2335-
if (FunctionType->is<AnyFunctionType>())
2336-
FunctionType = FunctionType->castTo<AnyFunctionType>()->getResult();
2337-
}
2338-
} else {
2339-
NumParamLists = 1;
2331+
// Strip off '(_ self: Self)' if needed.
2332+
if (AFT && !IsImplicitlyCurriedInstanceMethod)
2333+
AFT = AFT->getResult()->getAs<AnyFunctionType>();
23402334
}
23412335

23422336
bool trivialTrailingClosure = false;
2343-
if (!IsImplicitlyCurriedInstanceMethod &&
2344-
FunctionType->is<AnyFunctionType>()) {
2345-
trivialTrailingClosure = hasTrivialTrailingClosure(
2346-
FD, FunctionType->castTo<AnyFunctionType>());
2347-
}
2337+
if (AFT && !IsImplicitlyCurriedInstanceMethod)
2338+
trivialTrailingClosure = hasTrivialTrailingClosure(FD, AFT);
23482339

23492340
// Add the method, possibly including any default arguments.
23502341
auto addMethodImpl = [&](bool includeDefaultArgs = true,
@@ -2364,14 +2355,13 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
23642355

23652356
llvm::SmallString<32> TypeStr;
23662357

2367-
if (!FunctionType->is<AnyFunctionType>()) {
2358+
if (!AFT) {
23682359
llvm::raw_svector_ostream OS(TypeStr);
23692360
FunctionType.print(OS);
23702361
Builder.addTypeAnnotation(OS.str());
23712362
return;
23722363
}
23732364

2374-
auto AFT = FunctionType->castTo<AnyFunctionType>();
23752365
if (IsImplicitlyCurriedInstanceMethod) {
23762366
Builder.addLeftParen();
23772367
addCallArgumentPatterns(Builder, AFT->getParams(),
@@ -2380,6 +2370,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
23802370
Builder.addRightParen();
23812371
} else if (trivialTrailingClosure) {
23822372
Builder.addBraceStmtWithCursor(" { code }");
2373+
addThrows(Builder, AFT, FD);
23832374
} else {
23842375
Builder.addLeftParen();
23852376
addCallArgumentPatterns(Builder, AFT, FD->getParameters(),
@@ -2393,11 +2384,12 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
23932384
// Build type annotation.
23942385
{
23952386
llvm::raw_svector_ostream OS(TypeStr);
2396-
for (unsigned i = 0; i < NumParamLists - 1; ++i) {
2387+
if (IsImplicitlyCurriedInstanceMethod) {
23972388
ResultType->castTo<AnyFunctionType>()->printParams(OS);
23982389
ResultType = ResultType->castTo<AnyFunctionType>()->getResult();
23992390
OS << " -> ";
24002391
}
2392+
24012393
// What's left is the result type.
24022394
if (ResultType->isVoid()) {
24032395
OS << "Void";
@@ -2417,15 +2409,16 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
24172409
Builder.addTypeAnnotation(TypeStr);
24182410
};
24192411

2420-
if (FunctionType->is<AnyFunctionType>() &&
2421-
hasInterestingDefaultValues(FD)) {
2422-
addMethodImpl(/*includeDefaultArgs*/ false);
2423-
}
2424-
if (trivialTrailingClosure) {
2425-
addMethodImpl(/*includeDefaultArgs=*/false,
2426-
/*trivialTrailingClosure=*/true);
2412+
if (!AFT || IsImplicitlyCurriedInstanceMethod) {
2413+
addMethodImpl();
2414+
} else {
2415+
if (trivialTrailingClosure)
2416+
addMethodImpl(/*includeDefaultArgs=*/false,
2417+
/*trivialTrailingClosure=*/true);
2418+
if (hasInterestingDefaultValues(FD))
2419+
addMethodImpl(/*includeDefaultArgs=*/false);
2420+
addMethodImpl(/*includeDefaultArgs=*/true);
24272421
}
2428-
addMethodImpl();
24292422
}
24302423

24312424
void addConstructorCall(const ConstructorDecl *CD, DeclVisibilityKind Reason,

test/IDE/complete_trailing_closure.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func test1() {
4141
// GLOBAL_1: Begin completions
4242
// GLOBAL_1-DAG: Decl[FreeFunction]/CurrModule: global1 {|}[#Void#]
4343
// GLOBAL_1-DAG: Decl[FreeFunction]/CurrModule: global2 {|}[#Void#]
44-
// GLOBAL_1-DAG: Decl[FreeFunction]/CurrModule: global3 {|}[#Void#]
44+
// GLOBAL_1-DAG: Decl[FreeFunction]/CurrModule: global3 {|}[' rethrows'][#Void#]
4545
// GLOBAL_1-DAG: Decl[FreeFunction]/CurrModule: global4 {|}[#Void#]
4646
// GLOBAL_1-DAG: Decl[FreeFunction]/CurrModule: global1({#() -> ()##() -> ()#})[#Void#]
4747
// GLOBAL_1-DAG: Decl[FreeFunction]/CurrModule: global2({#label: () -> ()##() -> ()#})[#Void#]

test/IDE/complete_value_expr.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ struct FooStruct {
216216
func instanceFunc8(_ a: (Int, Int)) {}
217217
mutating
218218
func instanceFunc9(a: @autoclosure () -> Int) {}
219+
mutating
220+
func instanceFunc10(arg: Int, optArg: Int = 0) {}
219221

220222
mutating
221223
func varargInstanceFunc0(_ v: Int...) {}
@@ -334,6 +336,8 @@ var fooObject: FooStruct
334336
// FOO_OBJECT_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: instanceFunc7({#a: Int#})[#Void#]{{; name=.+$}}
335337
// FOO_OBJECT_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: instanceFunc8({#(a): (Int, Int)#})[#Void#]{{; name=.+$}}
336338
// FOO_OBJECT_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: instanceFunc9({#a: Int#})[#Void#]{{; name=.+$}}
339+
// FOO_OBJECT_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: instanceFunc10({#arg: Int#})[#Void#]{{; name=.+$}}
340+
// FOO_OBJECT_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: instanceFunc10({#arg: Int#}, {#optArg: Int#})[#Void#]{{; name=.+$}}
337341
//
338342
// FOO_OBJECT_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: varargInstanceFunc0({#(v): Int...#})[#Void#]{{; name=.+$}}
339343
// FOO_OBJECT_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: varargInstanceFunc1({#(a): Float#}, {#v: Int...#})[#Void#]{{; name=.+$}}
@@ -367,6 +371,8 @@ var fooObject: FooStruct
367371
// FOO_OBJECT_NO_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: .instanceFunc7({#a: Int#})[#Void#]{{; name=.+$}}
368372
// FOO_OBJECT_NO_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: .instanceFunc8({#(a): (Int, Int)#})[#Void#]{{; name=.+$}}
369373
// FOO_OBJECT_NO_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: .instanceFunc9({#a: Int#})[#Void#]{{; name=.+$}}
374+
// FOO_OBJECT_NO_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: .instanceFunc10({#arg: Int#})[#Void#]{{; name=.+$}}
375+
// FOO_OBJECT_NO_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: .instanceFunc10({#arg: Int#}, {#optArg: Int#})[#Void#]{{; name=.+$}}
370376
//
371377
// FOO_OBJECT_NO_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: .varargInstanceFunc0({#(v): Int...#})[#Void#]{{; name=.+$}}
372378
// FOO_OBJECT_NO_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: .varargInstanceFunc1({#(a): Float#}, {#v: Int...#})[#Void#]{{; name=.+$}}
@@ -404,6 +410,7 @@ var fooObject: FooStruct
404410
// FOO_STRUCT_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: instanceFunc7({#(self): &FooStruct#})[#(a: Int) -> Void#]{{; name=.+$}}
405411
// FOO_STRUCT_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: instanceFunc8({#(self): &FooStruct#})[#((Int, Int)) -> Void#]{{; name=.+$}}
406412
// FOO_STRUCT_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: instanceFunc9({#(self): &FooStruct#})[#(a: @autoclosure () -> Int) -> Void#]{{; name=.+$}}
413+
// FOO_STRUCT_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: instanceFunc10({#(self): &FooStruct#})[#(arg: Int, optArg: Int) -> Void#]{{; name=.+$}}
407414
// FOO_STRUCT_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: varargInstanceFunc0({#(self): &FooStruct#})[#(Int...) -> Void#]{{; name=.+$}}
408415
// FOO_STRUCT_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: varargInstanceFunc1({#(self): &FooStruct#})[#(Float, v: Int...) -> Void#]{{; name=.+$}}
409416
// FOO_STRUCT_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: varargInstanceFunc2({#(self): &FooStruct#})[#(Float, b: Double, v: Int...) -> Void#]{{; name=.+$}}
@@ -451,6 +458,7 @@ var fooObject: FooStruct
451458
// FOO_STRUCT_NO_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: .instanceFunc7({#(self): &FooStruct#})[#(a: Int) -> Void#]{{; name=.+$}}
452459
// FOO_STRUCT_NO_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: .instanceFunc8({#(self): &FooStruct#})[#((Int, Int)) -> Void#]{{; name=.+$}}
453460
// FOO_STRUCT_NO_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: .instanceFunc9({#(self): &FooStruct#})[#(a: @autoclosure () -> Int) -> Void#]{{; name=.+$}}
461+
// FOO_STRUCT_NO_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: .instanceFunc10({#(self): &FooStruct#})[#(arg: Int, optArg: Int) -> Void#]{{; name=.+$}}
454462
// FOO_STRUCT_NO_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: .varargInstanceFunc0({#(self): &FooStruct#})[#(Int...) -> Void#]{{; name=.+$}}
455463
// FOO_STRUCT_NO_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: .varargInstanceFunc1({#(self): &FooStruct#})[#(Float, v: Int...) -> Void#]{{; name=.+$}}
456464
// FOO_STRUCT_NO_DOT-NEXT: Decl[InstanceMethod]/CurrNominal: .varargInstanceFunc2({#(self): &FooStruct#})[#(Float, b: Double, v: Int...) -> Void#]{{; name=.+$}}
@@ -1933,6 +1941,7 @@ func testThrows003(_ x: HasThrowingMembers) {
19331941
x.#^MEMBER_THROWS1^#
19341942
// MEMBER_THROWS1: Begin completions
19351943
// MEMBER_THROWS1-DAG: Decl[InstanceMethod]/CurrNominal: memberThrows()[' throws'][#Void#]
1944+
// MEMBER_THROWS1-DAG: Decl[InstanceMethod]/CurrNominal: memberRethrows {|}[' rethrows'][#Void#]
19361945
// MEMBER_THROWS1-DAG: Decl[InstanceMethod]/CurrNominal: memberRethrows({#(x): () throws -> ()##() throws -> ()#})[' rethrows'][#Void#]
19371946
// MEMBER_THROWS1: End completions
19381947
}

0 commit comments

Comments
 (0)