Skip to content

Misleading error "Sending 'asdf' risks causing data races" when var asdf is Sendable #76648

@nickasd

Description

@nickasd

Description

In Swift 6, the code below results in a compiler error

Sending 'asdf' risks causing data races

which is misleading, since the problem is not the variable asdf itself, but the object to which it is being assigned to, namely a, which is not Sendable.

Reproduction

class A {
    var description = ""
}

class B {
    let a = A()
    
    func b() {
        let asdf = ""
        Task { @MainActor in
            a.description = asdf // Sending 'asdf' risks causing data races
        }
    }
}

Expected behavior

An error like

Task or actor isolated value cannot be sent

This is in fact the error displayed when changing

a.description = asdf

to

a.description = "asdf"

but it is displayed at the line

Task { @MainActor in

Instead, it would be useful if the compiler indicated what particular value cannot be sent.

Environment

swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
Target: arm64-apple-macosx15.0

Additional information

No response

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.concurrencyFeature: umbrella label for concurrency language features

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions