Open
Description
Description
In a main actor isolated function, it is possible to send non-sendable values to a non-isolated async function by calling that function in a Task { ... }
.
Reproduction
class C {
var x: Int = 0
}
nonisolated func f(_ c: C) async {
c.x += 1
}
@MainActor
func run() async {
let c = C()
let t1 = Task {
await f(c)
}
let t2 = Task {
await f(c)
}
await t1.value
await t2.value
print(c.x)
}
Expected behavior
The code should produce an error about sending the non-sendable c
.
Environment
swift-driver version: 1.127.5.3 Apple Swift version 6.2 (swiftlang-6.2.0.10.950 clang-1700.3.10.950)
Target: arm64-apple-macosx15.0
Additional information
Compare this with alternative versions using async let
, or a non-isolated run
. Both of these produce errors as expected.
@MainActor
func run() async {
let c = C()
async let t1 = f(c)
async let t2 = f(c)
await (t1, t2)
print(c.x)
}
func run() async {
let c = C()
let t1 = Task {
await f(c)
}
let t2 = Task {
await f(c)
}
await t1.value
await t2.value
print(c.x)
}
From a Stack Overflow post.