Skip to content

Commit

Permalink
Fix <rdar://problem/22602657> better diagnostics for closures w/o "in…
Browse files Browse the repository at this point in the history
…" clause

improving the message when too-many arguments are used in a closureexpr with
a known contextual type.
  • Loading branch information
lattner committed Nov 18, 2015
1 parent 8b6d9e9 commit df9d572
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2053,8 +2053,8 @@ NOTE(remove_label_for_tuple_pattern,sema_tcp,none,
ERROR(tuple_pattern_in_non_tuple_context,sema_tcp,none,
"tuple pattern cannot match values of the non-tuple type %0", (Type))
ERROR(closure_argument_list_tuple,sema_tcp,none,
"contextual type for closure argument list expects %0 argument%s0, "
"but %1 were specified", (unsigned, unsigned))
"contextual closure type %0 expects %1 argument%s1, "
"but %2 were used in closure body", (Type, unsigned, unsigned))
ERROR(closure_argument_list_missing,sema_tcp,none,
"contextual type for closure argument list expects %0 argument%s0, "
"which cannot be implicitly ignored", (unsigned))
Expand Down
6 changes: 5 additions & 1 deletion lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4043,8 +4043,12 @@ bool FailureDiagnosis::visitClosureExpr(ClosureExpr *CE) {
return true;
}

// Okay, the wrong number of arguments was used, complain about that.
// Before doing so, strip attributes off the function type so that they
// don't confuse the issue.
fnType = FunctionType::get(fnType->getInput(), fnType->getResult());
diagnose(params->getStartLoc(), diag::closure_argument_list_tuple,
inferredArgCount, actualArgCount);
fnType, inferredArgCount, actualArgCount);
return true;
}

Expand Down
15 changes: 10 additions & 5 deletions test/Constraints/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,24 @@ var _: (Int)-> Int = { 0 }
// expected-error @+1 {{contextual type for closure argument list expects 2 arguments, which cannot be implicitly ignored}} {{28-28=_,_ in }}
var _: (Int, Int)-> Int = {0}

// expected-error @+1 {{contextual type for closure argument list expects 2 arguments, but 3 were specified}}
// expected-error @+1 {{contextual closure type '(Int, Int) -> Int' expects 2 arguments, but 3 were used in closure body}}
var _: (Int,Int)-> Int = {$0+$1+$2}

// expected-error @+1 {{contextual type for closure argument list expects 3 arguments, but 2 were specified}}
// expected-error @+1 {{contextual closure type '(Int, Int, Int) -> Int' expects 3 arguments, but 2 were used in closure body}}
var _: (Int, Int, Int)-> Int = {$0+$1}


var _: ()-> Int = {a in 0}

// expected-error @+1 {{contextual type for closure argument list expects 1 argument, but 2 were specified}}
// expected-error @+1 {{contextual closure type '(Int) -> Int' expects 1 argument, but 2 were used in closure body}}
var _: (Int)-> Int = {a,b in 0}

// expected-error @+1 {{contextual type for closure argument list expects 1 argument, but 3 were specified}}
// expected-error @+1 {{contextual closure type '(Int) -> Int' expects 1 argument, but 3 were used in closure body}}
var _: (Int)-> Int = {a,b,c in 0}

var _: (Int, Int)-> Int = {a in 0}

// expected-error @+1 {{contextual type for closure argument list expects 3 arguments, but 2 were specified}}
// expected-error @+1 {{contextual closure type '(Int, Int, Int) -> Int' expects 3 arguments, but 2 were used in closure body}}
var _: (Int, Int, Int)-> Int = {a, b in a+b}

// <rdar://problem/15998821> Fail to infer types for closure that takes an inout argument
Expand All @@ -157,3 +157,8 @@ func r15998821() {
let g = { x in x = 3 }
take_closure(g)
}

// <rdar://problem/22602657> better diagnostics for closures w/o "in" clause
var _: (Int,Int)-> Int = {$0+$1+$2} // expected-error {{contextual closure type '(Int, Int) -> Int' expects 2 arguments, but 3 were used in closure body}}


2 changes: 1 addition & 1 deletion test/Constraints/diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func f20371273() {
func rdar21078316() {
var foo : [String : String]?
var bar : [(String, String)]?
bar = foo.map { ($0, $1) } // expected-error {{contextual type for closure argument list expects 1 argument, but 2 were specified}}
bar = foo.map { ($0, $1) } // expected-error {{contextual closure type '([String : String]) -> [(String, String)]' expects 1 argument, but 2 were used in closure body}}
}


Expand Down

0 comments on commit df9d572

Please sign in to comment.