Skip to content

Swift cannot deduce thrown Error type when parameter closure throws the same error type as the calling function #80787

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

Open
elijah-semyonov opened this issue Apr 13, 2025 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@elijah-semyonov
Copy link

elijah-semyonov commented Apr 13, 2025

Description

The snippet in Reproduction doesn't compile.

Reproduction

Compiles:

struct SomeError: Error {
    
}

func perform<T>(
    block: () throws -> T
) throws -> T {
    try block()
}


let a: Int = try perform { 
    throw SomeError()
}

Doesn't compile:

struct SomeError: Error {
    
}

func perform<T>(
    block: () throws(SomeError) -> T
) throws(SomeError) -> T {
    try block()
}


let a: Int = try perform { // Invalid conversion of thrown error type 'any Error' to 'SomeError'

    throw SomeError()
}

Expected behavior

Compiler should deduce that error thrown by the closure is the same kind of error and successfully compile the program.

Environment

swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0

Additional information

No response

@elijah-semyonov elijah-semyonov added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Apr 13, 2025
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

1 participant