Skip to content

[6.2] Sema: Never record argument label mismatches for unlabeled trailing closures #82156

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
7 changes: 7 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,13 @@ static bool matchCallArgumentsImpl(
assert(argIdx != numArgs && "Must have a valid index to claim");
assert(!claimedArgs[argIdx] && "Argument already claimed");

// Prevent recording of an argument label mismatche for an unlabeled
// trailing closure. An unlabeled trailing closure is necessarily the first
// one and vice versa, per language syntax.
if (unlabeledTrailingClosureArgIndex == argIdx) {
expectedName = Identifier();
}

if (!actualArgNames.empty()) {
// We're recording argument names; record this one.
actualArgNames[argIdx] = expectedName;
Expand Down
4 changes: 3 additions & 1 deletion lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2697,8 +2697,10 @@ bool swift::diagnoseArgumentLabelError(ASTContext &ctx,
}
}

assert((numMissing + numExtra + numWrong > 0) &&
"Should not call this function with nothing to diagnose");

// Emit the diagnostic.
assert(numMissing > 0 || numExtra > 0 || numWrong > 0);
llvm::SmallString<16> haveBuffer; // note: diagOpt has references to this
llvm::SmallString<16> expectedBuffer; // note: diagOpt has references to this

Expand Down
36 changes: 36 additions & 0 deletions test/expr/closure/trailing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -488,3 +488,39 @@ func rdar92521618() {
if let _ = { foo {} }() {}
guard let _ = { foo {} }() else { return }
}

// Argument matching never binds trailing closure arguments to
// defaulted/variadic parameters of non-function type.
do {
// Trailing closure not considered fulfilled by 'arg'.
// Note: Used to crash.
do {
func variadic(arg: Int...) {} // expected-note@:10 {{'variadic(arg:)' declared here}}{{none}}
func defaulted(arg: Int = 0) {}

let _ = variadic { return () }
// expected-error@-1:22 {{trailing closure passed to parameter of type 'Int' that does not accept a closure}}{{none}}
let _ = defaulted { return () }
// expected-error@-1:23 {{extra trailing closure passed in call}}{{none}}
}
// Trailing closure considered fulfilled by 'x' instead of 'arg'.
do {
func variadic(arg: Int..., x: String) {} // expected-note@:10 {{'variadic(arg:x:)' declared here}}{{none}}
func defaulted(arg: Int = 0, x: String) {} // expected-note@:10 {{'defaulted(arg:x:)' declared here}}{{none}}

let _ = variadic { return () }
// expected-error@-1:22 {{trailing closure passed to parameter of type 'String' that does not accept a closure}}{{none}}
let _ = defaulted { return () }
// expected-error@-1:23 {{trailing closure passed to parameter of type 'String' that does not accept a closure}}{{none}}
}
// Trailing closure considered fulfilled by 'arg'; has function type.
do {
func variadic(arg: ((Int) -> Void)...) {}
func defaulted(arg: ((Int) -> Void) = { _ in }) {}

let _ = variadic { return () }
// expected-error@-1:22 {{contextual type for closure argument list expects 1 argument, which cannot be implicitly ignored}}{{23-23= _ in}}
let _ = defaulted { return () }
// expected-error@-1:23 {{contextual type for closure argument list expects 1 argument, which cannot be implicitly ignored}}{{24-24= _ in}}
}
}