Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly annotate existential usage of Message with the existential any keyword. #1509

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Conformance/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func buildResponse(serializedBytes: [UInt8]) -> Conformance_ConformanceResponse
return response
}

let msgType: SwiftProtobuf.Message.Type
let msgType: any SwiftProtobuf.Message.Type
tbkka marked this conversation as resolved.
Show resolved Hide resolved
let extensions: SwiftProtobuf.ExtensionMap
switch request.messageType {
case "":
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
8 changes: 4 additions & 4 deletions Sources/SwiftProtobuf/AnyMessageStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import Foundation

fileprivate func serializeAnyJSON(
for message: Message,
for message: any Message,
typeURL: String,
options: JSONEncodingOptions
) throws -> String {
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 @@ -55,7 +55,7 @@ fileprivate func asJSONObject(body: [UInt8]) -> Data {
fileprivate func unpack(contentJSON: [UInt8],
extensions: ExtensionMap,
options: JSONDecodingOptions,
as messageType: Message.Type) throws -> Message {
as messageType: any Message.Type) throws -> any Message {
guard messageType is _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 Down
8 changes: 4 additions & 4 deletions Sources/SwiftProtobuf/BinaryDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ internal struct BinaryDecoder: Decoder {

internal mutating func decodeExtensionField(
values: inout ExtensionFieldValueSet,
messageType: Message.Type,
messageType: any Message.Type,
tbkka marked this conversation as resolved.
Show resolved Hide resolved
fieldNumber: Int
) throws {
if let ext = extensions?[messageType, fieldNumber] {
Expand All @@ -1104,7 +1104,7 @@ 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
) throws {
Expand All @@ -1131,7 +1131,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 @@ -1175,7 +1175,7 @@ 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()
Expand Down
2 changes: 1 addition & 1 deletion 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
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
tbkka marked this conversation as resolved.
Show resolved Hide resolved

// 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
4 changes: 2 additions & 2 deletions Sources/SwiftProtobuf/ExtensionMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/// standard `SimpleExtensionMap` implementation.
public protocol ExtensionMap: Sendable {
/// Returns the extension object describing an extension or nil
subscript(messageType: Message.Type, fieldNumber: Int) -> AnyMessageExtension? { get }
subscript(messageType: any Message.Type, fieldNumber: Int) -> AnyMessageExtension? { get }

/// Returns the field number for a message with a specific field name
///
Expand All @@ -34,5 +34,5 @@ public protocol ExtensionMap: Sendable {
/// for the proto file and `message` is the name of the message in
/// which the extension was defined. (This is different from the
/// message that is being extended!)
func fieldNumberForProto(messageType: Message.Type, protoFieldName: String) -> Int?
func fieldNumberForProto(messageType: any Message.Type, protoFieldName: String) -> Int?
}
2 changes: 1 addition & 1 deletion Sources/SwiftProtobuf/Google_Protobuf_Any+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extension Google_Protobuf_Any {
/// - Throws: `BinaryEncodingError.missingRequiredFields` if `partial` is
/// false and `message` wasn't fully initialized.
public init(
message: Message,
message: any Message,
partial: Bool = false,
typePrefix: String = defaultAnyTypeURLPrefix
) throws {
Expand Down
14 changes: 7 additions & 7 deletions Sources/SwiftProtobuf/Google_Protobuf_Any+Registry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fileprivate let knownTypesQueue =
// TODO: Should these first four be exposed as methods to go with
// the general registry support?

internal func buildTypeURL(forMessage message: Message, typePrefix: String) -> String {
internal func buildTypeURL(forMessage message: any Message, typePrefix: String) -> String {
var url = typePrefix
let needsSlash = typePrefix.isEmpty || typePrefix.last != "/"
if needsSlash {
Expand All @@ -35,7 +35,7 @@ internal func buildTypeURL(forMessage message: Message, typePrefix: String) -> S
return url + typeName(fromMessage: message)
}

internal func typeName(fromMessage message: Message) -> String {
internal func typeName(fromMessage message: any Message) -> String {
let messageType = type(of: message)
return messageType.protoMessageName
}
Expand Down Expand Up @@ -67,7 +67,7 @@ fileprivate final class UnsafeMutableTransferBox<Wrapped> {
extension UnsafeMutableTransferBox: @unchecked Sendable {}

// All access to this should be done on `knownTypesQueue`.
fileprivate let knownTypes: UnsafeMutableTransferBox<[String:Message.Type]> = .init([
fileprivate let knownTypes: UnsafeMutableTransferBox<[String: any Message.Type]> = .init([
tbkka marked this conversation as resolved.
Show resolved Hide resolved
// Seeded with the Well Known Types.
"google.protobuf.Any": Google_Protobuf_Any.self,
"google.protobuf.BoolValue": Google_Protobuf_BoolValue.self,
Expand Down Expand Up @@ -117,7 +117,7 @@ extension Google_Protobuf_Any {
///
/// Returns: true if the type was registered, false if something
/// else was already registered for the messageName.
@discardableResult public static func register(messageType: Message.Type) -> Bool {
@discardableResult public static func register(messageType: any Message.Type) -> Bool {
let messageTypeName = messageType.protoMessageName
var result: Bool = false
execute(flags: .barrier) {
Expand All @@ -136,14 +136,14 @@ extension Google_Protobuf_Any {
}

/// Returns the Message.Type expected for the given type URL.
public static func messageType(forTypeURL url: String) -> Message.Type? {
public static func messageType(forTypeURL url: String) -> (any Message.Type)? {
let messageTypeName = typeName(fromURL: url)
return messageType(forMessageName: messageTypeName)
}

/// Returns the Message.Type expected for the given proto message name.
public static func messageType(forMessageName name: String) -> Message.Type? {
var result: Message.Type?
public static func messageType(forMessageName name: String) -> (any Message.Type)? {
var result: (any Message.Type)?
execute(flags: .none) {
result = knownTypes.wrappedValue[name]
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftProtobuf/Internal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public enum Internal {

/// Helper to loop over a list of Messages to see if they are all
/// initialized (see Message.isInitialized for what that means).
public static func areAllInitialized(_ listOfMessages: [Message]) -> Bool {
public static func areAllInitialized(_ listOfMessages: [any Message]) -> Bool {
for msg in listOfMessages {
if !msg.isInitialized {
return false
Expand All @@ -40,7 +40,7 @@ public enum Internal {

/// Helper to loop over dictionary with values that are Messages to see if
/// they are all initialized (see Message.isInitialized for what that means).
public static func areAllInitialized<K>(_ mapToMessages: [K: Message]) -> Bool {
public static func areAllInitialized<K>(_ mapToMessages: [K: any Message]) -> Bool {
for (_, msg) in mapToMessages {
if !msg.isInitialized {
return false
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftProtobuf/JSONDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Foundation

internal struct JSONDecoder: Decoder {
internal var scanner: JSONScanner
internal var messageType: Message.Type
internal var messageType: any Message.Type
private var fieldCount = 0
private var isMapKey = false
private var fieldNameMap: _NameMap?
Expand All @@ -30,14 +30,14 @@ internal struct JSONDecoder: Decoder {
}

internal init(source: UnsafeRawBufferPointer, options: JSONDecodingOptions,
messageType: Message.Type, extensions: ExtensionMap?) {
messageType: any Message.Type, extensions: ExtensionMap?) {
let scanner = JSONScanner(source: source,
options: options,
extensions: extensions)
self.init(scanner: scanner, messageType: messageType)
}

private init(scanner: JSONScanner, messageType: Message.Type) {
private init(scanner: JSONScanner, messageType: any Message.Type) {
self.scanner = scanner
self.messageType = messageType
}
Expand Down Expand Up @@ -730,7 +730,7 @@ internal struct JSONDecoder: Decoder {

mutating func decodeExtensionField(
values: inout ExtensionFieldValueSet,
messageType: Message.Type,
messageType: any Message.Type,
fieldNumber: Int
) throws {
// Force-unwrap: we can only get here if the extension exists.
Expand Down
10 changes: 5 additions & 5 deletions Sources/SwiftProtobuf/JSONEncodingVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal struct JSONEncodingVisitor: Visitor {

/// Creates a new visitor for serializing a message of the given type to JSON
/// format.
init(type: Message.Type, options: JSONEncodingOptions) throws {
init(type: any Message.Type, options: JSONEncodingOptions) throws {
if let nameProviding = type as? _ProtoNameProviding.Type {
self.nameMap = nameProviding._protobuf_nameMap
} else {
Expand All @@ -51,13 +51,13 @@ internal struct JSONEncodingVisitor: Visitor {
encoder.endArray()
}

mutating func startObject(message: Message) {
self.extensions = (message as? ExtensibleMessage)?._protobuf_extensionFieldValues
mutating func startObject(message: any Message) {
self.extensions = (message as? (any ExtensibleMessage))?._protobuf_extensionFieldValues
encoder.startObject()
}

mutating func startArrayObject(message: Message) {
self.extensions = (message as? ExtensibleMessage)?._protobuf_extensionFieldValues
mutating func startArrayObject(message: any Message) {
self.extensions = (message as? (any ExtensibleMessage))?._protobuf_extensionFieldValues
encoder.startArrayObject()
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftProtobuf/JSONScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ internal struct JSONScanner {
/// it silently skips it.
internal mutating func nextFieldNumber(
names: _NameMap,
messageType: Message.Type
messageType: any Message.Type
) throws -> Int? {
while true {
var fieldName: String
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftProtobuf/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public protocol Message: _CommonMessageConformances {
/// Helper to compare `Message`s when not having a specific type to use
/// normal `Equatable`. `Equatable` is provided with specific generated
/// types.
func isEqualTo(message: Message) -> Bool
func isEqualTo(message: any Message) -> Bool
}

#if DEBUG
Expand Down Expand Up @@ -189,7 +189,7 @@ public protocol _MessageImplementationBase: Message, Hashable {
}

extension _MessageImplementationBase {
public func isEqualTo(message: Message) -> Bool {
public func isEqualTo(message: any Message) -> Bool {
guard let other = message as? Self else {
return false
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftProtobuf/MessageExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public protocol AnyMessageExtension: Sendable {
var fieldNumber: Int { get }
var fieldName: String { get }
var messageType: Message.Type { get }
var messageType: any Message.Type { get }
func _protobuf_newField<D: Decoder>(decoder: inout D) throws -> AnyExtensionField?
}

Expand All @@ -29,7 +29,7 @@ public protocol AnyMessageExtension: Sendable {
public final class MessageExtension<FieldType: ExtensionField, MessageType: Message>: AnyMessageExtension {
public let fieldNumber: Int
public let fieldName: String
public let messageType: Message.Type
public let messageType: any Message.Type
public init(_protobuf_fieldNumber: Int, fieldName: String) {
self.fieldNumber = _protobuf_fieldNumber
self.fieldName = fieldName
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftProtobuf/SimpleExtensionMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public struct SimpleExtensionMap: ExtensionMap, ExpressibleByArrayLiteral {
}
}

public subscript(messageType: Message.Type, fieldNumber: Int) -> AnyMessageExtension? {
public subscript(messageType: any Message.Type, fieldNumber: Int) -> AnyMessageExtension? {
get {
if let l = fields[fieldNumber] {
for e in l {
Expand All @@ -45,7 +45,7 @@ public struct SimpleExtensionMap: ExtensionMap, ExpressibleByArrayLiteral {
}
}

public func fieldNumberForProto(messageType: Message.Type, protoFieldName: String) -> Int? {
public func fieldNumberForProto(messageType: any Message.Type, protoFieldName: String) -> Int? {
// TODO: Make this faster...
for (_, list) in fields {
for e in list {
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftProtobuf/TextFormatDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal struct TextFormatDecoder: Decoder {
private var fieldCount = 0
private var terminator: UInt8?
private var fieldNameMap: _NameMap?
private var messageType: Message.Type?
private var messageType: (any Message.Type)?

internal var complete: Bool {
mutating get {
Expand All @@ -34,7 +34,7 @@ internal struct TextFormatDecoder: Decoder {
}

internal init(
messageType: Message.Type,
messageType: any Message.Type,
utf8Pointer: UnsafeRawPointer,
count: Int,
options: TextFormatDecodingOptions,
Expand All @@ -48,7 +48,7 @@ internal struct TextFormatDecoder: Decoder {
self.messageType = messageType
}

internal init(messageType: Message.Type, scanner: TextFormatScanner, terminator: UInt8?) throws {
internal init(messageType: any Message.Type, scanner: TextFormatScanner, terminator: UInt8?) throws {
self.scanner = scanner
self.terminator = terminator
guard let nameProviding = (messageType as? _ProtoNameProviding.Type) else {
Expand Down Expand Up @@ -706,7 +706,7 @@ internal struct TextFormatDecoder: Decoder {
}
}

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 {
if let ext = scanner.extensions?[messageType, fieldNumber] {
try values.modify(index: fieldNumber) { fieldValue in
if fieldValue != nil {
Expand Down
Loading