-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
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
-
Create a new Swift playground.
-
Paste the code below into it.
-
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.