8000 It is possible to send non-sendable values using Task { ... } when isolated to main actor · Issue #82610 · swiftlang/swift · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
It is possible to send non-sendable values using Task { ... } when isolated to main actor #82610
Open
@Sweeper777

Description

@Sweeper777

Description

In a main actor isolated function, it is possible to send non-sendable values to a non-isolated async function by calling that function in a Task { ... }.

Reproduction

class C {
    var x: Int = 0
}

nonisolated func f(_ c: C) async {
    c.x += 1
}

@MainActor
func run() async {
    let c = C()

    let t1 = Task {
        await f(c)
    }
    let t2 = Task {
        await f(c)
    }
    await t1.value
    await t2.value
    print(c.x)
}

Expected behavior

The code should produce an error about sending the non-sendable c.

Environment

swift-driver version: 1.127.5.3 Apple Swift version 6.2 (swiftlang-6.2.0.10.950 clang-1700.3.10.950)
Target: arm64-apple-macosx15.0

Additional information

Compare this with alternative versions using async let, or a non-isolated run. Both of these produce errors as expected.

@MainActor
func run() async {
    let c = C()
    async let t1 = f(c)
    async let t2 = f(c)
    await (t1, t2)
    print(c.x)
}
func run() async {
    let c = C()

    let t1 = Task {
        await f(c)
    }
    let t2 = Task {
        await f(c)
    }
    await t1.value
    await t2.value
    print(c.x)
}

From a Stack Overflow post.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0