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

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

Open
edwinveger opened this issue Mar 21, 2025 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@edwinveger
Copy link

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.

@edwinveger edwinveger added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Mar 21, 2025
@Brett-Best
Copy link

Brett-Best commented Mar 21, 2025

You can work around this problem with:

class MyService {
    enum MyServiceError: Error {
        case unknown
    }

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

    func wrapInResult() {
        let result: Result<(), MyServiceError> = Result(catching: { () throws(MyServiceError) in try attemptSomethingWithTypedError() })
    }
}

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