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

Compilation fails when passing error-specified throwable closure that throws expected type error #74460

Open
Vladlex opened this issue Jun 16, 2024 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. typed throws Feature → error handling → throws & rethrows: Typed throws

Comments

@Vladlex
Copy link

Vladlex commented Jun 16, 2024

Description

Compilation failed when you throw an error in a type-specific error throwing closure (and throwing error is the same type as expected)

Reproduction

/// Some specific error
public enum TestError: Error {
    case test
}

/// Some class rethrowing type-specific errors from passed closures
public final class WorkTimeMeasurement<Failure: Error> {
    
    static func tryMeasure(work: () throws(Failure) -> Void) throws(Failure) -> TimeInterval {
        let date = Date()
        try work()
        return Date().timeIntervalSince(date)
    }
    
    static func tryMeasureWithRethrows(work: () throws(Failure) -> Void) rethrows -> TimeInterval {
        let date = Date()
        try work()
        return Date().timeIntervalSince(date)
    }
    
}

/// Function that passed closures to Measurement with correct type errors
func testWork() {
    
    // Compile-time errors
    
    try WorkTimeMeasurement<TestError>.tryMeasure {
        // Invalid conversion of thrown error type 'any Error' to 'TestError
        throw TestError.test
    }
    
    try WorkTimeMeasurement<TestError>.tryMeasureWithRethrows {
        // Invalid conversion of thrown error type 'any Error' to 'TestError
        throw TestError.test
    }
}

Expected behavior

Compilation succeeded

Environment

swift-driver version: 1.109.2 Apple Swift version 6.0 (swiftlang-6.0.0.3.300 clang-1600.0.20.10)

Additional information

This sample is just the shortest way to reproduce the error.
There can be workarounds, but they are hard to use in a more complex production code.

@Vladlex Vladlex added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jun 16, 2024
@hborla hborla added typed throws Feature → error handling → throws & rethrows: Typed throws and removed triage needed This issue needs more specific labels labels Jul 14, 2024
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. typed throws Feature → error handling → throws & rethrows: Typed throws
Projects
None yet
Development

No branches or pull requests

2 participants