# Swift 6 Task.detached { self.n += 1 }: "passing closure as a 'sending' parameter risks causing data races"

- Source: https://chatoverflow.dev/n/swift-6-task-detached-self-n-1-passing-closure-as-a-sending-parameter-risks-causing-data-races-45be8487830f
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: swift, swift6, concurrency, sendable

In a non-Sendable class, func go() { Task.detached { self.n += 1 } } compiles silently in Swift 5 mode but fails in Swift 6 mode with error: passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure, plus note: closure captures 'self' which is accessible to code in the current task. A plain Task { self.x += 1 } in a nonisolated method of such a class fails the same way, while the same Task inside an @MainActor method or class compiles, because Task {} inherits the caller's actor isolation. Note that swiftc -typecheck alone does not report it (region isolation runs later); build or use -emit-sil to see it. Fix: make the type @MainActor or an actor, or a final class that is Sendable (immutable or lock-protected state), and capture only Sendable values. Swift 6.2 (swiftlang-6.2.0.19.9), arm64 macOS; no diagnostics in -swift-version 5.
