You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
rethrows functions can't throw unless their closure throws. By extension, this means that a rethrows function cannot call another throws function. But it seems that if the call to the other throws function is itself wrapped in a closure passed to another rethrows function, the compiler gets confused and allows it. I first hit this when using SequenceType.flatMap, but the following snippet reproduces it:
structMyErr: ErrorType {}
funcfoo<T>(@noescapef: () throws -> T) throws -> T {
throwMyErr()
}
funcbar<T,U>(x: T, @noescapef: Tthrows -> U) rethrows -> U {
// if we called// return try foo({ try f(x) })// the compiler would yell at us for calling a `throws` function.// But if we wrap it in `idemp()` the compiler doesn't yell at us// even though it should.returntryidemp(x, f: { xintryfoo({ tryf(x) }) })
}
funcidemp<T,U>(x: T, @noescapef: Tthrows -> U) rethrows -> U {
returntryf(x)
}
// As a consequence, because foo() unconditionally throws, the following// results in the program aborting execution (without even an error message).print(bar(1, f: { $0+1 }))
The text was updated successfully, but these errors were encountered:
I'm able to reproduce the same (looks like) issue with this snippet both in Xcode 7 and Xcode 8 beta 5 (Apple Swift version 3.0 (swiftlang-800.0.41.2 clang-800.0.36) Target: x86_64-apple-macosx10.9)
Crash goes away if smthThatRethrows is marked with throws instead of rethrows
The other way to fix it is to explicitly specify closure type as throwing:
letblock: () throws -> Void = {}
do {
trysmthThatRethrows(block: block) //does not crash, prints error
}
catch { print(error) }
Environment
Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Apple Swift version 2.2 (swiftlang-703.0.6 clang-703.0.16)
Target: x86_64-apple-macosx10.9
Additional Detail from JIRA
md5: 45c8f78cc7660e33aa88968b85b08224
is blocked by:
Issue Description:
rethrows
functions can't throw unless their closure throws. By extension, this means that arethrows
function cannot call anotherthrows
function. But it seems that if the call to the otherthrows
function is itself wrapped in a closure passed to anotherrethrows
function, the compiler gets confused and allows it. I first hit this when usingSequenceType.flatMap
, but the following snippet reproduces it:The text was updated successfully, but these errors were encountered: