Skip to content

Commit 859106e

Browse files
committed
[Diagnostics] Expand trailing closure failure to handle all closure arguments
1 parent 9257772 commit 859106e

File tree

6 files changed

+15
-14
lines changed

6 files changed

+15
-14
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,9 +1550,9 @@ ERROR(extra_argument_to_nullary_call,none,
15501550
ERROR(extra_trailing_closure_in_call,none,
15511551
"extra trailing closure passed in "
15521552
"%select{call|subscript|macro expansion}0", (unsigned))
1553-
ERROR(trailing_closure_bad_param,none,
1554-
"trailing closure passed to parameter of type %0 that does not "
1555-
"accept a closure", (Type))
1553+
ERROR(closure_bad_param,none,
1554+
"%select{|trailing }1closure passed to parameter of type %0 that does not "
1555+
"accept a closure", (Type, bool))
15561556
WARNING(unlabeled_trailing_closure_deprecated,Deprecation,
15571557
"backward matching of the unlabeled trailing closure is deprecated; label the argument with %0 to suppress this warning",
15581558
(Identifier))

lib/Sema/CSDiagnostics.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7292,7 +7292,7 @@ bool ArgumentMismatchFailure::diagnoseAsError() {
72927292
if (diagnoseAttemptedRegexBuilder())
72937293
return true;
72947294

7295-
if (diagnoseTrailingClosureMismatch())
7295+
if (diagnoseClosureMismatch())
72967296
return true;
72977297

72987298
if (diagnoseKeyPathAsFunctionResultMismatch())
@@ -7638,15 +7638,16 @@ bool ArgumentMismatchFailure::diagnoseAttemptedRegexBuilder() const {
76387638
return true;
76397639
}
76407640

7641-
bool ArgumentMismatchFailure::diagnoseTrailingClosureMismatch() const {
7642-
if (!Info.isTrailingClosure())
7641+
bool ArgumentMismatchFailure::diagnoseClosureMismatch() const {
7642+
if (!isExpr<ClosureExpr>(getAnchor()))
76437643
return false;
76447644

76457645
auto paramType = getToType();
76467646
if (paramType->lookThroughAllOptionalTypes()->is<AnyFunctionType>())
76477647
return false;
76487648

7649-
emitDiagnostic(diag::trailing_closure_bad_param, paramType)
7649+
emitDiagnostic(diag::closure_bad_param, paramType,
7650+
Info.isTrailingClosure())
76507651
.highlight(getSourceRange());
76517652

76527653
if (auto overload = getCalleeOverloadChoiceIfAvailable(getLocator())) {

lib/Sema/CSDiagnostics.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,9 +2118,9 @@ class ArgumentMismatchFailure : public ContextualFailure {
21182118
/// or now deprecated `init(initialValue:)`.
21192119
bool diagnosePropertyWrapperMismatch() const;
21202120

2121-
/// Tailored diagnostics for argument mismatches associated with trailing
2121+
/// Tailored diagnostics for argument mismatches associated with (trailing)
21222122
/// closures being passed to non-closure parameters.
2123-
bool diagnoseTrailingClosureMismatch() const;
2123+
bool diagnoseClosureMismatch() const;
21242124

21252125
/// Tailored key path as function diagnostics for argument mismatches where
21262126
/// argument is a keypath expression that has a root type that matches a

test/Constraints/closures.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ struct R_76250381<Result, Failure: Error> {
11531153
func rdar77022842(argA: Bool? = nil, argB: Bool? = nil) {
11541154
if let a = argA ?? false, if let b = argB ?? {
11551155
// expected-error@-1 {{initializer for conditional binding must have Optional type, not 'Bool'}}
1156-
// expected-error@-2 {{cannot convert value of type '() -> ()' to expected argument type 'Bool?'}}
1156+
// expected-error@-2 {{closure passed to parameter of type 'Bool?' that does not accept a closure}}
11571157
// expected-error@-3 {{cannot convert value of type 'Void' to expected condition type 'Bool'}}
11581158
} // expected-error {{expected '{' after 'if' condition}}
11591159
}

test/Sema/property_wrapper_parameter_invalid.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,12 @@ struct ProjectionWrapper<Value> {
262262

263263
func testInvalidWrapperInference() {
264264
struct S<V> {
265-
static func test(_ keyPath: KeyPath<V, String>) {} // expected-note {{'test' declared here}}
265+
static func test(_ keyPath: KeyPath<V, String>) {} // expected-note 2 {{'test' declared here}}
266266
}
267267

268268
// expected-error@+1 {{trailing closure passed to parameter of type 'KeyPath<Int, String>' that does not accept a closure}}
269269
S<Int>.test { $value in }
270-
// expected-error@+1 {{cannot convert value of type '(_) -> ()' to expected argument type 'KeyPath<Int, String>'}}
270+
// expected-error@+1 {{closure passed to parameter of type 'KeyPath<Int, String>' that does not accept a closure}}
271271
S<Int>.test({ $value in })
272272

273273
func testGenericClosure<T>(_ closure: T) {}

test/expr/closure/closures.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ func funcdecl5(_ a: Int, _ y: Int) {
4242
func6({($0) + $0}) // // expected-error {{contextual closure type '(Int, Int) -> Int' expects 2 arguments, but 1 was used in closure body}}
4343

4444

45-
var testfunc : ((), Int) -> Int // expected-note {{'testfunc' declared here}}
45+
var testfunc : ((), Int) -> Int // expected-note 2 {{'testfunc' declared here}}
4646
testfunc({$0+1}) // expected-error {{missing argument for parameter #2 in call}}
47-
// expected-error@-1 {{cannot convert value of type '(Int) -> Int' to expected argument type '()'}}
47+
// expected-error@-1 {{closure passed to parameter of type '()' that does not accept a closure}}
4848

4949
funcdecl5(1, 2) // recursion.
5050

0 commit comments

Comments
 (0)