Skip to content

Commit 132d1b5

Browse files
committed
Add enableUpcomingFeature("ExistentialAny") and update accordingly.
Enable the feature on all targets, and then go through doing the fixits within Xcode 15 that are triggered as a result. This basically replaces apple#1509
1 parent 0d922f0 commit 132d1b5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+227
-206
lines changed

Package.swift

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,42 @@ let package = Package(
3939
name: "SwiftProtobuf",
4040
exclude: ["CMakeLists.txt"],
4141
swiftSettings: [
42+
.enableUpcomingFeature("ExistentialAny"),
4243
.enableExperimentalFeature("StrictConcurrency=complete"),
4344
]
4445
),
4546
.target(
4647
name: "SwiftProtobufPluginLibrary",
4748
dependencies: ["SwiftProtobuf"],
48-
exclude: ["CMakeLists.txt"]
49+
exclude: ["CMakeLists.txt"],
50+
swiftSettings: [
51+
.enableUpcomingFeature("ExistentialAny"),
52+
]
4953
),
5054
.target(
5155
name: "SwiftProtobufTestHelpers",
5256
dependencies: ["SwiftProtobuf"],
5357
swiftSettings: [
58+
.enableUpcomingFeature("ExistentialAny"),
5459
.enableExperimentalFeature("StrictConcurrency=complete"),
5560
]
5661
),
5762
.executableTarget(
5863
name: "protoc-gen-swift",
5964
dependencies: ["SwiftProtobufPluginLibrary", "SwiftProtobuf"],
60-
exclude: ["CMakeLists.txt"]
65+
exclude: ["CMakeLists.txt"],
66+
swiftSettings: [
67+
.enableUpcomingFeature("ExistentialAny")
68+
]
6169
),
6270
.executableTarget(
6371
name: "Conformance",
6472
dependencies: ["SwiftProtobuf"],
65-
exclude: ["failure_list_swift.txt", "text_format_failure_list_swift.txt"]
73+
exclude: ["failure_list_swift.txt", "text_format_failure_list_swift.txt"],
74+
swiftSettings: [
75+
.enableUpcomingFeature("ExistentialAny"),
76+
.enableExperimentalFeature("StrictConcurrency=complete"),
77+
]
6678
),
6779
.plugin(
6880
name: "SwiftProtobufPlugin",
@@ -75,16 +87,25 @@ let package = Package(
7587
name: "SwiftProtobufTests",
7688
dependencies: ["SwiftProtobuf"],
7789
swiftSettings: [
90+
.enableUpcomingFeature("ExistentialAny"),
7891
.enableExperimentalFeature("StrictConcurrency=complete"),
7992
]
8093
),
8194
.testTarget(
8295
name: "SwiftProtobufPluginLibraryTests",
83-
dependencies: ["SwiftProtobufPluginLibrary", "SwiftProtobufTestHelpers"]
96+
dependencies: ["SwiftProtobufPluginLibrary", "SwiftProtobufTestHelpers"],
97+
swiftSettings: [
98+
.enableUpcomingFeature("ExistentialAny"),
99+
.enableExperimentalFeature("StrictConcurrency=complete"),
100+
]
84101
),
85102
.testTarget(
86103
name: "protoc-gen-swiftTests",
87-
dependencies: ["protoc-gen-swift", "SwiftProtobufTestHelpers"]
104+
dependencies: ["protoc-gen-swift", "SwiftProtobufTestHelpers"],
105+
swiftSettings: [
106+
.enableUpcomingFeature("ExistentialAny"),
107+
.enableExperimentalFeature("StrictConcurrency=complete"),
108+
]
88109
),
89110
],
90111
swiftLanguageVersions: [.v5]

Sources/Conformance/main.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ func buildResponse(serializedBytes: [UInt8]) -> Conformance_ConformanceResponse
8484
return response
8585
}
8686

87-
let msgType: SwiftProtobuf.Message.Type
88-
let extensions: SwiftProtobuf.ExtensionMap
87+
let msgType: any SwiftProtobuf.Message.Type
88+
let extensions: any SwiftProtobuf.ExtensionMap
8989
switch request.messageType {
9090
case "":
9191
// Note: This case is here to cover using a old version of the conformance test
@@ -102,7 +102,7 @@ func buildResponse(serializedBytes: [UInt8]) -> Conformance_ConformanceResponse
102102
return response
103103
}
104104

