Description
Description
With Swift 6 language mode, It is not possible to call withTaskCancellationHandler
and withCheckedContinuation(isolation:, body:)
within a function on Xcode 16 Beta 6 / Linux. Compilation fails with error:
❌ error: sending 'body' risks causing data races
- ❌ Linux Swift 6 language mode
⚠️ Linux Swift 5 language mode (⚠️ warns with.enableExperimentalFeature("StrictConcurrency")
)- ✅ macOS Swift 6 language mode (Xcode 16 beta 5)
- ❌ macOS Swift 6 language mode (Xcode 16 beta 6)
⚠️ macOS Swift 5 language mode (Xcode 16 beta 6)
Reproduction
Compile the following using Swift 6 language mode on linux / Xcode 16 beta 6
% swiftc main.swift -swift-version 6
func withContinuation<T>(
isolation: isolated (any Actor)? = #isolation,
body: (CheckedContinuation<T, Never>) -> Void
) async -> T {
await withTaskCancellationHandler {
await withCheckedContinuation(isolation: isolation) {
body($0) // ❌ error: sending 'body' risks causing data races
}
} onCancel: {
}
}
Removing withTaskCancellationHandler { }
compiles and runs correctly:
func withContinuation<T>(
isolation: isolated (any Actor)? = #isolation,
body: (CheckedContinuation<T, Never>) -> Void
) async -> T {
await withCheckedContinuation(isolation: isolation) {
body($0)
}
}
Removing isolation
from withCheckedContinuation
compiles, but isolation is not preserved and body is executed on another executor.
func withContinuation<T>(
isolation: isolated (any Actor)? = #isolation,
body: (CheckedContinuation<T, Never>) -> Void
) async -> T {
await withTaskCancellationHandler {
await withCheckedContinuation {
body($0)
}
} onCancel: {
}
}
actor Foo {
func bar() async {
await withContinuation {
assertIsolated()
$0.resume()
}
}
}
await Foo().bar() // ❌ Incorrect actor executor assumption 💣 Program crashed:
Expected behavior
Program compiles fine and body closure is executed within the current isolation.
Environment
Swift version 6.0-dev (LLVM a0bfc8c8fd6ffd6, Swift 629aa83)
Target: x86_64-unknown-linux-gnu
Docker
swiftlang/swift nightly-6.0-jammy eafb9a2f9d84
Additional information
No response