Skip to content

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

@swift-ci

Description

@swift-ci
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    @autoclosureFeature → attributes: the @autoclosure type attributeattributesFeature: Declaration and type attributesclosuresFeature: closurescompilerThe Swift compiler itselferror handlingexpressionsFeature: expressionsswift 6.0throws & rethrowsFeature → error handling: throws & rethrowstype checkerArea → compiler: Semantic analysistype inferenceFeature: type inferenceunexpected errorBug: Unexpected error

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions