Autoclosures fail to contain thrown errors when created inside a closure
| Originator: | nathaniel.chandler | ||
| Number: | rdar://22406315 | Date Originated: | 2015 Aug 24 |
| Status: | Open | Resolved: | |
| Product: | iOS SDK | Product Version: | Xcode7 Beta5 |
| Classification: | Serious Bug | Reproducible: | Always |
Summary:
An autoclosure parameter whose type throws requires that client code try when passing the de-braced closure:
consume_an_error_from_a_throwing_func(try a_func_that_throws)
However, in the context of a closure, this syntax does not work properly. In particular, the error "leaks" out of <<try a_func_that_throws>> into the surrounding scope, making the closure be inferred to be of throwing type.
let c = {
consume_an_error_from_a_throwing_func(try a_func_that_throws)
}
Here, c is inferred to be of type () throws -> (). However, consume_an_error_from_a_throwing_func, the only function called from this closure, does not throw. (a_func_that_throws is called inside the implementation of consume_an_error_from_a_throwing_func and consequently any error it throws must be handled there.) The inferred type of c should be () -> (), a non-throwing function.
Steps to Reproduce:
Attempt to compile the following code with Xcode7 Beta 5.
func attempt<T>(@autoclosure f: () throws -> T) -> T? {
//...
}
func create_int() throws -> Int {
return 1
}
func call(function: () -> ()) {
function()
}
call {
attempt(try create_int())
}
func direct() {
attempt(try create_int())
}
Notice the compile error on the line where call is invoked: Invalid conversion from throwing function of type '() throws -> _' to non-throwing function of type '() -> ()'.
Expected Results:
This code should compile just fine.
Actual Results:
A compile error is raised.
Version:
Xcode 7 Beta 5
Notes:
Configuration:
MacBook Pro Retina
Attachments:
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!