Skip to content

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

@KeithBauerANZ

Description

@KeithBauerANZ

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions