Skip to content

Commit

Permalink
Add enableUpcomingFeature("ExistentialAny") and update accordingly.
Browse files Browse the repository at this point in the history
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 #1509
  • Loading branch information
thomasvl committed Dec 6, 2023
1 parent 0d922f0 commit 0f3e371
Show file tree
Hide file tree
Showing 45 changed files with 226 additions and 206 deletions.
30 changes: 25 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,41 @@ let package = Package(
name: "SwiftProtobuf",
exclude: ["CMakeLists.txt"],
swiftSettings: [
.enableUpcomingFeature("ExistentialAny"),
.enableExperimentalFeature("StrictConcurrency=complete"),
]
),
.target(
name: "SwiftProtobufPluginLibrary",
dependencies: ["SwiftProtobuf"],
exclude: ["CMakeLists.txt"]
exclude: ["CMakeLists.txt"],
swiftSettings: [
.enableUpcomingFeature("ExistentialAny"),
]
),
.target(
name: "SwiftProtobufTestHelpers",
dependencies: ["SwiftProtobuf"],
swiftSettings: [
.enableUpcomingFeature("ExistentialAny"),
.enableExperimentalFeature("StrictConcurrency=complete"),
]
),
.executableTarget(
name: "protoc-gen-swift",
dependencies: ["SwiftProtobufPluginLibrary", "SwiftProtobuf"],
exclude: ["CMakeLists.txt"]
exclude: ["CMakeLists.txt"],
swiftSettings: [
.enableUpcomingFeature("ExistentialAny")
]
),
.executableTarget(
name: "Conformance",
dependencies: ["SwiftProtobuf"],
exclude: ["failure_list_swift.txt", "text_format_failure_list_swift.txt"]
exclude: ["failure_list_swift.txt", "text_format_failure_list_swift.txt"],
swiftSettings: [
.enableUpcomingFeature("ExistentialAny"),
]
),
.plugin(
name: "SwiftProtobufPlugin",
Expand All @@ -75,16 +86,25 @@ let package = Package(
name: "SwiftProtobufTests",
dependencies: ["SwiftProtobuf"],
swiftSettings: [
.enableUpcomingFeature("ExistentialAny"),
.enableExperimentalFeature("StrictConcurrency=complete"),
]
),
.testTarget(
name: "SwiftProtobufPluginLibraryTests",
dependencies: ["SwiftProtobufPluginLibrary", "SwiftProtobufTestHelpers"]
dependencies: ["SwiftProtobufPluginLibrary", "SwiftProtobufTestHelpers"],
swiftSettings: [
.enableUpcomingFeature("ExistentialAny"),
.enableExperimentalFeature("StrictConcurrency=complete"),
]
),
.testTarget(
name: "protoc-gen-swiftTests",
dependencies: ["protoc-gen-swift", "SwiftProtobufTestHelpers"]
dependencies: ["protoc-gen-swift", "SwiftProtobufTestHelpers"],
swiftSettings: [
.enableUpcomingFeature("ExistentialAny"),
.enableExperimentalFeature("StrictConcurrency=complete"),
]
),
],
swiftLanguageVersions: [.v5]
Expand Down
6 changes: 3 additions & 3 deletions Sources/Conformance/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func buildResponse(serializedBytes: [UInt8]) -> Conformance_ConformanceResponse
return response
}

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

