Skip to content

Commit 104326b

Browse files
authored
Make XCTestContext sendable (#74)
With concurrency warnings dialed up, this type can cause warnings to be emitted, but as a task local should be safe to pass along.
1 parent 89a3632 commit 104326b

File tree

3 files changed

+29
-121
lines changed

3 files changed

+29
-121
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.5
1+
// swift-tools-version:5.7
22

33
import PackageDescription
44

@@ -22,7 +22,7 @@ let package = Package(
2222
]
2323
)
2424

25-
#if swift(>=5.6) && !os(Windows)
25+
#if !os(Windows)
2626
// Add the documentation compiler plugin if possible
2727
package.dependencies.append(
2828
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0")

Sources/XCTestDynamicOverlay/Internal/GeneratePlaceholder.swift

Lines changed: 26 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -17,133 +17,41 @@ private func _optionalPlaceholder<Result>() throws -> Result {
1717
throw PlaceholderGenerationFailure()
1818
}
1919

20-
#if swift(>=5.7)
21-
private func _placeholder<Result>() -> Result? {
22-
switch Result.self {
23-
case let type as _DefaultInitializable.Type: return type.placeholder as? Result
24-
case is Void.Type: return () as? Result
25-
case let type as any RangeReplaceableCollection.Type: return type.placeholder as? Result
26-
case let type as any AdditiveArithmetic.Type: return type.placeholder as? Result
27-
case let type as any ExpressibleByArrayLiteral.Type: return type.placeholder as? Result
28-
case let type as any ExpressibleByBooleanLiteral.Type: return type.placeholder as? Result
29-
case let type as any ExpressibleByDictionaryLiteral.Type: return type.placeholder as? Result
30-
case let type as any ExpressibleByFloatLiteral.Type: return type.placeholder as? Result
31-
case let type as any ExpressibleByIntegerLiteral.Type: return type.placeholder as? Result
32-
case let type as any ExpressibleByUnicodeScalarLiteral.Type: return type.placeholder as? Result
33-
default: return nil
34-
}
35-
}
36-
37-
private func _rawRepresentable<Result>() -> Result? {
38-
func posiblePlaceholder<T: RawRepresentable>(for type: T.Type) -> T? {
39-
(_placeholder() as T.RawValue?).flatMap(T.init(rawValue:))
40-
}
41-
42-
return (Result.self as? any RawRepresentable.Type).flatMap {
43-
posiblePlaceholder(for: $0) as? Result
44-
}
45-
}
46-
47-
private func _caseIterable<Result>() -> Result? {
48-
func firstCase<T: CaseIterable>(for type: T.Type) -> Result? {
49-
T.allCases.first as? Result
50-
}
51-
52-
return (Result.self as? any CaseIterable.Type).flatMap {
53-
firstCase(for: $0)
54-
}
55-
}
56-
#else
57-
private func _placeholder<Result>() -> Result? {
58-
if let result = (Result.self as? _DefaultInitializable.Type)?.placeholder {
59-
return result as? Result
60-
}
61-
62-
if Result.self == Void.self {
63-
return () as? Result
64-
}
65-
66-
switch Witness<Result>.self {
67-
case let type as AnyRangeReplaceableCollection.Type: return type.placeholder as? Result
68-
case let type as AnyAdditiveArithmetic.Type: return type.placeholder as? Result
69-
case let type as AnyExpressibleByArrayLiteral.Type: return type.placeholder as? Result
70-
case let type as AnyExpressibleByBooleanLiteral.Type: return type.placeholder as? Result
71-
case let type as AnyExpressibleByDictionaryLiteral.Type: return type.placeholder as? Result
72-
case let type as AnyExpressibleByFloatLiteral.Type: return type.placeholder as? Result
73-
case let type as AnyExpressibleByIntegerLiteral.Type: return type.placeholder as? Result
74-
case let type as AnyExpressibleByUnicodeScalarLiteral.Type: return type.placeholder as? Result
75-
default: return nil
76-
}
77-
}
78-
79-
private func _rawRepresentable<Result>() -> Result? {
80-
(Witness<Result>.self as? AnyRawRepresentable.Type).flatMap {
81-
$0.possiblePlaceholder as? Result
82-
}
83-
}
84-
85-
private func _caseIterable<Result>() -> Result? {
86-
(Witness<Result>.self as? AnyCaseIterable.Type).flatMap {
87-
$0.firstCase as? Result
88-
}
89-
}
90-
91-
private enum Witness<Value> {}
92-
private protocol AnyAdditiveArithmetic { static var placeholder: Any { get } }
93-
extension Witness: AnyAdditiveArithmetic where Value: AdditiveArithmetic {
94-
fileprivate static var placeholder: Any { Value.placeholder }
95-
}
96-
97-
private protocol AnyExpressibleByArrayLiteral { static var placeholder: Any { get } }
98-
extension Witness: AnyExpressibleByArrayLiteral where Value: ExpressibleByArrayLiteral {
99-
fileprivate static var placeholder: Any { Value.placeholder }
100-
}
101-
102-
private protocol AnyExpressibleByBooleanLiteral { static var placeholder: Any { get } }
103-
extension Witness: AnyExpressibleByBooleanLiteral where Value: ExpressibleByBooleanLiteral {
104-
fileprivate static var placeholder: Any { Value.placeholder }
105-
}
106-
107-
private protocol AnyExpressibleByDictionaryLiteral { static var placeholder: Any { get } }
108-
extension Witness: AnyExpressibleByDictionaryLiteral where Value: ExpressibleByDictionaryLiteral {
109-
fileprivate static var placeholder: Any { Value.placeholder }
110-
}
111-
112-
private protocol AnyExpressibleByFloatLiteral { static var placeholder: Any { get } }
113-
extension Witness: AnyExpressibleByFloatLiteral where Value: ExpressibleByFloatLiteral {
114-
fileprivate static var placeholder: Any { Value.placeholder }
115-
}
116-
117-
private protocol AnyExpressibleByIntegerLiteral { static var placeholder: Any { get } }
118-
extension Witness: AnyExpressibleByIntegerLiteral where Value: ExpressibleByIntegerLiteral {
119-
fileprivate static var placeholder: Any { Value.placeholder }
20+
private func _placeholder<Result>() -> Result? {
21+
switch Result.self {
22+
case let type as _DefaultInitializable.Type: return type.placeholder as? Result
23+
case is Void.Type: return () as? Result
24+
case let type as any RangeReplaceableCollection.Type: return type.placeholder as? Result
25+
case let type as any AdditiveArithmetic.Type: return type.placeholder as? Result
26+
case let type as any ExpressibleByArrayLiteral.Type: return type.placeholder as? Result
27+
case let type as any ExpressibleByBooleanLiteral.Type: return type.placeholder as? Result
28+
case let type as any ExpressibleByDictionaryLiteral.Type: return type.placeholder as? Result
29+
case let type as any ExpressibleByFloatLiteral.Type: return type.placeholder as? Result
30+
case let type as any ExpressibleByIntegerLiteral.Type: return type.placeholder as? Result
31+
case let type as any ExpressibleByUnicodeScalarLiteral.Type: return type.placeholder as? Result
32+
default: return nil
12033
}
34+
}
12135

122-
private protocol AnyExpressibleByUnicodeScalarLiteral { static var placeholder: Any { get } }
123-
extension Witness: AnyExpressibleByUnicodeScalarLiteral
124-
where Value: ExpressibleByUnicodeScalarLiteral {
125-
fileprivate static var placeholder: Any { Value.placeholder }
36+
private func _rawRepresentable<Result>() -> Result? {
37+
func posiblePlaceholder<T: RawRepresentable>(for type: T.Type) -> T? {
38+
(_placeholder() as T.RawValue?).flatMap(T.init(rawValue:))
12639
}
12740

128-
private protocol AnyRangeReplaceableCollection { static var placeholder: Any { get } }
129-
extension Witness: AnyRangeReplaceableCollection where Value: RangeReplaceableCollection {
130-
fileprivate static var placeholder: Any { Value.placeholder }
41+
return (Result.self as? any RawRepresentable.Type).flatMap {
42+
posiblePlaceholder(for: $0) as? Result
13143
}
44+
}
13245

133-
private protocol AnyRawRepresentable { static var possiblePlaceholder: Any? { get } }
134-
extension Witness: AnyRawRepresentable where Value: RawRepresentable {
135-
fileprivate static var possiblePlaceholder: Any? {
136-
(_placeholder() as Value.RawValue?).flatMap(Value.init(rawValue:))
137-
}
46+
private func _caseIterable<Result>() -> Result? {
47+
func firstCase<T: CaseIterable>(for type: T.Type) -> Result? {
48+
T.allCases.first as? Result
13849
}
13950

140-
private protocol AnyCaseIterable { static var firstCase: Any? { get } }
141-
extension Witness: AnyCaseIterable where Value: CaseIterable {
142-
fileprivate static var firstCase: Any? {
143-
Value.allCases.first
144-
}
51+
return (Result.self as? any CaseIterable.Type).flatMap {
52+
firstCase(for: $0)
14553
}
146-
#endif
54+
}
14755

14856
struct PlaceholderGenerationFailure: Error {}
14957
func _generatePlaceholder<Result>() throws -> Result {

Sources/XCTestDynamicOverlay/XCTFail.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Foundation
22

3-
public struct XCTFailContext {
3+
public struct XCTFailContext: Sendable {
44
@TaskLocal public static var current: Self?
55

66
public var file: StaticString

0 commit comments

Comments
 (0)