Skip to content

Unable to type-check inline closure expressions with @MainActor and/or @Sendable #73199

Open
@NachoSoto

Description

@NachoSoto
Contributor

Description

These simple expressions lead to "Type of expression is ambiguous without a type annotation"

Reproduction

None of these can be type checked:

let _: (@MainActor @Sendable () -> Void)? = true ? { } : nil
let _: (@Sendable () -> Void)? = true ? { } : nil
let _: (@MainActor () -> Void)? = true ? { } : nil

These can:

let _: (() -> Void)? = true ? { } : nil
let _: (@MainActor @Sendable () -> Void)? = {}
let _: @MainActor @Sendable () -> Void = {}
let _: (@MainActor @Sendable () -> Void)? = true ? { @MainActor @Sendable in } : nil

Another example that breaks even with the explicit @MainActor @Sendable in:

func g(_ a: Int) async throws {}

func f() {
  let _: (@MainActor @Sendable (Int) async throws -> Void)? = true ? {
    @MainActor @Sendable in try await g($0)
  } : nil
}

Expected behavior

It compiles.

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: x86_64-apple-macosx14.0

Additional information

No response

Activity

added
bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.
triage neededThis issue needs more specific labels
on Apr 23, 2024
changed the title [-]Unable to type-check inline closure expression[/-] [+]Unable to type-check inline closure expressions with `@MainActor` and/or `@Sendable`[/+] on Apr 23, 2024
added
compilerThe Swift compiler itself
type checkerArea → compiler: Semantic analysis
attributesFeature: Declaration and type attributes
function typesFeature → types: function types
typesFeature: types
ternary operatorFeature → operators: The ternary operator
on Apr 23, 2024
AnthonyLatsis

AnthonyLatsis commented on Apr 23, 2024

@AnthonyLatsis
Collaborator

Dropping optionality seems like it would make these simpler to debug:

let _: @MainActor @Sendable () -> Void = true ? {} : {}
let _: @Sendable () -> Void = true ? {} : {}
let _: @MainActor () -> Void = true ? {} : {}
added
concurrencyFeature: umbrella label for concurrency language features
and removed
triage neededThis issue needs more specific labels
on Apr 23, 2024
xedin

xedin commented on Apr 23, 2024

@xedin
Contributor

We have a few tests in the suite with this problem already, this is binding inference issue related to ranking between bindings that come from the ternary branches and a contextual type. rdar://125948508 for future reference.

NachoSoto

NachoSoto commented on Jun 19, 2024

@NachoSoto
ContributorAuthor

Still broken on Swift 6.

NachoSoto

NachoSoto commented on Feb 14, 2025

@NachoSoto
ContributorAuthor

Still broken on Swift version 6.0.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    SendableArea → standard library: The Sendable protocolattributesFeature: Declaration and type attributesbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfconcurrencyFeature: umbrella label for concurrency language featuresexpressionsFeature: expressionsfunction typesFeature → types: function typesswift 6.0ternary operatorFeature → operators: The ternary operatortype 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

        Participants

        @xedin@NachoSoto@AnthonyLatsis

        Issue actions

          Unable to type-check inline closure expressions with `@MainActor` and/or `@Sendable` · Issue #73199 · swiftlang/swift