Hey everyone, I'm running into a compiler error which does not make sense to me. Am I missing something or is this a bug?:
While trying to access an actor from a main actor-isolated task group in Swift 6 I get the following error Main actor-isolated value of type '() async -> ()' passed as a strongly transferred parameter; later accesses could race
.
actor MyActor {
func foo() { }
}
func a() {
Task { @MainActor in
let myActor = MyActor()
await withTaskGroup(of: Void.self) { taskGroup in
taskGroup.addTask { @MainActor in // 🛑 Main actor-isolated value of type '() async -> ()' passed as a strongly transferred parameter; later accesses could race
await myActor.foo()
}
}
}
}
When I remove the @MainActor
it compiles just fine:
func b() {
Task {
let myActor = MyActor()
await withTaskGroup(of: Void.self) { taskGroup in
taskGroup.addTask {
await myActor.foo()
}
}
}
}
And with a sendable class it works fine too:
final class MyClass: @unchecked Sendable {
func foo() { }
}
func c() {
Task { @MainActor in
let myClass = MyClass()
await withTaskGroup(of: Void.self) { taskGroup in
taskGroup.addTask { @MainActor in
myClass.foo()
}
}
}
}