Last active
September 24, 2024 17:24
-
-
Save robertmryan/2ee27f4eff8c364b4e313f3fd6d950e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 (seefoo
). However, if you add acatch
block to silence that incorrect error (inbar
), it now correctly recognizes that this extracatch
is unnecessary and “will never be executed.”