Commit 3b137b2
committed
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>.1 parent eb68737 commit 3b137b2
File tree
7 files changed
+131
-100
lines changed- include/swift/AST
- lib
- AST
- Sema
- test
- Misc
- expr/closure
7 files changed
+131
-100
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
329 | 329 | | |
330 | 330 | | |
331 | 331 | | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | 332 | | |
337 | | - | |
| 333 | + | |
338 | 334 | | |
339 | 335 | | |
340 | 336 | | |
| |||
3393 | 3389 | | |
3394 | 3390 | | |
3395 | 3391 | | |
3396 | | - | |
3397 | 3392 | | |
3398 | 3393 | | |
3399 | 3394 | | |
| |||
3419 | 3414 | | |
3420 | 3415 | | |
3421 | 3416 | | |
3422 | | - | |
3423 | | - | |
3424 | | - | |
3425 | | - | |
3426 | | - | |
3427 | | - | |
3428 | | - | |
3429 | | - | |
3430 | | - | |
3431 | | - | |
3432 | | - | |
3433 | | - | |
3434 | 3417 | | |
3435 | 3418 | | |
3436 | 3419 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2080 | 2080 | | |
2081 | 2081 | | |
2082 | 2082 | | |
2083 | | - | |
2084 | | - | |
2085 | 2083 | | |
2086 | 2084 | | |
2087 | 2085 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1827 | 1827 | | |
1828 | 1828 | | |
1829 | 1829 | | |
1830 | | - | |
1831 | | - | |
1832 | | - | |
1833 | | - | |
1834 | | - | |
1835 | | - | |
| 1830 | + | |
| 1831 | + | |
| 1832 | + | |
| 1833 | + | |
1836 | 1834 | | |
1837 | 1835 | | |
1838 | 1836 | | |
1839 | 1837 | | |
1840 | | - | |
1841 | | - | |
1842 | | - | |
1843 | | - | |
1844 | | - | |
| 1838 | + | |
| 1839 | + | |
| 1840 | + | |
| 1841 | + | |
1845 | 1842 | | |
| 1843 | + | |
1846 | 1844 | | |
1847 | 1845 | | |
1848 | 1846 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1198 | 1198 | | |
1199 | 1199 | | |
1200 | 1200 | | |
1201 | | - | |
1202 | | - | |
1203 | | - | |
1204 | | - | |
| 1201 | + | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
1205 | 1205 | | |
1206 | 1206 | | |
1207 | 1207 | | |
| |||
1210 | 1210 | | |
1211 | 1211 | | |
1212 | 1212 | | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
1213 | 1224 | | |
1214 | 1225 | | |
1215 | 1226 | | |
| |||
5017 | 5028 | | |
5018 | 5029 | | |
5019 | 5030 | | |
5020 | | - | |
5021 | | - | |
5022 | | - | |
5023 | | - | |
5024 | | - | |
5025 | | - | |
5026 | | - | |
5027 | | - | |
| 5031 | + | |
| 5032 | + | |
| 5033 | + | |
| 5034 | + | |
| 5035 | + | |
| 5036 | + | |
5028 | 5037 | | |
5029 | 5038 | | |
5030 | 5039 | | |
| |||
5051 | 5060 | | |
5052 | 5061 | | |
5053 | 5062 | | |
5054 | | - | |
5055 | 5063 | | |
5056 | 5064 | | |
5057 | 5065 | | |
| |||
5065 | 5073 | | |
5066 | 5074 | | |
5067 | 5075 | | |
| 5076 | + | |
| 5077 | + | |
| 5078 | + | |
| 5079 | + | |
| 5080 | + | |
| 5081 | + | |
| 5082 | + | |
| 5083 | + | |
| 5084 | + | |
| 5085 | + | |
| 5086 | + | |
| 5087 | + | |
| 5088 | + | |
| 5089 | + | |
| 5090 | + | |
| 5091 | + | |
| 5092 | + | |
| 5093 | + | |
| 5094 | + | |
| 5095 | + | |
| 5096 | + | |
| 5097 | + | |
| 5098 | + | |
| 5099 | + | |
| 5100 | + | |
| 5101 | + | |
| 5102 | + | |
| 5103 | + | |
| 5104 | + | |
| 5105 | + | |
| 5106 | + | |
| 5107 | + | |
5068 | 5108 | | |
5069 | 5109 | | |
5070 | 5110 | | |
| |||
6471 | 6511 | | |
6472 | 6512 | | |
6473 | 6513 | | |
6474 | | - | |
| 6514 | + | |
| 6515 | + | |
6475 | 6516 | | |
6476 | 6517 | | |
| 6518 | + | |
| 6519 | + | |
| 6520 | + | |
| 6521 | + | |
| 6522 | + | |
6477 | 6523 | | |
6478 | 6524 | | |
6479 | 6525 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1959 | 1959 | | |
1960 | 1960 | | |
1961 | 1961 | | |
1962 | | - | |
1963 | | - | |
| 1962 | + | |
| 1963 | + | |
1964 | 1964 | | |
1965 | | - | |
| 1965 | + | |
| 1966 | + | |
1966 | 1967 | | |
1967 | 1968 | | |
1968 | 1969 | | |
| |||
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
0 commit comments