nh7a
1
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
nh7a
3
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