Skip to content

Commit df9d572

Browse files
committed
Fix <rdar://problem/22602657> better diagnostics for closures w/o "in" clause
improving the message when too-many arguments are used in a closureexpr with a known contextual type.
1 parent 8b6d9e9 commit df9d572

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,8 +2053,8 @@ NOTE(remove_label_for_tuple_pattern,sema_tcp,none,
20532053
ERROR(tuple_pattern_in_non_tuple_context,sema_tcp,none,
20542054
"tuple pattern cannot match values of the non-tuple type %0", (Type))
20552055
ERROR(closure_argument_list_tuple,sema_tcp,none,
2056-
"contextual type for closure argument list expects %0 argument%s0, "
2057-
"but %1 were specified", (unsigned, unsigned))
2056+
"contextual closure type %0 expects %1 argument%s1, "
2057+
"but %2 were used in closure body", (Type, unsigned, unsigned))
20582058
ERROR(closure_argument_list_missing,sema_tcp,none,
20592059
"contextual type for closure argument list expects %0 argument%s0, "
20602060
"which cannot be implicitly ignored", (unsigned))

lib/Sema/CSDiag.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4043,8 +4043,12 @@ bool FailureDiagnosis::visitClosureExpr(ClosureExpr *CE) {
40434043
return true;
40444044
}
40454045

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

test/Constraints/closures.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,24 @@ var _: (Int)-> Int = { 0 }
115115
// expected-error @+1 {{contextual type for closure argument list expects 2 arguments, which cannot be implicitly ignored}} {{28-28=_,_ in }}
116116
var _: (Int, Int)-> Int = {0}
117117

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

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

124124

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

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

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

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

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

138138
// <rdar://problem/15998821> Fail to infer types for closure that takes an inout argument
@@ -157,3 +157,8 @@ func r15998821() {
157157
let g = { x in x = 3 }
158158
take_closure(g)
159159
}
160+
161+
// <rdar://problem/22602657> better diagnostics for closures w/o "in" clause
162+
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}}
163+
164+

test/Constraints/diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ func f20371273() {
412412
func rdar21078316() {
413413
var foo : [String : String]?
414414
var bar : [(String, String)]?
415-
bar = foo.map { ($0, $1) } // expected-error {{contextual type for closure argument list expects 1 argument, but 2 were specified}}
415+
bar = foo.map { ($0, $1) } // expected-error {{contextual closure type '([String : String]) -> [(String, String)]' expects 1 argument, but 2 were used in closure body}}
416416
}
417417

418418

0 commit comments

Comments
 (0)