let testMessage: SwiftProtobuf.Message
let testMessage: any SwiftProtobuf.Message
switch request.payload {
case .protobufPayload(let data)?:
do {
Expand Down
16 changes: 8 additions & 8 deletions Sources/SwiftProtobuf/AnyMessageStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
import Foundation

fileprivate func serializeAnyJSON(
for message: Message,
for message: any Message,
typeURL: String,
options: JSONEncodingOptions
) throws -> String {
var visitor = try JSONEncodingVisitor(type: type(of: message), options: options)
visitor.startObject(message: message)
visitor.encodeField(name: "@type", stringValue: typeURL)
if let m = message as? _CustomJSONCodable {
if let m = message as? (any _CustomJSONCodable) {
let value = try m.encodedJSONString(options: options)
visitor.encodeField(name: "value", jsonText: value)
} else {
Expand All @@ -33,7 +33,7 @@ fileprivate func serializeAnyJSON(
return visitor.stringResult
}

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

fileprivate func unpack(contentJSON: [UInt8],
extensions: ExtensionMap,
extensions: any ExtensionMap,
options: JSONDecodingOptions,
as messageType: Message.Type) throws -> Message {
guard messageType is _CustomJSONCodable.Type else {
as messageType: any Message.Type) throws -> any Message {
guard messageType is any _CustomJSONCodable.Type else {
let contentJSONAsObject = asJSONObject(body: contentJSON)
return try messageType.init(jsonUTF8Bytes: contentJSONAsObject, extensions: extensions, options: options)
}
Expand Down Expand Up @@ -135,7 +135,7 @@ internal class AnyMessageStorage {
// unpacking that takes new options when a developer decides to decode it.
case binary(Data)
// a message
case message(Message)
case message(any Message)
// parsed JSON with the @type removed and the decoding options.
case contentJSON([UInt8], JSONDecodingOptions)
}
Expand All @@ -162,7 +162,7 @@ internal class AnyMessageStorage {
// replaced during the unpacking and never as a merge.
func unpackTo<M: Message>(
target: inout M,
extensions: ExtensionMap?,
extensions: (any ExtensionMap)?,
options: BinaryDecodingOptions
) throws {
guard isA(M.self) else {
Expand Down
10 changes: 5 additions & 5 deletions Sources/SwiftProtobuf/AsyncMessageSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension AsyncSequence where Element == UInt8 {
@inlinable
public func binaryProtobufDelimitedMessages<M: Message>(
of messageType: M.Type = M.self,
extensions: ExtensionMap? = nil,
extensions: (any ExtensionMap)? = nil,
partial: Bool = false,
options: BinaryDecodingOptions = BinaryDecodingOptions()
) -> AsyncMessageSequence<Self, M> {
Expand All @@ -58,7 +58,7 @@ public struct AsyncMessageSequence<
public typealias Element = M

private let base: Base
private let extensions: ExtensionMap?
private let extensions: (any ExtensionMap)?
private let partial: Bool
private let options: BinaryDecodingOptions

Expand All @@ -80,7 +80,7 @@ public struct AsyncMessageSequence<
/// messages.
public init(
base: Base,
extensions: ExtensionMap? = nil,
extensions: (any ExtensionMap)? = nil,
partial: Bool = false,
options: BinaryDecodingOptions = BinaryDecodingOptions()
) {
Expand All @@ -95,15 +95,15 @@ public struct AsyncMessageSequence<
@usableFromInline
var iterator: Base.AsyncIterator?
@usableFromInline
let extensions: ExtensionMap?
let extensions: (any ExtensionMap)?
@usableFromInline
let partial: Bool
@usableFromInline
let options: BinaryDecodingOptions

init(
iterator: Base.AsyncIterator,
extensions: ExtensionMap?,
extensions: (any ExtensionMap)?,
partial: Bool,
options: BinaryDecodingOptions
) {
Expand Down
16 changes: 8 additions & 8 deletions Sources/SwiftProtobuf/BinaryDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal struct BinaryDecoder: Decoder {
// Field number for last-parsed field tag
private var fieldNumber: Int = 0
// Collection of extension fields for this decode
private var extensions: ExtensionMap?
private var extensions: (any ExtensionMap)?
// The current group number. See decodeFullGroup(group:fieldNumber:) for how
// this is used.
private var groupFieldNumber: Int?
Expand All @@ -54,7 +54,7 @@ internal struct BinaryDecoder: Decoder {
forReadingFrom pointer: UnsafeRawPointer,
count: Int,
options: BinaryDecodingOptions,
extensions: ExtensionMap? = nil
extensions: (any ExtensionMap)? = nil
) {
// Assuming baseAddress is not nil.
p = pointer
Expand Down Expand Up @@ -1088,7 +1088,7 @@ internal struct BinaryDecoder: Decoder {

internal mutating func decodeExtensionField(
values: inout ExtensionFieldValueSet,
messageType: Message.Type,
messageType: any Message.Type,
fieldNumber: Int
) throws {
if let ext = extensions?[messageType, fieldNumber] {
Expand All @@ -1102,9 +1102,9 @@ internal struct BinaryDecoder: Decoder {
/// Helper to reuse between Extension decoding and MessageSet Extension decoding.
private mutating func decodeExtensionField(
values: inout ExtensionFieldValueSet,
messageType: Message.Type,
messageType: any Message.Type,
fieldNumber: Int,
messageExtension ext: AnyMessageExtension
messageExtension ext: any AnyMessageExtension
) throws {
assert(!consumed)
assert(fieldNumber == ext.fieldNumber)
Expand All @@ -1129,7 +1129,7 @@ internal struct BinaryDecoder: Decoder {

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

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

var msgExtension: AnyMessageExtension?
var msgExtension: (any AnyMessageExtension)?
var fieldData: Data?

// In this loop, if wire types are wrong, things don't decode,
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftProtobuf/BinaryDelimited.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public enum BinaryDelimited {
/// `BinaryDelimited.Error` for some writing errors, or the
/// underlying `OutputStream.streamError` for a stream error.
public static func serialize(
message: Message,
message: any Message,
to stream: OutputStream,
partial: Bool = false
) throws {
Expand Down Expand Up @@ -125,7 +125,7 @@ public enum BinaryDelimited {
public static func parse<M: Message>(
messageType: M.Type,
from stream: InputStream,
extensions: ExtensionMap? = nil,
extensions: (any ExtensionMap)? = nil,
partial: Bool = false,
options: BinaryDecodingOptions = BinaryDecodingOptions()
) throws -> M {
Expand Down Expand Up @@ -166,7 +166,7 @@ public enum BinaryDelimited {
public static func merge<M: Message>(
into message: inout M,
from stream: InputStream,
extensions: ExtensionMap? = nil,
extensions: (any ExtensionMap)? = nil,
partial: Bool = false,
options: BinaryDecodingOptions = BinaryDecodingOptions()
) throws {
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftProtobuf/Decoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ public protocol Decoder {
// Decode extension fields

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

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

/// Most Decoders won't care about Extension handing as in MessageSet
Expand All @@ -217,7 +217,7 @@ public protocol Decoder {
extension Decoder {
public mutating func decodeExtensionFieldsAsMessageSet(
values: inout ExtensionFieldValueSet,
messageType: Message.Type
messageType: any Message.Type
) throws {
while let fieldNumber = try self.nextFieldNumber() {
try self.decodeExtensionField(values: &values,
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftProtobuf/Enum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension Enum {
/// Since the text format and JSON names are always identical, we don't need
/// to distinguish them.
internal var name: _NameMap.Name? {
guard let nameProviding = Self.self as? _ProtoNameProviding.Type else {
guard let nameProviding = Self.self as? any _ProtoNameProviding.Type else {
return nil
}
return nameProviding._protobuf_nameMap.names(for: rawValue)?.proto
Expand All @@ -63,7 +63,7 @@ extension Enum {
///
/// - Parameter name: The name of the enum case.
internal init?(name: String) {
guard let nameProviding = Self.self as? _ProtoNameProviding.Type,
guard let nameProviding = Self.self as? any _ProtoNameProviding.Type,
let number = nameProviding._protobuf_nameMap.number(forJSONName: name) else {
return nil
}
Expand All @@ -78,7 +78,7 @@ extension Enum {
///
/// - Parameter name: Buffer holding the UTF-8 bytes of the desired name.
internal init?(rawUTF8: UnsafeRawBufferPointer) {
guard let nameProviding = Self.self as? _ProtoNameProviding.Type,
guard let nameProviding = Self.self as? any _ProtoNameProviding.Type,
let number = nameProviding._protobuf_nameMap.number(forJSONName: rawUTF8) else {
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftProtobuf/ExtensionFieldValueSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// -----------------------------------------------------------------------------

public struct ExtensionFieldValueSet: Hashable, Sendable {
fileprivate var values = [Int : AnyExtensionField]()
fileprivate var values = [Int : any AnyExtensionField]()

public static func ==(lhs: ExtensionFieldValueSet,
rhs: ExtensionFieldValueSet) -> Bool {
Expand Down Expand Up @@ -62,12 +62,12 @@ public struct ExtensionFieldValueSet: Hashable, Sendable {
}
}

public subscript(index: Int) -> AnyExtensionField? {
public subscript(index: Int) -> (any AnyExtensionField)? {
get { return values[index] }
set { values[index] = newValue }
}

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

0 comments on commit 0f3e371

Please sign in to comment.