Skip to content

Commit 3b7becc

Browse files
authored
Fix Swift <5.7 (#69)
1 parent 6130ecf commit 3b7becc

File tree

1 file changed

+104
-108
lines changed

1 file changed

+104
-108
lines changed

Sources/XCTestDynamicOverlay/Internal/GeneratePlaceholder.swift

Lines changed: 104 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,7 @@ extension ExpressibleByIntegerLiteral { fileprivate static var placeholder: Self
88
extension ExpressibleByUnicodeScalarLiteral { fileprivate static var placeholder: Self { " " } }
99
extension RangeReplaceableCollection { fileprivate static var placeholder: Self { Self() } }
1010

11-
#if swift(>=5.7)
12-
13-
private func _placeholder<Result>() -> Result? {
14-
switch Result.self {
15-
case let type as _DefaultInitializable.Type: return type.placeholder as? Result
16-
case is Void.Type: return () as? Result
17-
case let type as any RangeReplaceableCollection.Type: return type.placeholder as? Result
18-
case let type as any AdditiveArithmetic.Type: return type.placeholder as? Result
19-
case let type as any ExpressibleByArrayLiteral.Type: return type.placeholder as? Result
20-
case let type as any ExpressibleByBooleanLiteral.Type: return type.placeholder as? Result
21-
case let type as any ExpressibleByDictionaryLiteral.Type: return type.placeholder as? Result
22-
case let type as any ExpressibleByFloatLiteral.Type: return type.placeholder as? Result
23-
case let type as any ExpressibleByIntegerLiteral.Type: return type.placeholder as? Result
24-
case let type as any ExpressibleByUnicodeScalarLiteral.Type: return type.placeholder as? Result
25-
default: return nil
26-
}
27-
}
28-
29-
private protocol _OptionalProtocol { static var none: Self { get }}
11+
private protocol _OptionalProtocol { static var none: Self { get } }
3012
extension Optional: _OptionalProtocol {}
3113
private func _optionalPlaceholder<Result>() throws -> Result {
3214
if let result = (Result.self as? any _OptionalProtocol.Type) {
@@ -35,118 +17,132 @@ private func _optionalPlaceholder<Result>() throws -> Result {
3517
throw PlaceholderGenerationFailure()
3618
}
3719

38-
private func _rawRepresentable<Result>() -> Result? {
39-
func posiblePlaceholder<T: RawRepresentable>(for type: T.Type) -> T? {
40-
(_placeholder() as T.RawValue?).flatMap(T.init(rawValue:))
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+
}
4155
}
56+
#else
57+
private func _placeholder<Result>() -> Result? {
58+
if let result = (Result.self as? _DefaultInitializable.Type)?.placeholder {
59+
return result as? Result
60+
}
4261

43-
return (Result.self as? any RawRepresentable.Type).flatMap {
44-
posiblePlaceholder(for: $0) as? Result
45-
}
46-
}
62+
if Result.self == Void.self {
63+
return () as? Result
64+
}
4765

48-
private func _caseIterable<Result>() -> Result? {
49-
func firstCase<T: CaseIterable>(for type: T.Type) -> Result? {
50-
T.allCases.first as? Result
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+
}
5177
}
5278

53-
return (Result.self as? any CaseIterable.Type).flatMap {
54-
firstCase(for: $0)
79+
private func _rawRepresentable<Result>() -> Result? {
80+
(Witness<Result>.self as? AnyRawRepresentable.Type).flatMap {
81+
$0.possiblePlaceholder as? Result
82+
}
5583
}
56-
}
57-
58-
#else
5984

60-
private func _placeholder<Result>() -> Result? {
61-
if let result = (Result.self as? _DefaultInitializable.Type)?.placeholder {
62-
return result as? Result
85+
private func _caseIterable<Result>() -> Result? {
86+
(Witness<Result>.self as? AnyCaseIterable.Type).flatMap {
87+
$0.firstCase as? Result
88+
}
6389
}
6490

65-
if Result.self == Void.self {
66-
return () as? Result
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 }
6795
}
6896

69-
switch Witness<Result>.self {
70-
case let type as AnyRangeReplaceableCollection.Type: return type.placeholder as? Result
71-
case let type as AnyAdditiveArithmetic.Type: return type.placeholder as? Result
72-
case let type as AnyExpressibleByArrayLiteral.Type: return type.placeholder as? Result
73-
case let type as AnyExpressibleByBooleanLiteral.Type: return type.placeholder as? Result
74-
case let type as AnyExpressibleByDictionaryLiteral.Type: return type.placeholder as? Result
75-
case let type as AnyExpressibleByFloatLiteral.Type: return type.placeholder as? Result
76-
case let type as AnyExpressibleByIntegerLiteral.Type: return type.placeholder as? Result
77-
case let type as AnyExpressibleByUnicodeScalarLiteral.Type: return type.placeholder as? Result
78-
default: return nil
97+
private protocol AnyExpressibleByArrayLiteral { static var placeholder: Any { get } }
98+
extension Witness: AnyExpressibleByArrayLiteral where Value: ExpressibleByArrayLiteral {
99+
fileprivate static var placeholder: Any { Value.placeholder }
79100
}
80-
}
81101

82-
private func _rawRepresentable<Result>() -> Result? {
83-
(Witness<Result>.self as? AnyRawRepresentable.Type).flatMap {
84-
$0.possiblePlaceholder as? Result
102+
private protocol AnyExpressibleByBooleanLiteral { static var placeholder: Any { get } }
103+
extension Witness: AnyExpressibleByBooleanLiteral where Value: ExpressibleByBooleanLiteral {
104+
fileprivate static var placeholder: Any { Value.placeholder }
85105
}
86-
}
87106

88-
private func _caseIterable<Result>() -> Result? {
89-
(Witness<Result>.self as? AnyCaseIterable.Type).flatMap {
90-
$0.firstCase as? Result
107+
private protocol AnyExpressibleByDictionaryLiteral { static var placeholder: Any { get } }
108+
extension Witness: AnyExpressibleByDictionaryLiteral where Value: ExpressibleByDictionaryLiteral {
109+
fileprivate static var placeholder: Any { Value.placeholder }
91110
}
92-
}
93-
94-
private enum Witness<Value> {}
95-
private protocol AnyAdditiveArithmetic { static var placeholder: Any { get } }
96-
extension Witness: AnyAdditiveArithmetic where Value: AdditiveArithmetic {
97-
fileprivate static var placeholder: Any { Value.placeholder }
98-
}
99111

100-
private protocol AnyExpressibleByArrayLiteral { static var placeholder: Any { get } }
101-
extension Witness: AnyExpressibleByArrayLiteral where Value: ExpressibleByArrayLiteral {
102-
fileprivate static var placeholder: Any { Value.placeholder }
103-
}
104-
105-
private protocol AnyExpressibleByBooleanLiteral { static var placeholder: Any { get } }
106-
extension Witness: AnyExpressibleByBooleanLiteral where Value: ExpressibleByBooleanLiteral {
107-
fileprivate static var placeholder: Any { Value.placeholder }
108-
}
109-
110-
private protocol AnyExpressibleByDictionaryLiteral { static var placeholder: Any { get } }
111-
extension Witness: AnyExpressibleByDictionaryLiteral where Value: ExpressibleByDictionaryLiteral {
112-
fileprivate static var placeholder: Any { Value.placeholder }
113-
}
114-
115-
private protocol AnyExpressibleByFloatLiteral { static var placeholder: Any { get } }
116-
extension Witness: AnyExpressibleByFloatLiteral where Value: ExpressibleByFloatLiteral {
117-
fileprivate static var placeholder: Any { Value.placeholder }
118-
}
119-
120-
private protocol AnyExpressibleByIntegerLiteral { static var placeholder: Any { get } }
121-
extension Witness: AnyExpressibleByIntegerLiteral where Value: ExpressibleByIntegerLiteral {
122-
fileprivate static var placeholder: Any { Value.placeholder }
123-
}
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+
}
124116

125-
private protocol AnyExpressibleByUnicodeScalarLiteral { static var placeholder: Any { get } }
126-
extension Witness: AnyExpressibleByUnicodeScalarLiteral
127-
where Value: ExpressibleByUnicodeScalarLiteral {
128-
fileprivate static var placeholder: Any { Value.placeholder }
129-
}
117+
private protocol AnyExpressibleByIntegerLiteral { static var placeholder: Any { get } }
118+
extension Witness: AnyExpressibleByIntegerLiteral where Value: ExpressibleByIntegerLiteral {
119+
fileprivate static var placeholder: Any { Value.placeholder }
120+
}
130121

131-
private protocol AnyRangeReplaceableCollection { static var placeholder: Any { get } }
132-
extension Witness: AnyRangeReplaceableCollection where Value: RangeReplaceableCollection {
133-
fileprivate static var placeholder: Any { Value.placeholder }
134-
}
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 }
126+
}
135127

136-
private protocol AnyRawRepresentable { static var possiblePlaceholder: Any? { get } }
137-
extension Witness: AnyRawRepresentable where Value: RawRepresentable {
138-
fileprivate static var possiblePlaceholder: Any? {
139-
(_placeholder() as Value.RawValue?).flatMap(Value.init(rawValue:))
128+
private protocol AnyRangeReplaceableCollection { static var placeholder: Any { get } }
129+
extension Witness: AnyRangeReplaceableCollection where Value: RangeReplaceableCollection {
130+
fileprivate static var placeholder: Any { Value.placeholder }
140131
}
141-
}
142132

143-
private protocol AnyCaseIterable { static var firstCase: Any? { get } }
144-
extension Witness: AnyCaseIterable where Value: CaseIterable {
145-
fileprivate static var firstCase: Any? {
146-
Value.allCases.first
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+
}
147138
}
148-
}
149139

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+
}
145+
}
150146
#endif
151147

152148
struct PlaceholderGenerationFailure: Error {}
@@ -162,6 +158,6 @@ func _generatePlaceholder<Result>() throws -> Result {
162158
if let result = _caseIterable() as Result? {
163159
return result
164160
}
165-
161+
166162
return try _optionalPlaceholder()
167163
}

0 commit comments

Comments
 (0)