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

[SR-10814] Incorrect compiler error with try + autoclosure inside another closure #53204

Closed
swift-ci opened this issue May 31, 2019 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-10814
Radar None
Original Reporter nonsensery (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment
  • macOS 10.14.5

  • Xcode 10.2.1

  • Swift 5.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: cece38381f5ed1ae82c7f2f657ef1657

duplicates:

Issue Description:

Steps to reproduce

  1. Create a new Swift playground.

  2. Paste the code below into it.

  3. Attempt to run it.

func takesThrowingClosureButDoesNotRethrow(_ block: @autoclosure () throws -> Void) {
    do {
        try block()
    } catch {
        print(error)
    }
}

func throwingOperation() throws {
    // ...
}

// At the top level, of the file, this compiles fine:
takesThrowingClosureButDoesNotRethrow(try throwingOperation())

func compilesOK() {
    // Inside a function, this compiles fine:
    takesThrowingClosureButDoesNotRethrow(try throwingOperation())
}

let failsToCompile: () -> Void = {
    // Inside a closure, the compiler thinks that this line throws an error:
    takesThrowingClosureButDoesNotRethrow(try throwingOperation())
}

Expected Result

The code should compile and run.

Actual Result

The code does not compile. The compiler reports an error on the failsToCompile declaration:

Invalid conversion from throwing function of type '() throws -> ()' to non-throwing function type '() -> Void'"

Discussion

The sample code contains a function – takesThrowingClosureButDoesNotRethrow – that has as an @autoclosure throwing closure argument, but does not "rethrow" errors.

Calling this function with a throwing expression from the top level of the playground, or from inside a function works as expected. But, doing the same from inside a closure results in a compiler error.

One other note: Removing the @autoclosure annotation and explicitly passing `{ try throwingOperation() }` works in all cases.

@belkadan
Copy link
Contributor

belkadan commented Jun 3, 2019

I think this one's tricky because the throws checker runs before learning that the argument expression there is an @autoclosure. I'll find the dup later.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants