apple / swift-argument-parser

Straightforward, type-safe argument parsing for Swift
Apache License 2.0
3.31k stars 311 forks source link

Add Sendable conformance #582

Closed vlm closed 10 months ago

vlm commented 1 year ago

It can be useful to send AsyncParsableCommand as an argument to some function. This is trivial to do in a non-async context. However, in async context the type that is sent as an argument has to be Sendable. This patch adds Sendable conformance so this becomes expressible and warning-free:

struct Args: AsyncParsableCommand, Sendable {
    func run() async throws {
        Task { await otherFunction(arguments: self) }
    }
}

The warnings will become errors in Swift 6, so this is a forward-looking change.

Checklist

natecook1000 commented 1 year ago

Thanks for this, @vlm! Do we not need to declare the Parsed enum as sendable as well? That can indirectly store a user-supplied closure via initializers that include a transform parameter, which could break encapsulation. Should we look at adding @Sendable annotations to those closures at the same time?

vlm commented 1 year ago

@natecook1000 looking...

vlm commented 1 year ago

@natecook1000 these conformances doesn't appear to be required. Added a test to showcase that. No warnings.

https://github.com/apple/swift-argument-parser/pull/582/files#diff-1d0a514c56a2c811f846acdce3410d034f8008d908b6180d6a4fa1d536a279c3R36

natecook1000 commented 10 months ago

@swift-ci Please test

rauhul commented 10 months ago

Is it possible to put the additional sendable restrictions behind conditional compilation so this change isn't source breaking?

natecook1000 commented 10 months ago

@rauhul None of these changes should be source breaking, since checking is only a warning until you upgrade to Swift 6 mode. Additionally, to turn off the warnings making other source changes, a package that depends on ArgumentParser can add @preconcurrency to their import statement.

natecook1000 commented 10 months ago

@swift-ci Please test