Skip to content

Implicit cast from @Sendable functions to non-sendable ones doesn't happen in arbitrary expressions #78753

Open
@li3zhen1

Description

@li3zhen1

Description

Implicit cast from @Sendable functions to non-sendable ones doesn't happen in arbitrary expressions.

As SE-302 says:

@Sendable functions are always subtypes of non-@Sendable functions, and implicitly convert when needed.

I would expect the following code would work like when replaced with classes and subclasses.

Reproduction

func identity<T>(_ x: T) -> T { x }

func test() {
    
    let sendableClosure: @Sendable (Int) -> (Int) = { $0 }
    
    // ✅
    let _: (Int) -> (Int) = sendableClosure
    
    // ❌Conflicting arguments to generic parameter 'T' ('@Sendable (Int) -> Int' vs. '(Int) -> Int')
    let _: (Int) -> (Int) = identity(sendableClosure)
    
    // ❌Type of expression is ambiguous without a type annotation
    let _: (Int) -> (Int) = true ? sendableClosure : sendableClosure
    
}

Expected behavior

Perform implicit casts as class and sub-class would do.

Environment

Apple Swift version 6.0.3 (swiftlang-6.0.3.1.4 clang-1600.0.30)
Target: arm64-apple-macosx15.0

Additional information

Swift forums thread: https://forums.swift.org/t/why-is-there-a-type-of-expression-is-ambiguous-without-a-type-annotation-error-when-using-a-ternary-operator-on-inferred-function-types-in-swift/77306

Metadata

Metadata

Assignees

Labels

SendableArea → standard library: The Sendable protocolbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfconcurrencyFeature: umbrella label for concurrency language featuresfunction typesFeature → types: function typesimplicit conversionsFeature: implicit conversionsswift 6.2type checkerArea → compiler: Semantic analysistypesFeature: typesunexpected errorBug: Unexpected error

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions