Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typed throws doesn't infer error type for do/catch block #75260

Closed
KeithBauerANZ opened this issue Jul 15, 2024 · 2 comments
Closed

Typed throws doesn't infer error type for do/catch block #75260

KeithBauerANZ opened this issue Jul 15, 2024 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@KeithBauerANZ
Copy link

Description

Typed throws seems to presume a do/catch block rethrowing the error it caught, is always throwing any Error, when in fact it may know the type of the thrown error.

Reproduction

func doStuff<E: Error>(
    with closure: () throws(E) -> Void
) throws(E) -> Void {
    do {
        do {
            try closure()
        } catch {
            throw error
        }
    } catch {
        throw error //  error: thrown expression type 'any Error' cannot be converted to error type 'E'
    }
}

(Same behavior with FullTypedThrows enabled)

Expected behavior

This should compile — the inner do block can only throw E, and therefore the outer catch block can only throw E

Environment

swift-driver version: 1.111.2 Apple Swift version 6.0 (swiftlang-6.0.0.5.15 clang-1600.0.22.6)
Target: arm64-apple-macosx14.0

Additional information

No response

@KeithBauerANZ KeithBauerANZ added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jul 15, 2024
@xwu
Copy link
Collaborator

xwu commented Jul 16, 2024

(Same behavior with FullTypedThrows enabled)

One expects that's because FullTypedThrows isn't fully implemented. You can explicitly type the do block for now.

@KeithBauerANZ
Copy link
Author

Seems like this behaves as expected for now, then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

2 participants