Why does sending a sendable value risk causing data races?

With Xcode 16 beta 3 (Swift 6 mode), the code below no longer builds.

@MainActor struct Foo {
  var num: Int { 42 }
  nonisolated func foo(_ int: Int) async {}
  func bar() async {
    await foo(num)  // ๐Ÿ›‘ Sending 'self.num' risks causing data races
  }
}

Sending main actor-isolated 'self.num' to nonisolated instance method 'foo' risks causing data races between nonisolated and main actor-isolated uses

Since Int is Sendable, I don't see how this makes sense, but does it?

2 Likes

This is a known issue. It was fixed in f0fff2e5a02d8f56fc272ed0562a888d10f17f9b

6 Likes

Cool. Meanwhile, is any workaround available, or do we need to wait until Xcode 16 beta 4?

Your can store the variable locally before using it as a parameter:

@MainActor struct Foo {
    var num: Int { 42 }
    nonisolated func foo(_ int: Int) async {}
    func bar() async {
        var num = num // sometimes a let works too, but not always
        await foo(num)
    }
}
2 Likes

I'm going back to Xcode 16 Beta 2 because of this.

Great, this workaround is useful for me

Seems to be the same in Beta 4 unfortunately

4 Likes

I also ran into this issue, but am unable to build my project with the Swift 6.0.1 toolchain, as Xcode reports an SDK/toolchain mismatch when trying to import SwiftUI.

See my corresponding post here: Unable to Use SwiftUI With Latest Swift 6.0.1. Compiler Due to Toolchain / SDK Mismatch