File tree Expand file tree Collapse file tree 4 files changed +34
-4
lines changed Expand file tree Collapse file tree 4 files changed +34
-4
lines changed Original file line number Diff line number Diff line change @@ -6304,6 +6304,22 @@ void FailureDiagnosis::diagnoseAmbiguity(Expr *E) {
6304
6304
return ;
6305
6305
}
6306
6306
6307
+ // A very common cause of this diagnostic is a situation where a closure expr
6308
+ // has no inferred type, due to being a multiline closure. Check to see if
6309
+ // this is the case and (if so), speculatively diagnose that as the problem.
6310
+ bool didDiagnose = false ;
6311
+ E->forEachChildExpr ([&](Expr *subExpr) -> Expr*{
6312
+ auto closure = dyn_cast<ClosureExpr>(subExpr);
6313
+ if (!didDiagnose && closure)
6314
+ didDiagnose = checkMultistatementClosureForAmbiguity (closure,
6315
+ CS->getTypeChecker ());
6316
+
6317
+ return subExpr;
6318
+ });
6319
+
6320
+ if (didDiagnose) return ;
6321
+
6322
+
6307
6323
6308
6324
// Attempt to re-type-check the entire expression, allowing ambiguity, but
6309
6325
// ignoring a contextual type.
Original file line number Diff line number Diff line change @@ -218,5 +218,16 @@ let things = Thing().map { thing in // expected-error {{unable to infer closure
218
218
}
219
219
220
220
221
+ // <rdar://problem/21675896> QoI: [Closure return type inference] Swift cannot find members for the result of inlined lambdas with branches
222
+ func r21675896( file : String ) {
223
+ let x : String = { // expected-error {{unable to infer closure return type in current context}}
224
+ if true {
225
+ return " foo "
226
+ }
227
+ else {
228
+ return file
229
+ }
230
+ } ( ) . pathExtension
231
+ }
221
232
222
233
Original file line number Diff line number Diff line change @@ -465,9 +465,12 @@ func f20371273() {
465
465
466
466
467
467
// <rdar://problem/20921068> Swift fails to compile: [0].map() { _ in let r = (1,2).0; return r }
468
- // FIXME: Should complain about not having a return type annotation in the closure.
469
- [ 0 ] . map { _ in let r = ( 1 , 2 ) . 0 ; return r }
470
- // expected-error @-1 {{expression type '[_]' is ambiguous without more context}}
468
+ [ 0 ] . map { // expected-error {{unable to infer closure return type in current context}}
469
+ _ in
470
+ let r = ( 1 , 2 ) . 0
471
+ return r
472
+ }
473
+
471
474
472
475
// <rdar://problem/21078316> Less than useful error message when using map on optional dictionary type
473
476
func rdar21078316( ) {
Original file line number Diff line number Diff line change @@ -259,7 +259,7 @@ Void(0) // expected-error{{argument passed to call that takes no arguments}}
259
259
_ = { 0 }
260
260
261
261
// <rdar://problem/22086634> "multi-statement closures require an explicit return type" should be an error not a note
262
- let samples = { // expected-error {{type of expression is ambiguous without more context}}
262
+ let samples = { // expected-error {{unable to infer closure return type in current context}}
263
263
// FIXME: This diagnostic should be improved, we can infer a type for the closure expr from
264
264
// its body (by trying really hard in diagnostic generation) and say that we need an explicit
265
265
// contextual result specified because we don't do cross-statement type inference or something.
You can’t perform that action at this time.
0 commit comments