Skip to content

Error about non-Sendable type when using Swift 5 mode with @preconcurrency API #76652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
michaeljtsai opened this issue Sep 23, 2024 · 1 comment
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself concurrency Feature: umbrella label for concurrency language features expressions Feature: expressions Sendable Area → standard library: The Sendable protocol type checker Area → compiler: Semantic analysis

Comments

@michaeljtsai
Copy link

michaeljtsai commented Sep 23, 2024

Description

After updating to Swift 6, I am getting Swift Concurrency warnings, even with the Swift 5 language mode, for code that used to compile cleanly. For example, the example below produces this error:

warning: capture of 'buffer' with non-sendable type 'UnsafeMutableBufferPointer<NSNumber>' in a `@Sendable` closure; this is an error in the Swift 6 language mode
        buffer[index] = NSNumber(value: index)
        ^
Swift.UnsafeMutableBufferPointer:1:23: note: generic struct 'UnsafeMutableBufferPointer' does not conform to the 'Sendable' protocol
@frozen public struct UnsafeMutableBufferPointer<Element> : Copyable where Element : ~Copyable {

Reproduction

I can reproduce this issue with a CLI project created in Xcode 16, configured to use Swift 5 language mode.

let count = 10
let array = Array(unsafeUninitializedCapacity: count) { (buffer, initializedCount) in
    DispatchQueue.concurrentPerform(iterations: count) { index in
        buffer[index] = NSNumber(value: index)
    }
    initializedCount = count
}

Expected behavior

I expected this to compile without any warnings or errors using the Swift 5 mode of the Swift 6 compiler, as it did with Swift 5 and Xcode 15. DispatchQueue.concurrentPerform() is annotated with @preconcurrency, which I thought was supposed to suppress these sorts of warnings.

Environment

Version 16.1 beta 2 (16B5014f)
swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.11 clang-1600.0.26.2)
Target: x86_64-apple-macosx15.0

Additional information

The problem does not occur if I write it like this:

let count = 10
let array = Array(unsafeUninitializedCapacity: count) { (buffer, initializedCount) in
    let closure = { (index: Int) in
        buffer[index] = NSNumber(value: index)    
    }
    DispatchQueue.concurrentPerform(iterations: count, execute: closure)
    initializedCount = count
}

I am also seeing similar issues calling DispatchQueue.main.sync() as well as these APIs:

@MainActor func filePromiseProvider(_ filePromiseProvider: NSFilePromiseProvider, fileNameForType fileType: String) -> String
nonisolated func filePromiseProvider(_ filePromiseProvider: NSFilePromiseProvider, writePromiseTo url: URL) async throws

and can provide more info on that if you think these are separate issues.

@michaeljtsai michaeljtsai added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Sep 23, 2024
@xedin xedin added concurrency Feature: umbrella label for concurrency language features compiler The Swift compiler itself type checker Area → compiler: Semantic analysis expressions Feature: expressions Sendable Area → standard library: The Sendable protocol and removed triage needed This issue needs more specific labels labels Sep 23, 2024
@hborla hborla self-assigned this Oct 21, 2024
@hborla
Copy link
Member

hborla commented Nov 14, 2024

I merged a fix for this to both main and release/6.0.

@hborla hborla closed this as completed Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself concurrency Feature: umbrella label for concurrency language features expressions Feature: expressions Sendable Area → standard library: The Sendable protocol type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants