Skip to content

Result.init(catching:) doesn't infer typed throw but instead falls back to any Error #80193

Open
@edwinveger

Description

@edwinveger

Description

While working with typed throws, I found that the Result.init(catching:) doesn't infer the typed error. But its signature implies that it should.

public enum Result<Success, Failure> where Failure : Error, Success : ~Copyable {
    // …
    public init(catching body: () throws(Failure) -> Success) {
        // …
    }
}

This issue might be related to other typed throws issues like #75430 or #69985.

Reproduction

class MyService {
    enum MyServiceError: Error {
        case unknown
    }

    func attemptSomethingWithTypedError() throws(MyServiceError) {
        throw .unknown
    }

    func wrapInResult() {
        // ❗️ compilation error
        // result is inferred to be of type `Result<(), any Error>` instead
        let result: Result<(), MyServiceError> = Result(catching: { try attemptSomethingWithTypedError() })
    }
}

Expected behavior

In this example, I expect the inferred result type to be Result<(), MyServiceError>.

Environment

swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)
Target: arm64-apple-macosx15.0

Additional information

Reproducible in Xcode playground + Swift REPL.

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