Skip to content

Instantly share code, notes, and snippets.

@robertmryan
Last active September 24, 2024 17:24
Show Gist options
  • Save robertmryan/2ee27f4eff8c364b4e313f3fd6d950e8 to your computer and use it in GitHub Desktop.
Save robertmryan/2ee27f4eff8c364b4e313f3fd6d950e8 to your computer and use it in GitHub Desktop.
enum MyError: Error {
case one
case two
}
struct MyTest {
private func funcWithTypedThrow() async throws(MyError) {…}
// example where typed-throw incorrectly concludes catch is not exhaustive
func foo() async {
do {
try await funcWithTypedThrow() // Swift 6.0 compiler incorrectly produces error: “Errors thrown from here are not handled because the enclosing catch is not exhaustive”
} catch .one {
print("one")
} catch .two {
print("two")
}
}
// example where we add extra `catch`, but compiler now recognizes that is unnecessary
func bar() async {
do {
try await funcWithTypedThrow()
} catch .one {
print("one")
} catch .two {
print("two")
} catch { // Swift 6.0 compiler correctly produces warning: “Case will never be executed”
print("this is required to silence 'non-exhaustive' warning")
}
}
}
@robertmryan
Copy link
Author

If you used typed-throwing, Swift 6.0 (in Xcode 16.0) will understand that the error is MyError, but incorrectly concludes that the catch blocks are not exhaustive (see foo). However, if you add a catch block to silence that incorrect error (in bar), it now correctly recognizes that this extra catch is unnecessary and “will never be executed.”

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment