Skip to content

Commit 56aa257

Browse files
committed
Directly generate func ==(lhs:, rhs:) -> Bool.
Maintain the older _protobuf_ method to not break source compatibility, but remove the indirection.
1 parent 4c89d0d commit 56aa257

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

Sources/SwiftProtobuf/Message.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ public extension Message {
174174
/// multiple message types that uses equality tests, puts messages in a `Set`,
175175
/// or uses them as `Dictionary` keys.
176176
public protocol _MessageImplementationBase: Message, Hashable {
177+
178+
// Legacy function; no longer used, but left to maintain source compatibility.
177179
func _protobuf_generated_isEqualTo(other: Self) -> Bool
178180
}
179181

@@ -185,7 +187,16 @@ public extension _MessageImplementationBase {
185187
return self == other
186188
}
187189

190+
// Legacy default implementation that is used by old generated code, current
191+
// versions of the plugin/generator provide this directly, but this is here
192+
// just to avoid breaking source compatibility.
188193
public static func ==(lhs: Self, rhs: Self) -> Bool {
189194
return lhs._protobuf_generated_isEqualTo(other: rhs)
190195
}
196+
197+
// Legacy function that is generated by old versions of the plugin/generator,
198+
// defaulted to keep things simple without changing the api surface.
199+
public func _protobuf_generated_isEqualTo(other: Self) -> Bool {
200+
return self == other
201+
}
191202
}

Sources/protoc-gen-swift/MessageFieldGenerator.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,17 @@ class MessageFieldGenerator: FieldGeneratorBase, FieldGenerator {
138138
}
139139

140140
func generateFieldComparison(printer p: inout CodePrinter) {
141+
let lhsProperty: String
141142
let otherStoredProperty: String
142143
if usesHeapStorage {
143-
otherStoredProperty = "other_storage.\(underscoreSwiftName)"
144+
lhsProperty = "_storage.\(underscoreSwiftName)"
145+
otherStoredProperty = "rhs_storage.\(underscoreSwiftName)"
144146
} else {
145-
otherStoredProperty = "other.\(hasFieldPresence ? underscoreSwiftName : swiftName)"
147+
lhsProperty = "lhs.\(hasFieldPresence ? underscoreSwiftName : swiftName)"
148+
otherStoredProperty = "rhs.\(hasFieldPresence ? underscoreSwiftName : swiftName)"
146149
}
147150

148-
p.print("if \(storedProperty) != \(otherStoredProperty) {return false}\n")
151+
p.print("if \(lhsProperty) != \(otherStoredProperty) {return false}\n")
149152
}
150153

151154
func generateRequiredFieldCheck(printer p: inout CodePrinter) {

Sources/protoc-gen-swift/MessageGenerator.swift

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class MessageGenerator {
221221
p.print("\n")
222222
generateTraverse(printer: &p)
223223
p.print("\n")
224-
generateMessageImplementationBase(printer: &p)
224+
generateMessageEquality(printer: &p)
225225
p.outdent()
226226
p.print("}\n")
227227

@@ -345,22 +345,23 @@ class MessageGenerator {
345345
p.print("}\n")
346346
}
347347

348-
private func generateMessageImplementationBase(printer p: inout CodePrinter) {
349-
p.print("\(visibility)func _protobuf_generated_isEqualTo(other: \(swiftFullName)) -> Bool {\n")
348+
private func generateMessageEquality(printer p: inout CodePrinter) {
349+
p.print("\(visibility)static func ==(lhs: \(swiftFullName), rhs: \(swiftFullName)) -> Bool {\n")
350350
p.indent()
351351
var compareFields = true
352352
if let storage = storage {
353-
p.print("if _storage !== other._storage {\n")
353+
p.print("if lhs._storage !== rhs._storage {\n")
354354
p.indent()
355355
p.print("let storagesAreEqual: Bool = ")
356356
if storage.storageProvidesEqualTo {
357-
p.print("_storage.isEqualTo(other: other._storage)\n")
357+
p.print("lhs._storage.isEqualTo(other: rhs._storage)\n")
358358
compareFields = false
359359
}
360360
}
361361
if compareFields {
362362
generateWithLifetimeExtension(printer: &p,
363-
alsoCapturing: "other") { p in
363+
alsoCapturing: "rhs",
364+
selfQualifier: "lhs") { p in
364365
for f in fields {
365366
f.generateFieldComparison(printer: &p)
366367
}
@@ -374,9 +375,9 @@ class MessageGenerator {
374375
p.outdent()
375376
p.print("}\n")
376377
}
377-
p.print("if unknownFields != other.unknownFields {return false}\n")
378+
p.print("if lhs.unknownFields != rhs.unknownFields {return false}\n")
378379
if isExtensible {
379-
p.print("if _protobuf_extensionFieldValues != other._protobuf_extensionFieldValues {return false}\n")
380+
p.print("if lhs._protobuf_extensionFieldValues != rhs._protobuf_extensionFieldValues {return false}\n")
380381
}
381382
p.print("return true\n")
382383
p.outdent()
@@ -457,25 +458,33 @@ class MessageGenerator {
457458
throws canThrow: Bool = false,
458459
returns: Bool = false,
459460
alsoCapturing capturedVariable: String? = nil,
461+
selfQualifier qualifier: String? = nil,
460462
body: (inout CodePrinter) -> Void
461463
) {
462464
if storage != nil {
463465
let prefixKeywords = "\(returns ? "return " : "")" +
464466
"\(canThrow ? "try " : "")"
465467
p.print(prefixKeywords)
466468

469+
let selfQualifier: String
470+
if let qualifier = qualifier {
471+
selfQualifier = "\(qualifier)."
472+
} else {
473+
selfQualifier = ""
474+
}
475+
467476
if let capturedVariable = capturedVariable {
468477
// withExtendedLifetime can only pass a single argument,
469478
// so we have to build and deconstruct a tuple in this case:
470-
let actualArgs = "(_storage, \(capturedVariable)._storage)"
479+
let actualArgs = "(\(selfQualifier)_storage, \(capturedVariable)._storage)"
471480
let formalArgs = "(_args: (_StorageClass, _StorageClass))"
472481
p.print("withExtendedLifetime(\(actualArgs)) { \(formalArgs) in\n")
473482
p.indent()
474483
p.print("let _storage = _args.0\n")
475484
p.print("let \(capturedVariable)_storage = _args.1\n")
476485
} else {
477486
// Single argument can be passed directly:
478-
p.print("withExtendedLifetime(_storage) { (_storage: _StorageClass) in\n")
487+
p.print("withExtendedLifetime(\(selfQualifier)_storage) { (_storage: _StorageClass) in\n")
479488
p.indent()
480489
}
481490
}

Sources/protoc-gen-swift/OneofGenerator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,9 @@ class OneofGenerator {
368368

369369
let otherStoredProperty: String
370370
if usesHeapStorage {
371-
otherStoredProperty = "other_storage.\(underscoreSwiftFieldName)"
371+
otherStoredProperty = "rhs_storage.\(underscoreSwiftFieldName)"
372372
} else {
373-
otherStoredProperty = "other.\(swiftFieldName)"
373+
otherStoredProperty = "rhs.\(swiftFieldName)"
374374
}
375375

376376
p.print("if \(storedProperty) != \(otherStoredProperty) {return false}\n")

0 commit comments

Comments
 (0)