Open jumhyn-browser opened 1 month ago
here's a reduction for future triaging/investigations:
func f() async {
await withTaskGroup(of: Void.self) { group in
group.addTask { @MainActor in
await Task.yield()
}
}
}
/*
<source>:3:15: error: sending value of non-Sendable type '() async -> ()' risks causing data races
1 | func f() async {
2 | await withTaskGroup(of: Void.self) { group in
3 | group.addTask { @MainActor in
| |- error: sending value of non-Sendable type '() async -> ()' risks causing data races
| `- note: Passing main actor-isolated value of non-Sendable type '() async -> ()' as a 'sending' parameter risks causing races inbetween main actor-isolated uses and uses reachable from the callee
4 | await Task.yield()
5 | }
*/
it also appears it's not unique to the main actor – any global actor exhibits the issue. the following approaches all seem to avoid the diagnostic:
func f() async {
await withTaskGroup(of: Void.self) { group in
// 1
let op: @MainActor () async -> Void = { await Task.yield() }
group.addTask(operation: op)
// 2
group.addTask(operation: { await Task.yield() } as @MainActor () async -> Void)
// 3
group.addTask { @Sendable @MainActor in
await Task.yield()
}
}
}
edit: this is perhaps the same underlying issue as https://github.com/swiftlang/swift/issues/76242
Description
In the below example, the call to
addTask
which callsh
give a concurrency warning that seems incorrect. The given error is:Am I missing something about why this is not allowable in the general case?
Reproduced on godbolt nightly but have seen the same/similar issue adopting Swift 6.0
Reproduction
Expected behavior
Both calls to
addTask
compile without warning.Environment
Additional information
No response