Created
March 12, 2025 14:36
-
-
Save DivineDominion/8da46d9a3b63db3ababf666e3182de42 to your computer and use it in GitHub Desktop.
Demonstration how compiler cannot infer that typed errors are exhausted
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 failureA | |
case failureB | |
} | |
func produceFailure() throws(MyError) -> Int { | |
throw .failureB | |
} | |
do throws(MyError) { | |
let result = try produceFailure() | |
print("Result:", result) | |
} catch MyError.failureA { | |
print("Failure A") | |
} catch MyError.failureB { | |
print("Failure B") | |
} |
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 failureA | |
case failureB | |
} | |
func produceFailure() throws(MyError) -> Int { | |
throw .failureB | |
} | |
func anotherFunction() { | |
do throws(MyError) { | |
let result = try produceFailure() | |
// `- error: errors thrown from here are not handled because the enclosing catch is not exhaustive | |
print("Result:", result) | |
} catch MyError.failureA { | |
print("Failure A") | |
} catch MyError.failureB { | |
print("Failure B") | |
} | |
} | |
anotherFunction() |
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 failureA | |
case failureB | |
} | |
func produceFailure() throws(MyError) -> Int { | |
throw .failureB | |
} | |
func anotherFunction() { | |
do throws(MyError) { | |
let result = try produceFailure() | |
print("Result:", result) | |
} catch MyError.failureA { | |
print("Failure A") | |
} catch MyError.failureB { | |
print("Failure B") | |
} catch { | |
// `- warning: case will never be executed | |
print("Failure:", error) | |
} | |
} | |
anotherFunction() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NB: The code doesn't work in a Playground. At all.
