How to use `autoreleasepool` with typed throws

I don't see how the linked section SE-0431 ∕ Closure thrown type inference explains the error @Saafo is seeing here.

That section says that plain throw statements in a closure body will be inferred to throw any Error. But importantly, it doesn't say the same about a try statement calling a function that uses typed throws, as in @Saafo's example. In fact, the proposal explicitly says that this situation should result in an inferred closure type with a typed error (emphasis mine):

With the rule specified here, it will be inferred as throws(CatError). This could break some code that depends on the precisely inferred type. To prevent this from becoming a source compatibility problem, we apply the same rule as for do...catch statements to limit inference: throw statements within the closure body are treated as having the type any Error in Swift 5. This way, one can only infer a more specific thrown error type in a closure when the try operations are calling functions that make use of typed errors.

As far as I can tell, the behavior @Saafo describes here is not covered by the type inference limitations/shortcuts documented in SE-0431. And the same goes for @Saafo's other example discussed here: How to catch typed throws in clousures. I think both of these should compile as-is according to SE-0431.

2 Likes