Skip to content

Commit 7f08508

Browse files
Merge pull request #38153 from JiarenWang/wjr
[SR-14824] Improve diagnostic for multi-statement closures instead of…
2 parents 11fbdaa + c5de1f0 commit 7f08508

File tree

9 files changed

+17
-18
lines changed

9 files changed

+17
-18
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ ERROR(cannot_infer_closure_parameter_type,none,
273273
ERROR(cannot_infer_closure_type,none,
274274
"unable to infer closure type in the current context", ())
275275
ERROR(cannot_infer_closure_result_type,none,
276-
"unable to infer%select{ complex|}0 closure return type; "
277-
"add explicit type to disambiguate", (bool))
276+
"cannot infer return type for closure with multiple statements; "
277+
"add explicit type to disambiguate", ())
278278
FIXIT(insert_closure_return_type_placeholder,
279279
"%select{| () }0-> <#Result#> %select{|in }0",
280280
(bool))

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6938,8 +6938,7 @@ bool UnableToInferClosureParameterType::diagnoseAsError() {
69386938
bool UnableToInferClosureReturnType::diagnoseAsError() {
69396939
auto *closure = castToExpr<ClosureExpr>(getRawAnchor());
69406940

6941-
auto diagnostic = emitDiagnostic(diag::cannot_infer_closure_result_type,
6942-
closure->hasSingleExpressionBody());
6941+
auto diagnostic = emitDiagnostic(diag::cannot_infer_closure_result_type);
69436942

69446943
// If there is a location for an 'in' token, then the argument list was
69456944
// specified somehow but no return type was. Insert a "-> ReturnType "

test/Constraints/closures.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ struct CC {}
252252
func callCC<U>(_ f: (CC) -> U) -> () {}
253253

254254
func typeCheckMultiStmtClosureCrash() {
255-
callCC { // expected-error {{unable to infer complex closure return type; add explicit type to disambiguate}} {{none}}
255+
callCC { // expected-error {{cannot infer return type for closure with multiple statements; add explicit type to disambiguate}} {{none}}
256256
_ = $0
257257
return 1
258258
}
@@ -313,7 +313,7 @@ struct Thing {
313313
init?() {}
314314
}
315315
// This throws a compiler error
316-
let things = Thing().map { thing in // expected-error {{unable to infer complex closure return type; add explicit type to disambiguate}} {{34-34=-> <#Result#> }}
316+
let things = Thing().map { thing in // expected-error {{cannot infer return type for closure with multiple statements; add explicit type to disambiguate}} {{34-34=-> <#Result#> }}
317317
// Commenting out this makes it compile
318318
_ = thing
319319
return thing
@@ -322,7 +322,7 @@ let things = Thing().map { thing in // expected-error {{unable to infer complex
322322

323323
// <rdar://problem/21675896> QoI: [Closure return type inference] Swift cannot find members for the result of inlined lambdas with branches
324324
func r21675896(file : String) {
325-
let x: String = { // expected-error {{unable to infer complex closure return type; add explicit type to disambiguate}} {{20-20= () -> <#Result#> in }}
325+
let x: String = { // expected-error {{cannot infer return type for closure with multiple statements; add explicit type to disambiguate}} {{20-20= () -> <#Result#> in }}
326326
if true {
327327
return "foo"
328328
}
@@ -360,7 +360,7 @@ func someGeneric19997471<T>(_ x: T) {
360360

361361

362362
// <rdar://problem/20921068> Swift fails to compile: [0].map() { _ in let r = (1,2).0; return r }
363-
[0].map { // expected-error {{unable to infer complex closure return type; add explicit type to disambiguate}} {{5-5=-> <#Result#> }}
363+
[0].map { // expected-error {{cannot infer return type for closure with multiple statements; add explicit type to disambiguate}} {{5-5=-> <#Result#> }}
364364
_ in
365365
let r = (1,2).0
366366
return r
@@ -408,7 +408,7 @@ func r20789423() {
408408
print(p.f(p)()) // expected-error {{cannot convert value of type 'C' to expected argument type 'Int'}}
409409
// expected-error@-1:11 {{cannot call value of non-function type '()'}}
410410

411-
let _f = { (v: Int) in // expected-error {{unable to infer complex closure return type; add explicit type to disambiguate}} {{23-23=-> <#Result#> }}
411+
let _f = { (v: Int) in // expected-error {{cannot infer return type for closure with multiple statements; add explicit type to disambiguate}} {{23-23=-> <#Result#> }}
412412
print("a")
413413
return "hi"
414414
}

test/Constraints/diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public func myMap<T, U>(_ x: T?, _ f: (T) -> U) -> U? {
161161

162162
// <rdar://problem/20142523>
163163
func rdar20142523() {
164-
myMap(0..<10, { x in // expected-error{{unable to infer complex closure return type; add explicit type to disambiguate}} {{21-21=-> <#Result#> }} {{educational-notes=complex-closure-inference}}
164+
myMap(0..<10, { x in // expected-error{{cannot infer return type for closure with multiple statements; add explicit type to disambiguate}} {{21-21=-> <#Result#> }} {{educational-notes=complex-closure-inference}}
165165
()
166166
return x
167167
})

test/Constraints/patterns.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,14 @@ func good(_ a: A<EE>) -> Int {
230230
}
231231

232232
func bad(_ a: A<EE>) {
233-
a.map { // expected-error {{unable to infer complex closure return type; add explicit type to disambiguate}} {{none}}
233+
a.map { // expected-error {{cannot infer return type for closure with multiple statements; add explicit type to disambiguate}} {{none}}
234234
let _: EE = $0
235235
return 1
236236
}
237237
}
238238

239239
func ugly(_ a: A<EE>) {
240-
a.map { // expected-error {{unable to infer complex closure return type; add explicit type to disambiguate}} {{none}}
240+
a.map { // expected-error {{cannot infer return type for closure with multiple statements; add explicit type to disambiguate}} {{none}}
241241
switch $0 {
242242
case .A:
243243
return 1

test/diagnostics/pretty-printed-diagnostics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ foo(b:
128128
// CHECK: [[#LINE-1]] |
129129
// CHECK: [[#LINE]] | let x = { () -> Result in
130130
// CHECK: | +++++++++++++++++
131-
// CHECK: | ^ error: unable to infer complex closure return type; add explicit type to disambiguate
131+
// CHECK: | ^ error: cannot infer return type for closure with multiple statements; add explicit type to disambiguate
132132
// CHECK: [[#LINE+1]] | let y = 1
133133

134134
// CHECK: SOURCE_DIR{{[/\]+}}test{{[/\]+}}diagnostics{{[/\]+}}pretty-printed-diagnostics.swift:[[#LINE:]]:8
@@ -152,7 +152,7 @@ foo(b:
152152
// CHECK: SOURCE_DIR{{[/\]+}}test{{[/\]+}}diagnostics{{[/\]+}}pretty-printed-diagnostics.swift:[[#LINE:]]:20
153153
// CHECK: [[#LINE-1]] |
154154
// CHECK: [[#LINE]] | let 👍👍👍 = {
155-
// CHECK: | --> error: unable to infer complex closure return type; add explicit type to disambiguate [insert ' () -> <#Result#> in ']
155+
// CHECK: | --> error: cannot infer return type for closure with multiple statements; add explicit type to disambiguate [insert ' () -> <#Result#> in ']
156156
// CHECK: [[#LINE+1]] | let y = 1
157157

158158
// CHECK: SOURCE_DIR{{[/\]+}}test{{[/\]+}}diagnostics{{[/\]+}}pretty-printed-diagnostics.swift:[[#LINE:]]:5

test/expr/closure/closures.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ Void(0) // expected-error{{argument passed to call that takes no arguments}}
403403
_ = {0}
404404

405405
// <rdar://problem/22086634> "multi-statement closures require an explicit return type" should be an error not a note
406-
let samples = { // expected-error {{unable to infer complex closure return type; add explicit type to disambiguate}} {{16-16= () -> <#Result#> in }}
406+
let samples = { // expected-error {{cannot infer return type for closure with multiple statements; add explicit type to disambiguate}} {{16-16= () -> <#Result#> in }}
407407
if (i > 10) { return true }
408408
else { return false }
409409
}()
@@ -485,7 +485,7 @@ func lvalueCapture<T>(c: GenericClass<T>) {
485485
}
486486

487487
// Don't expose @lvalue-ness in diagnostics.
488-
let closure = { // expected-error {{unable to infer complex closure return type; add explicit type to disambiguate}} {{16-16= () -> <#Result#> in }}
488+
let closure = { // expected-error {{cannot infer return type for closure with multiple statements; add explicit type to disambiguate}} {{16-16= () -> <#Result#> in }}
489489
var helper = true
490490
return helper
491491
}

userdocs/diagnostics/complex-closure-inference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ let doubler = {
1010
If a closure body is not a single expression, it will not be considered when inferring the closure type. This is consistent with how type inference works in other parts of the language, where it proceeds one statement at a time. For example, in the following code an error will be reported because the type of `evenDoubler` cannot be inferred from its surrounding context and no signature was provided:
1111

1212
```swift
13-
// error: unable to infer complex closure return type; add explicit type to disambiguate
13+
// error: cannot infer return type for closure with multiple statements; add explicit type to disambiguate
1414
let evenDoubler = { x in
1515
if x.isMultiple(of: 2) {
1616
return x * 2

validation-test/compiler_crashers_2_fixed/0119-rdar33613329.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ struct X { var y: Int = 0 }
4343
var x = X()
4444
x ~> \X.y ≈> { a in a += 1; return 3 }
4545
// expected-error@-1 {{referencing operator function '~>' on 'P' requires that 'M<WritableKeyPath<X, Int>, R>' conform to 'P'}}
46-
// expected-error@-2 {{unable to infer complex closure return type; add explicit type to disambiguate}}
46+
// expected-error@-2 {{cannot infer return type for closure with multiple statements; add explicit type to disambiguate}}

0 commit comments

Comments
 (0)