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?

1 Like

This is a known issue. It was fixed in f0fff2e5a02d8f56fc272ed0562a888d10f17f9b

4 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)
    }
}

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

Great, this workaround is useful for me