Poor type inference with typed throws #78639
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
compiler
The Swift compiler itself
error handling
generics
Feature: generic declarations and types
Swift 6.2-dev
throws & rethrows
Feature → error handling: throws & rethrows
type checker
Area → compiler: Semantic analysis
type inference
Feature: type inference
typed throws
Feature → error handling → throws & rethrows: Typed throws
unexpected error
Bug: Unexpected error
Description
Consider the following function:
We don't need to write explicit signatures for the closure that we pass to
withMemoryRebound
, because the compiler can infer it - it knows thatblock($0)
returns anR
, so the closure does, so the call towithMemoryRebound
does, so the body of the function does.I would expect this same level of type inference when adopting typed throws, but even simple examples require explicit type annotations.
Reproduction
I take the example above, and add an
E
generic parameter as the type of error thrown byblock
:It fails to compile.
To make it work, you need to add an explicit type signature to the closure where
block
is called:Expected behavior
I expect the first example in the reproduction section (with typed throws, without explicit type annotations in the function body) to compile
Environment
Swift version 6.2-dev (LLVM 81859ac55f8d09a, Swift 8ec8a12)
Target: x86_64-unknown-linux-gnu
Also Xcode 16.0 (16A242d)
Additional information
No response
The text was updated successfully, but these errors were encountered: