Skip to content

[code-complete] Don't suggest trailing closure for autoclosure #13696

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
11 changes: 8 additions & 3 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1546,9 +1546,14 @@ static bool hasTrivialTrailingClosure(const FuncDecl *FD,
OneArg = (NonDefault == 0);
}

if (OneArg)
if (auto Fn = funcType->getParams().back().getType()->getAs<AnyFunctionType>())
return Fn->getInput()->isVoid() && Fn->getResult()->isVoid();
if (OneArg) {
auto param = funcType->getParams().back();
if (!param.isAutoClosure()) {
if (auto Fn = param.getType()->getAs<AnyFunctionType>()) {
return Fn->getInput()->isVoid() && Fn->getResult()->isVoid();
}
}
}

return false;
}
Expand Down
3 changes: 3 additions & 0 deletions test/IDE/complete_trailing_closure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ struct S {
func method1(_: ()->()) {}
static func method2(_: ()->()) {}
func nonTrivial1(_: (Int)->()) {}
func nonTrivial2(_: @autoclosure ()->()) {}
func test2() {
self.#^METHOD_1^#
}
// METHOD_1: Begin completions
// METHOD_1: Decl[InstanceMethod]/CurrNominal: method1 {|}[#Void#]
// METHOD_1: Decl[InstanceMethod]/CurrNominal: method1({#() -> ()##() -> ()#})[#Void#]
// METHOD_1: Decl[InstanceMethod]/CurrNominal: nonTrivial1({#(Int) -> ()##(Int) -> ()#})[#Void#]
// METHOD_1: Decl[InstanceMethod]/CurrNominal: nonTrivial2({#()#})[#Void#]
// METHOD_1: End completions

func test3() {
Expand All @@ -85,6 +87,7 @@ class C {
func method1(_: ()->()) {}
class func method2(_: ()->()) {}
func nonTrivial1(_: (Int)->()) {}
func nonTrivial2(_: @autoclosure ()->()) {}
func test6() {
self.#^METHOD_4^#
}
Expand Down