Skip to content

Commit cf0f6b1

Browse files
authored
Handle Swift 6.2 sendability changes (#789)
SendableMetatype is a marker protocol, so while it doesn't have runtime availability requirements, it's only available with >= 6.2 Swift compiler. This uses an underscored protocol to conditionally include conformance without repeating the entire declaration (thanks @rauhul).
1 parent 932e6cd commit cf0f6b1

File tree

7 files changed

+26
-3
lines changed

7 files changed

+26
-3
lines changed

Sources/ArgumentParser/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ add_library(ArgumentParser
4646
Utilities/Platform.swift
4747
Utilities/SequenceExtensions.swift
4848
Utilities/StringExtensions.swift
49+
Utilities/SwiftExtensions.swift
4950
Utilities/Tree.swift
5051

5152
Validators/CodingKeyValidator.swift

Sources/ArgumentParser/Parsable Types/ExpressibleByArgument.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
/// A type that can be expressed as a command-line argument.
13-
public protocol ExpressibleByArgument {
13+
public protocol ExpressibleByArgument: _SendableMetatype {
1414
/// Creates a new instance of this type from a command-line-specified
1515
/// argument.
1616
init?(argument: String)

Sources/ArgumentParser/Parsable Types/ParsableArguments.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
///
1414
/// When you implement a `ParsableArguments` type, all properties must be declared with
1515
/// one of the four property wrappers provided by the `ArgumentParser` library.
16-
public protocol ParsableArguments: Decodable {
16+
public protocol ParsableArguments: Decodable, _SendableMetatype {
1717
/// Creates an instance of this parsable type using the definitions
1818
/// given by each property's wrapper.
1919
init()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Argument Parser open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
#if compiler(>=6.2)
13+
/// Designates a type as having a sendable metatype.
14+
public protocol _SendableMetatype: SendableMetatype {}
15+
#else
16+
public protocol _SendableMetatype {}
17+
#endif

Tests/ArgumentParserEndToEndTests/CustomParsingEndToEndTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,19 @@ extension Array where Element == Name {
3535
// MARK: -
3636

3737
private struct Foo: ParsableCommand {
38-
enum Subgroup: Equatable {
38+
enum Subgroup: Equatable, Sendable {
3939
case first(Int)
4040
case second(Int)
4141

42+
@Sendable
4243
static func makeFirst(_ str: String) throws -> Subgroup {
4344
guard let value = Int(str) else {
4445
throw ValidationError("Not a valid integer for 'first'")
4546
}
4647
return .first(value)
4748
}
4849

50+
@Sendable
4951
static func makeSecond(_ str: String) throws -> Subgroup {
5052
guard let value = Int(str) else {
5153
throw ValidationError("Not a valid integer for 'second'")

Tests/ArgumentParserUnitTests/HelpGenerationTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ extension HelpGenerationTests {
148148
enum OptionFlags: String, EnumerableFlag { case optional, required }
149149
enum Degree {
150150
case bachelor, graduate, doctorate
151+
152+
@Sendable
151153
static func degreeTransform(_ string: String) throws -> Degree {
152154
switch string {
153155
case "bachelor":

Tests/ArgumentParserUnitTests/UsageGenerationTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ extension UsageGenerationTests {
132132
enum Color {
133133
case red, blue
134134

135+
@Sendable
135136
static func transform(_ string: String) throws -> Color {
136137
switch string {
137138
case "red":

0 commit comments

Comments
 (0)