Avatar
Avatar
ジュニア
@swift-6.0.3 -swift-version 6 final class NonSendableCounter { var count = 0 nonisolated func increment() async { count += 1 } } // @MainActor func foo() async { let counter = NonSendableCounter() var tasks: [Task<(), Never>] = [] for _ in 0..<10000 { let task = Task { await counter.increment() // NG? } tasks.append(task) } for task in tasks { await task.value } print(counter.count) } await foo()
exit status: 1 with <stdin>:14:20: error: value of non-Sendable type '@isolated(any) @async @callee_guaranteed @substituted <τ_0_0> () -> @out τ_0_0 for <()>' accessed after being transferred; later accesses could race 12 | var tasks: [Task<(), Never>] = [] 13 | for _ in 0..<10000 { 14 | let task = Task { | |- error: value of non-Sendable type '@isolated(any) @async @callee_guaranteed @substituted <τ_0_0> () -> @out τ_0_0 for <()>' accessed after being transferred; later accesses could race | `- note: access can happen concurrently 15 | await counter.increment() // NG? 16 | }