Swift 6 fails to cast inferred function types as return types from implicit @Sendable on return statement #78769
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
triage needed
This issue needs more specific labels
Description
Type checker appears to fail to cast an implicit
@Sendable
inferred function type to the expected return type in specific circumstances such as when it is directly returned. There may be other cases this is happening but the issue results in a compile time error that is otherwise unexpected as this behavior appears to work fine prior to Swift 6.Context
There is an example in the Swift Programming Language Documentation under the Function Types as Return Types section which includes a code snippet for returning an inferred function type. The example from the documentation is as follows:
However, the line
return backward ? stepBackward : stepForward
results in a compile time error that reads: "Type of expression is ambiguous without a type annotation."To get the code to compile with the
return
line as-is, it requires explicitly adding the otherwise implicit@Sendable
annotation to the return type as follows:Workarounds
There are several workarounds that do work as found by several community members which I will summarize here:
@Sendable
in the function bodyReproduction
Expected behavior
The Swift Programming Language Documentation Function Types as Return Types code should compile without error and correctly infer the return type when it is an inferred function type.
Environment
Note: Please see the forum post links where other community members tried on different environments, setups, Xcode projects and packages (not just Playgrounds).
Additional information
Please read these posts for detailed background. I posted these to get help from the community to understand what was going on and it has valuable discussion and context. Thank you to all who contributed and encouraged me to submit this issue!
The text was updated successfully, but these errors were encountered: