That documentation change tracked the following compiler behavior change, implemented by @Lily_Ballard who might be able to share more of the thoughts behind it:
opened 09:11PM - 05 Feb 16 UTC
closed 06:53AM - 02 Mar 16 UTC
bug
compiler
| | |
|------------------|-----------------|…
|Previous ID | SR-681 |
|Radar | None |
|Original Reporter | @lilyball |
|Type | Bug |
|Status | Closed |
|Resolution | Done |
<details>
<summary>Environment</summary>
Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Apple Swift version 2.2 (swiftlang-703.0.6 clang-703.0.16)
Target: x86_64-apple-macosx10.9
</details>
<details>
<summary>Additional Detail from JIRA</summary>
| | |
|------------------|-----------------|
|Votes | 0 |
|Component/s | Compiler |
|Labels | Bug |
|Assignee | @lilyball |
|Priority | |
md5: 195e8fe56938aa35f00d66470337489b
</details>
**Issue Description:**
Given the following code:
``` java
struct MyErr: ErrorType {
var counter: Int = 0
}
func foo<T>(@noescape f: () throws -> T) rethrows -> T {
do {
return try f()
} catch let err as MyErr {
throw MyErr(counter: err.counter+1)
}
}
```
It's obvious from reading it that this function can only ever throw if the closure argument throws. But the compiler doesn't recognize this, and reports an error:
unnamed.swift:8:9: error: a function declared 'rethrows' may only throw if its parameter does
throw MyErr(counter: err.counter+1)
^
apple:master
← lilyball:better_rethrows_analysis
opened 01:50AM - 12 Feb 16 UTC
A catch block can only be entered if the do block threw an error. In a
rethrows … function, if the do block throws an error only under rethrows
conditions, then the catch block can only be entered under rethrows
conditions, which means the catch block can unconditionally throw and
it's still safe.
This enables code that looks like
``` swift
func foo(f: () throws -> Void) rethrows {
do {
try f()
} catch is SomeError {
throw OtherError()
}
}
```
Fixes [SR-681](https://bugs.swift.org/browse/SR-681).
EDIT: The docs describing the original limitation came from commit f8a46d52 , which @Joe_Groff reviewed, and which resolved an issue pointed out via Twitter.
2 Likes