swift
3b137b2f - Sema: Implicit conversion for single-expression closures of Never type

Commit
9 years ago
Sema: Implicit conversion for single-expression closures of Never type This fixes a usability regression with the removal of @noreturn in Swift 3. Previously, it was legal to write this: let callback: () -> Int = { fatalError() } Now that the special @noreturn attribute has been replaced with a Never type, the above fails to typecheck, because the expression now has type 'Never', and we expect a value of type 'Int'. Getting around this behavior requires ugly workarounds to force the parser to treat the body as a statement rather than an expression; for example, let callback: () -> Int = { _ = (); fatalError() } This patch generalized single-expression closures to allow the 'Never to T' conversion. Note that this is rather narrow in scope -- it only applies to closure *literals*, single-expression ones at that, not arbitrary function *values*. In fact, it is not really a conversion at all, but more of a desugaring rule for single-expression closures. They can now be summarized as follows: - If the closure literal has contextual return type T and the expression has Never type, the closure desugars as { _ = <expr> }, with no ReturnStmt. - If the closure literal has contextual return type T for some non-void type T, the closure desugars as { return <expr> }; the expression type must be convertible to T. - If the closure literal has contextual return type Void, and the expression has some non-Void type T, the closure desugars as { _ = <expr>; return () }. Fixes <rdar://problem/28269358> and <https://bugs.swift.org/browse/SR-2661>.
Author
Committer
Parents
Loading