105-
let testMessage: SwiftProtobuf.Message
105+
let testMessage: any SwiftProtobuf.Message
106106
switch request.payload {
107107
case .protobufPayload(let data)?:
108108
do {

Sources/SwiftProtobuf/AnyMessageStorage.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
import Foundation
1717

1818
fileprivate func serializeAnyJSON(
19-
for message: Message,
19+
for message: any Message,
2020
typeURL: String,
2121
options: JSONEncodingOptions
2222
) throws -> String {
2323
var visitor = try JSONEncodingVisitor(type: type(of: message), options: options)
2424
visitor.startObject(message: message)
2525
visitor.encodeField(name: "@type", stringValue: typeURL)
26-
if let m = message as? _CustomJSONCodable {
26+
if let m = message as? (any _CustomJSONCodable) {
2727
let value = try m.encodedJSONString(options: options)
2828
visitor.encodeField(name: "value", jsonText: value)
2929
} else {
@@ -33,7 +33,7 @@ fileprivate func serializeAnyJSON(
3333
return visitor.stringResult
3434
}
3535

36-
fileprivate func emitVerboseTextForm(visitor: inout TextFormatEncodingVisitor, message: Message, typeURL: String) {
36+
fileprivate func emitVerboseTextForm(visitor: inout TextFormatEncodingVisitor, message: any Message, typeURL: String) {
3737
let url: String
3838
if typeURL.isEmpty {
3939
url = buildTypeURL(forMessage: message, typePrefix: defaultAnyTypeURLPrefix)
@@ -53,10 +53,10 @@ fileprivate func asJSONObject(body: [UInt8]) -> Data {
5353
}
5454

5555
fileprivate func unpack(contentJSON: [UInt8],
56-
extensions: ExtensionMap,
56+
extensions: any ExtensionMap,
5757
options: JSONDecodingOptions,
58-
as messageType: Message.Type) throws -> Message {
59-
guard messageType is _CustomJSONCodable.Type else {
58+
as messageType: any Message.Type) throws -> any Message {
59+
guard messageType is any _CustomJSONCodable.Type else {
6060
let contentJSONAsObject = asJSONObject(body: contentJSON)
6161
return try messageType.init(jsonUTF8Bytes: contentJSONAsObject, extensions: extensions, options: options)
6262
}
@@ -135,7 +135,7 @@ internal class AnyMessageStorage {
135135
// unpacking that takes new options when a developer decides to decode it.
136136
case binary(Data)
137137
// a message
138-
case message(Message)
138+
case message(any Message)
139139
// parsed JSON with the @type removed and the decoding options.
140140
case contentJSON([UInt8], JSONDecodingOptions)
141141
}
@@ -162,7 +162,7 @@ internal class AnyMessageStorage {
162162
// replaced during the unpacking and never as a merge.
163163
func unpackTo<M: Message>(
164164
target: inout M,
165-
extensions: ExtensionMap?,
165+
extensions: (any ExtensionMap)?,
166166
options: BinaryDecodingOptions
167167
) throws {
168168
guard isA(M.self) else {

Sources/SwiftProtobuf/AsyncMessageSequence.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension AsyncSequence where Element == UInt8 {
3434
@inlinable
3535
public func binaryProtobufDelimitedMessages<M: Message>(
3636
of messageType: M.Type = M.self,
37-
extensions: ExtensionMap? = nil,
37+
extensions: (any ExtensionMap)? = nil,
3838
partial: Bool = false,
3939
options: BinaryDecodingOptions = BinaryDecodingOptions()
4040
) -> AsyncMessageSequence<Self, M> {
@@ -58,7 +58,7 @@ public struct AsyncMessageSequence<
5858
public typealias Element = M
5959

6060
private let base: Base
61-
private let extensions: ExtensionMap?
61+
private let extensions: (any ExtensionMap)?
6262
private let partial: Bool
6363
private let options: BinaryDecodingOptions
6464

@@ -80,7 +80,7 @@ public struct AsyncMessageSequence<
8080
/// messages.
8181
public init(
8282
base: Base,
83-
extensions: ExtensionMap? = nil,
83+
extensions: (any ExtensionMap)? = nil,
8484
partial: Bool = false,
8585
options: BinaryDecodingOptions = BinaryDecodingOptions()
8686
) {
@@ -95,15 +95,15 @@ public struct AsyncMessageSequence<
9595
@usableFromInline
9696
var iterator: Base.AsyncIterator?
9797
@usableFromInline
98-
let extensions: ExtensionMap?
98+
let extensions: (any ExtensionMap)?
9999
@usableFromInline
100100
let partial: Bool
101101
@usableFromInline
102102
let options: BinaryDecodingOptions
103103

104104
init(
105105
iterator: Base.AsyncIterator,
106-
extensions: ExtensionMap?,
106+
extensions: (any ExtensionMap)?,
107107
partial: Bool,
108108
options: BinaryDecodingOptions
109109
) {

Sources/SwiftProtobuf/BinaryDecoder.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal struct BinaryDecoder: Decoder {
3333
// Field number for last-parsed field tag
3434
private var fieldNumber: Int = 0
3535
// Collection of extension fields for this decode
36-
private var extensions: ExtensionMap?
36+
private var extensions: (any ExtensionMap)?
3737
// The current group number. See decodeFullGroup(group:fieldNumber:) for how
3838
// this is used.
3939
private var groupFieldNumber: Int?
@@ -54,7 +54,7 @@ internal struct BinaryDecoder: Decoder {
5454
forReadingFrom pointer: UnsafeRawPointer,
5555
count: Int,
5656
options: BinaryDecodingOptions,
57-
extensions: ExtensionMap? = nil
57+
extensions: (any ExtensionMap)? = nil
5858
) {
5959
// Assuming baseAddress is not nil.
6060
p = pointer
@@ -1088,7 +1088,7 @@ internal struct BinaryDecoder: Decoder {
10881088

10891089
internal mutating func decodeExtensionField(
10901090
values: inout ExtensionFieldValueSet,
1091-
messageType: Message.Type,
1091+
messageType: any Message.Type,
10921092
fieldNumber: Int
10931093
) throws {
10941094
if let ext = extensions?[messageType, fieldNumber] {
@@ -1102,9 +1102,9 @@ internal struct BinaryDecoder: Decoder {
11021102
/// Helper to reuse between Extension decoding and MessageSet Extension decoding.
11031103
private mutating func decodeExtensionField(
11041104
values: inout ExtensionFieldValueSet,
1105-
messageType: Message.Type,
1105+
messageType: any Message.Type,
11061106
fieldNumber: Int,
1107-
messageExtension ext: AnyMessageExtension
1107+
messageExtension ext: any AnyMessageExtension
11081108
) throws {
11091109
assert(!consumed)
11101110
assert(fieldNumber == ext.fieldNumber)
@@ -1129,7 +1129,7 @@ internal struct BinaryDecoder: Decoder {
11291129

11301130
internal mutating func decodeExtensionFieldsAsMessageSet(
11311131
values: inout ExtensionFieldValueSet,
1132-
messageType: Message.Type
1132+
messageType: any Message.Type
11331133
) throws {
11341134
// Spin looking for the Item group, everything else will end up in unknown fields.
11351135
while let fieldNumber = try self.nextFieldNumber() {
@@ -1173,14 +1173,14 @@ internal struct BinaryDecoder: Decoder {
11731173

11741174
private mutating func decodeMessageSetItem(
11751175
values: inout ExtensionFieldValueSet,
1176-
messageType: Message.Type
1176+
messageType: any Message.Type
11771177
) throws -> DecodeMessageSetItemResult {
11781178
// This is loosely based on the C++:
11791179
// ExtensionSet::ParseMessageSetItem()
11801180
// WireFormat::ParseAndMergeMessageSetItem()
11811181
// (yes, there have two versions that are almost the same)
11821182

1183-
var msgExtension: AnyMessageExtension?
1183+
var msgExtension: (any AnyMessageExtension)?
11841184
var fieldData: Data?
11851185

11861186
// In this loop, if wire types are wrong, things don't decode,

Sources/SwiftProtobuf/BinaryDelimited.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public enum BinaryDelimited {
6565
/// `BinaryDelimited.Error` for some writing errors, or the
6666
/// underlying `OutputStream.streamError` for a stream error.
6767
public static func serialize(
68-
message: Message,
68+
message: any Message,
6969
to stream: OutputStream,
7070
partial: Bool = false
7171
) throws {
@@ -125,7 +125,7 @@ public enum BinaryDelimited {
125125
public static func parse<M: Message>(
126126
messageType: M.Type,
127127
from stream: InputStream,
128-
extensions: ExtensionMap? = nil,
128+
extensions: (any ExtensionMap)? = nil,
129129
partial: Bool = false,
130130
options: BinaryDecodingOptions = BinaryDecodingOptions()
131131
) throws -> M {
@@ -166,7 +166,7 @@ public enum BinaryDelimited {
166166
public static func merge<M: Message>(
167167
into message: inout M,
168168
from stream: InputStream,
169-
extensions: ExtensionMap? = nil,
169+
extensions: (any ExtensionMap)? = nil,
170170
partial: Bool = false,
171171
options: BinaryDecodingOptions = BinaryDecodingOptions()
172172
) throws {

Sources/SwiftProtobuf/Decoder.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,11 @@ public protocol Decoder {
204204
// Decode extension fields
205205

206206
/// Decode an extension field
207-
mutating func decodeExtensionField(values: inout ExtensionFieldValueSet, messageType: Message.Type, fieldNumber: Int) throws
207+
mutating func decodeExtensionField(values: inout ExtensionFieldValueSet, messageType: any Message.Type, fieldNumber: Int) throws
208208

209209
// Run a decode loop decoding the MessageSet format for Extensions.
210210
mutating func decodeExtensionFieldsAsMessageSet(values: inout ExtensionFieldValueSet,
211-
messageType: Message.Type) throws
211+
messageType: any Message.Type) throws
212212
}
213213

214214
/// Most Decoders won't care about Extension handing as in MessageSet
@@ -217,7 +217,7 @@ public protocol Decoder {
217217
extension Decoder {
218218
public mutating func decodeExtensionFieldsAsMessageSet(
219219
values: inout ExtensionFieldValueSet,
220-
messageType: Message.Type
220+
messageType: any Message.Type
221221
) throws {
222222
while let fieldNumber = try self.nextFieldNumber() {
223223
try self.decodeExtensionField(values: &values,

Sources/SwiftProtobuf/Enum.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extension Enum {
4949
/// Since the text format and JSON names are always identical, we don't need
5050
/// to distinguish them.
5151
internal var name: _NameMap.Name? {
52-
guard let nameProviding = Self.self as? _ProtoNameProviding.Type else {
52+
guard let nameProviding = Self.self as? any _ProtoNameProviding.Type else {
5353
return nil
5454
}
5555
return nameProviding._protobuf_nameMap.names(for: rawValue)?.proto
@@ -63,7 +63,7 @@ extension Enum {
6363
///
6464
/// - Parameter name: The name of the enum case.
6565
internal init?(name: String) {
66-
guard let nameProviding = Self.self as? _ProtoNameProviding.Type,
66+
guard let nameProviding = Self.self as? any _ProtoNameProviding.Type,
6767
let number = nameProviding._protobuf_nameMap.number(forJSONName: name) else {
6868
return nil
6969
}
@@ -78,7 +78,7 @@ extension Enum {
7878
///
7979
/// - Parameter name: Buffer holding the UTF-8 bytes of the desired name.
8080
internal init?(rawUTF8: UnsafeRawBufferPointer) {
81-
guard let nameProviding = Self.self as? _ProtoNameProviding.Type,
81+
guard let nameProviding = Self.self as? any _ProtoNameProviding.Type,
8282
let number = nameProviding._protobuf_nameMap.number(forJSONName: rawUTF8) else {
8383
return nil
8484
}

Sources/SwiftProtobuf/ExtensionFieldValueSet.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// -----------------------------------------------------------------------------
1616

1717
public struct ExtensionFieldValueSet: Hashable, Sendable {
18-
fileprivate var values = [Int : AnyExtensionField]()
18+
fileprivate var values = [Int : any AnyExtensionField]()
1919

2020
public static func ==(lhs: ExtensionFieldValueSet,
2121
rhs: ExtensionFieldValueSet) -> Bool {
@@ -62,12 +62,12 @@ public struct ExtensionFieldValueSet: Hashable, Sendable {
6262
}
6363
}
6464

65-
public subscript(index: Int) -> AnyExtensionField? {
65+
public subscript(index: Int) -> (any AnyExtensionField)? {
6666
get { return values[index] }
6767
set { values[index] = newValue }
6868
}
6969

70-
mutating func modify<ReturnType>(index: Int, _ modifier: (inout AnyExtensionField?) throws -> ReturnType) rethrows -> ReturnType {
70+
mutating func modify<ReturnType>(index: Int, _ modifier: (inout (any AnyExtensionField)?) throws -> ReturnType) rethrows -> ReturnType {
7171
// This internal helper exists to invoke the _modify accessor on Dictionary for the given operation, which can avoid CoWs
7272
// during the modification operation.
7373
return try modifier(&values[index])

0 commit comments

Comments
 (0)