Missing Compiler Error "Sending value risks causing data races"

The code below compiles without errors:

@globalActor actor SomeGlobalActor: GlobalActor {
    static let shared = SomeGlobalActor()
}

class NonSendable {}

@MainActor func foo(value: NonSendable) async throws {}
@SomeGlobalActor func bar(value: NonSendable) async throws {}

@MainActor
func main() async throws {
    let value = NonSendable()
    let task = Task {
        try await foo(value: value)
    }
    try await bar(value: value)
    try await task.value
}

I would expect an error, "Sending 'value' risks causing data races" in line try await bar(value: value).

This code actually causes a data race, since value can be mutated concurrently on actor MainActor and SomeGlobalActor.

Note: when changing the code such, that value will be accessed, after the above statements (for example print(value), the compiler finally puts this error on the line try await bar(value: value). This is however unrelated to the race occurring in the task and in async function bar.

swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)

3 Likes

this looks like a bug to me, and structurally seems somewhat similar to this existing GitHub issue. i encourage you to file a bug report in the compiler repo and perhaps reference that existing issue in some manner.

1 Like