Skip to content

Commit 9257772

Browse files
committed
[Diagnostics] Add a tailored note when passing a closure to a non-closure accepting parameter
1 parent 9071c82 commit 9257772

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,9 @@ ERROR(cannot_convert_argument_value_for_swift_func,none,
388388
NOTE(candidate_has_invalid_argument_at_position,none,
389389
"candidate expects %select{|in-out }2value of type %0 for parameter #%1 (got %3)",
390390
(Type, unsigned, bool, Type))
391+
NOTE(candidate_has_invalid_closure_at_position,none,
392+
"closure passed to parameter of type %0 that does not "
393+
"accept a closure", (Type))
391394

392395
ERROR(cannot_convert_array_to_variadic,none,
393396
"cannot pass array of type %0 as variadic arguments of type %1",

lib/Sema/CSDiagnostics.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7346,10 +7346,15 @@ bool ArgumentMismatchFailure::diagnoseAsError() {
73467346
bool ArgumentMismatchFailure::diagnoseAsNote() {
73477347
auto *locator = getLocator();
73487348
if (auto *callee = getCallee()) {
7349-
emitDiagnosticAt(callee, diag::candidate_has_invalid_argument_at_position,
7350-
getToType(), getParamPosition(),
7351-
locator->isLastElement<LocatorPathElt::LValueConversion>(),
7352-
getFromType());
7349+
if (isExpr<ClosureExpr>(getAnchor())) {
7350+
emitDiagnosticAt(callee, diag::candidate_has_invalid_closure_at_position,
7351+
getToType());
7352+
} else {
7353+
emitDiagnosticAt(callee, diag::candidate_has_invalid_argument_at_position,
7354+
getToType(), getParamPosition(),
7355+
locator->isLastElement<LocatorPathElt::LValueConversion>(),
7356+
getFromType());
7357+
}
73537358
return true;
73547359
}
73557360

test/Constraints/closures.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,3 +1247,14 @@ do {
12471247

12481248

12491249
}
1250+
1251+
do {
1252+
func test(_: Int, _: Int) {}
1253+
// expected-note@-1 {{closure passed to parameter of type 'Int' that does not accept a closure}}
1254+
func test(_: Int, _: String) {}
1255+
// expected-note@-1 {{closure passed to parameter of type 'String' that does not accept a closure}}
1256+
1257+
test(42) { // expected-error {{no exact matches in call to local function 'test'}}
1258+
print($0)
1259+
}
1260+
}

0 commit comments

Comments
 (0)