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

Undo breaking changes in SwiftProtobufPluginLibrary #1666

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import SwiftProtobuf

/// The spec for editions calls out them being ordered and comparable.
/// https://github.com/protocolbuffers/protobuf/blob/main/docs/design/editions/edition-naming.md
extension Google_Protobuf_Edition: Comparable {
extension Google_Protobuf_Edition: @retroactive Comparable {
public static func < (lhs: Google_Protobuf_Edition, rhs: Google_Protobuf_Edition) -> Bool {
return lhs.rawValue < rhs.rawValue
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/protoc-gen-swift/Descriptor+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ extension Descriptor {

// If it can support extensions, then return true as an extension could
// have a required field.
if !descriptor.extensionRanges.isEmpty {
if !descriptor.protoExtensionRanges.isEmpty {
return true
}

Expand All @@ -209,8 +209,8 @@ extension Descriptor {
///
/// This also uses Range<> since the options that could be on
/// `extensionRanges` no longer can apply as the things have been merged.
var normalizedExtensionRanges: [Range<Int32>] {
var ordered: [Range<Int32>] = self.extensionRanges.sorted(by: {
var _normalizedExtensionRanges: [Range<Int32>] {
var ordered: [Range<Int32>] = self.protoExtensionRanges.sorted(by: {
return $0.start < $1.start }).map { return $0.start ..< $0.end
}
if ordered.count > 1 {
Expand All @@ -231,8 +231,8 @@ extension Descriptor {
///
/// This also uses Range<> since the options that could be on
/// `extensionRanges` no longer can apply as the things have been merged.
var ambitiousExtensionRanges: [Range<Int32>] {
var merged = self.normalizedExtensionRanges
var _ambitiousExtensionRanges: [Range<Int32>] {
var merged = self._normalizedExtensionRanges
if merged.count > 1 {
var fieldNumbersReversedIterator =
self.fields.map({ Int($0.number) }).sorted(by: { $0 > $1 }).makeIterator()
Expand Down
4 changes: 2 additions & 2 deletions Sources/protoc-gen-swift/FieldGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class FieldGeneratorBase {
result = ".standard(proto: \"\(protoName)\")"
} else {
/// The library's generation didn't match, so specify this explicitly.
result = ".unique(proto: \"\(protoName)\", json: \"\(jsonName)\")"
result = ".unique(proto: \"\(protoName)\", json: \"\(String(describing: jsonName))\")"
thomasvl marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -105,7 +105,7 @@ class FieldGeneratorBase {
if nameLowercase == jsonName {
return [".same(proto: \"\(nameLowercase)\")", result]
} else {
return [".unique(proto: \"\(nameLowercase)\", json: \"\(jsonName)\")", result]
return [".unique(proto: \"\(nameLowercase)\", json: \"\(String(describing: jsonName))\")", result]
}
} else {
return [result]
Expand Down
6 changes: 3 additions & 3 deletions Sources/protoc-gen-swift/MessageGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class MessageGenerator {
self.namer = namer

visibility = generatorOptions.visibilitySourceSnippet
isExtensible = !descriptor.extensionRanges.isEmpty
isExtensible = !descriptor.protoExtensionRanges.isEmpty
swiftRelativeName = namer.relativeName(message: descriptor)
swiftFullName = namer.fullName(message: descriptor)

Expand Down Expand Up @@ -284,7 +284,7 @@ class MessageGenerator {
// code. This also avoids typechecking performance issues if there are
// dozens of ranges because we aren't constructing a single large
// expression containing untyped integer literals.
let normalizedExtensionRanges = descriptor.normalizedExtensionRanges
let normalizedExtensionRanges = descriptor._normalizedExtensionRanges
if !fields.isEmpty || normalizedExtensionRanges.count > 3 {
p.print("""
// The use of inline closures is to circumvent an issue where the compiler
Expand Down Expand Up @@ -346,7 +346,7 @@ class MessageGenerator {
// Use the "ambitious" ranges because for visit because subranges with no
// intermixed fields can be merged to reduce the number of calls for
// extension visitation.
var ranges = descriptor.ambitiousExtensionRanges.makeIterator()
var ranges = descriptor._ambitiousExtensionRanges.makeIterator()
var nextRange = ranges.next()
for f in fieldsSortedByNumber {
while nextRange != nil && Int(nextRange!.lowerBound) < f.number {
Expand Down
2 changes: 1 addition & 1 deletion Sources/protoc-gen-swift/OneofGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class OneofGenerator {
// from each extension range as an easy way to check for them being
// mixed in between the fields.
var parentNumbers = descriptor.containingType.fields.map { Int($0.number) }
parentNumbers.append(contentsOf: descriptor.containingType.normalizedExtensionRanges.map { Int($0.lowerBound) })
parentNumbers.append(contentsOf: descriptor.containingType._normalizedExtensionRanges.map { Int($0.lowerBound) })
var parentNumbersIterator = parentNumbers.sorted(by: { $0 < $1 }).makeIterator()
var nextParentFieldNumber = parentNumbersIterator.next()
var grouped = [[MemberFieldGenerator]]()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ final class Test_Descriptor_FeatureResolution: XCTestCase {
}
""")

let features = context.file.messages.first!.extensionRanges.first!.features
let features = context.file.messages.first!.protoExtensionRanges.first!.features
XCTAssertTrue(features.hasSwiftFeatureTest_test)
XCTAssertEqual(features.SwiftFeatureTest_test.feature1, .value1)
XCTAssertEqual(features.SwiftFeatureTest_test.feature2, .value1)
Expand Down Expand Up @@ -300,7 +300,7 @@ final class Test_Descriptor_FeatureResolution: XCTestCase {
}
""")

let features = context.file.messages.first!.extensionRanges.first!.features
let features = context.file.messages.first!.protoExtensionRanges.first!.features
XCTAssertTrue(features.hasSwiftFeatureTest_test)
XCTAssertEqual(features.SwiftFeatureTest_test.feature1, .value2) // File override
XCTAssertEqual(features.SwiftFeatureTest_test.feature2, .value3) // Message override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import SwiftProtobufTestHelpers
@testable import SwiftProtobufPluginLibrary

// Support equality to simplify testing of getting the correct errors.
extension ProtoFileToModuleMappings.LoadError: Equatable {
// We mark it as `@retroactive` because we're only using this for testing.
extension ProtoFileToModuleMappings.LoadError: @retroactive Equatable {
public static func ==(lhs: ProtoFileToModuleMappings.LoadError, rhs: ProtoFileToModuleMappings.LoadError) -> Bool {
switch (lhs, rhs) {
case (.entryMissingModuleName(let l), .entryMissingModuleName(let r)): return l == r
Expand Down
140 changes: 70 additions & 70 deletions Tests/protoc-gen-swiftTests/Test_DescriptorExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,96 +24,96 @@ final class Test_DescriptorExtensions: XCTestCase {

let msgDescriptor = descriptorSet.descriptor(named: "swift_descriptor_test.MsgExtensionRangeOrdering")!
// Quick check of what should be in the proto file
XCTAssertEqual(msgDescriptor.extensionRanges.count, 9)
XCTAssertEqual(msgDescriptor.extensionRanges[0].start, 1)
XCTAssertEqual(msgDescriptor.extensionRanges[1].start, 3)
XCTAssertEqual(msgDescriptor.extensionRanges[2].start, 2)
XCTAssertEqual(msgDescriptor.extensionRanges[3].start, 4)
XCTAssertEqual(msgDescriptor.extensionRanges[4].start, 7)
XCTAssertEqual(msgDescriptor.extensionRanges[5].start, 9)
XCTAssertEqual(msgDescriptor.extensionRanges[6].start, 100)
XCTAssertEqual(msgDescriptor.extensionRanges[7].start, 126)
XCTAssertEqual(msgDescriptor.extensionRanges[8].start, 111)
XCTAssertEqual(msgDescriptor.protoExtensionRanges.count, 9)
XCTAssertEqual(msgDescriptor.protoExtensionRanges[0].start, 1)
XCTAssertEqual(msgDescriptor.protoExtensionRanges[1].start, 3)
XCTAssertEqual(msgDescriptor.protoExtensionRanges[2].start, 2)
XCTAssertEqual(msgDescriptor.protoExtensionRanges[3].start, 4)
XCTAssertEqual(msgDescriptor.protoExtensionRanges[4].start, 7)
XCTAssertEqual(msgDescriptor.protoExtensionRanges[5].start, 9)
XCTAssertEqual(msgDescriptor.protoExtensionRanges[6].start, 100)
XCTAssertEqual(msgDescriptor.protoExtensionRanges[7].start, 126)
XCTAssertEqual(msgDescriptor.protoExtensionRanges[8].start, 111)

// Check sorting/merging
XCTAssertEqual(msgDescriptor.normalizedExtensionRanges.count, 5)
XCTAssertEqual(msgDescriptor.normalizedExtensionRanges[0].lowerBound, 1)
XCTAssertEqual(msgDescriptor.normalizedExtensionRanges[0].upperBound, 5)
XCTAssertEqual(msgDescriptor.normalizedExtensionRanges[1].lowerBound, 7)
XCTAssertEqual(msgDescriptor.normalizedExtensionRanges[1].upperBound, 8)
XCTAssertEqual(msgDescriptor.normalizedExtensionRanges[2].lowerBound, 9)
XCTAssertEqual(msgDescriptor.normalizedExtensionRanges[2].upperBound, 10)
XCTAssertEqual(msgDescriptor.normalizedExtensionRanges[3].lowerBound, 100)
XCTAssertEqual(msgDescriptor.normalizedExtensionRanges[3].upperBound, 121)
XCTAssertEqual(msgDescriptor.normalizedExtensionRanges[4].lowerBound, 126)
XCTAssertEqual(msgDescriptor.normalizedExtensionRanges[4].upperBound, 131)
XCTAssertEqual(msgDescriptor._normalizedExtensionRanges.count, 5)
XCTAssertEqual(msgDescriptor._normalizedExtensionRanges[0].lowerBound, 1)
XCTAssertEqual(msgDescriptor._normalizedExtensionRanges[0].upperBound, 5)
XCTAssertEqual(msgDescriptor._normalizedExtensionRanges[1].lowerBound, 7)
XCTAssertEqual(msgDescriptor._normalizedExtensionRanges[1].upperBound, 8)
XCTAssertEqual(msgDescriptor._normalizedExtensionRanges[2].lowerBound, 9)
XCTAssertEqual(msgDescriptor._normalizedExtensionRanges[2].upperBound, 10)
XCTAssertEqual(msgDescriptor._normalizedExtensionRanges[3].lowerBound, 100)
XCTAssertEqual(msgDescriptor._normalizedExtensionRanges[3].upperBound, 121)
XCTAssertEqual(msgDescriptor._normalizedExtensionRanges[4].lowerBound, 126)
XCTAssertEqual(msgDescriptor._normalizedExtensionRanges[4].upperBound, 131)


// Check the "ambitious" merging.
XCTAssertEqual(msgDescriptor.ambitiousExtensionRanges.count, 1)
XCTAssertEqual(msgDescriptor.ambitiousExtensionRanges[0].lowerBound, 1)
XCTAssertEqual(msgDescriptor.ambitiousExtensionRanges[0].upperBound, 131)
XCTAssertEqual(msgDescriptor._ambitiousExtensionRanges.count, 1)
XCTAssertEqual(msgDescriptor._ambitiousExtensionRanges[0].lowerBound, 1)
XCTAssertEqual(msgDescriptor._ambitiousExtensionRanges[0].upperBound, 131)

let msgDescriptor2 = descriptorSet.descriptor(named: "swift_descriptor_test.MsgExtensionRangeOrderingWithFields")!
// Quick check of what should be in the proto file
XCTAssertEqual(msgDescriptor2.extensionRanges.count, 9)
XCTAssertEqual(msgDescriptor2.extensionRanges[0].start, 1)
XCTAssertEqual(msgDescriptor2.extensionRanges[1].start, 3)
XCTAssertEqual(msgDescriptor2.extensionRanges[2].start, 2)
XCTAssertEqual(msgDescriptor2.extensionRanges[3].start, 4)
XCTAssertEqual(msgDescriptor2.extensionRanges[4].start, 7)
XCTAssertEqual(msgDescriptor2.extensionRanges[5].start, 9)
XCTAssertEqual(msgDescriptor2.extensionRanges[6].start, 100)
XCTAssertEqual(msgDescriptor2.extensionRanges[7].start, 126)
XCTAssertEqual(msgDescriptor2.extensionRanges[8].start, 111)
XCTAssertEqual(msgDescriptor2.protoExtensionRanges.count, 9)
XCTAssertEqual(msgDescriptor2.protoExtensionRanges[0].start, 1)
XCTAssertEqual(msgDescriptor2.protoExtensionRanges[1].start, 3)
XCTAssertEqual(msgDescriptor2.protoExtensionRanges[2].start, 2)
XCTAssertEqual(msgDescriptor2.protoExtensionRanges[3].start, 4)
XCTAssertEqual(msgDescriptor2.protoExtensionRanges[4].start, 7)
XCTAssertEqual(msgDescriptor2.protoExtensionRanges[5].start, 9)
XCTAssertEqual(msgDescriptor2.protoExtensionRanges[6].start, 100)
XCTAssertEqual(msgDescriptor2.protoExtensionRanges[7].start, 126)
XCTAssertEqual(msgDescriptor2.protoExtensionRanges[8].start, 111)

// Check sorting/merging
XCTAssertEqual(msgDescriptor2.normalizedExtensionRanges.count, 5)
XCTAssertEqual(msgDescriptor2.normalizedExtensionRanges[0].lowerBound, 1)
XCTAssertEqual(msgDescriptor2.normalizedExtensionRanges[0].upperBound, 5)
XCTAssertEqual(msgDescriptor2.normalizedExtensionRanges[1].lowerBound, 7)
XCTAssertEqual(msgDescriptor2.normalizedExtensionRanges[1].upperBound, 8)
XCTAssertEqual(msgDescriptor2.normalizedExtensionRanges[2].lowerBound, 9)
XCTAssertEqual(msgDescriptor2.normalizedExtensionRanges[2].upperBound, 10)
XCTAssertEqual(msgDescriptor2.normalizedExtensionRanges[3].lowerBound, 100)
XCTAssertEqual(msgDescriptor2.normalizedExtensionRanges[3].upperBound, 121)
XCTAssertEqual(msgDescriptor2.normalizedExtensionRanges[4].lowerBound, 126)
XCTAssertEqual(msgDescriptor2.normalizedExtensionRanges[4].upperBound, 131)
XCTAssertEqual(msgDescriptor2._normalizedExtensionRanges.count, 5)
XCTAssertEqual(msgDescriptor2._normalizedExtensionRanges[0].lowerBound, 1)
XCTAssertEqual(msgDescriptor2._normalizedExtensionRanges[0].upperBound, 5)
XCTAssertEqual(msgDescriptor2._normalizedExtensionRanges[1].lowerBound, 7)
XCTAssertEqual(msgDescriptor2._normalizedExtensionRanges[1].upperBound, 8)
XCTAssertEqual(msgDescriptor2._normalizedExtensionRanges[2].lowerBound, 9)
XCTAssertEqual(msgDescriptor2._normalizedExtensionRanges[2].upperBound, 10)
XCTAssertEqual(msgDescriptor2._normalizedExtensionRanges[3].lowerBound, 100)
XCTAssertEqual(msgDescriptor2._normalizedExtensionRanges[3].upperBound, 121)
XCTAssertEqual(msgDescriptor2._normalizedExtensionRanges[4].lowerBound, 126)
XCTAssertEqual(msgDescriptor2._normalizedExtensionRanges[4].upperBound, 131)


// Check the "ambitious" merging.
XCTAssertEqual(msgDescriptor2.ambitiousExtensionRanges.count, 3)
XCTAssertEqual(msgDescriptor2.ambitiousExtensionRanges[0].lowerBound, 1)
XCTAssertEqual(msgDescriptor2.ambitiousExtensionRanges[0].upperBound, 5)
XCTAssertEqual(msgDescriptor2.ambitiousExtensionRanges[1].lowerBound, 7)
XCTAssertEqual(msgDescriptor2.ambitiousExtensionRanges[1].upperBound, 121)
XCTAssertEqual(msgDescriptor2.ambitiousExtensionRanges[2].lowerBound, 126)
XCTAssertEqual(msgDescriptor2.ambitiousExtensionRanges[2].upperBound, 131)
XCTAssertEqual(msgDescriptor2._ambitiousExtensionRanges.count, 3)
XCTAssertEqual(msgDescriptor2._ambitiousExtensionRanges[0].lowerBound, 1)
XCTAssertEqual(msgDescriptor2._ambitiousExtensionRanges[0].upperBound, 5)
XCTAssertEqual(msgDescriptor2._ambitiousExtensionRanges[1].lowerBound, 7)
XCTAssertEqual(msgDescriptor2._ambitiousExtensionRanges[1].upperBound, 121)
XCTAssertEqual(msgDescriptor2._ambitiousExtensionRanges[2].lowerBound, 126)
XCTAssertEqual(msgDescriptor2._ambitiousExtensionRanges[2].upperBound, 131)

let msgDescriptor3 = descriptorSet.descriptor(named: "swift_descriptor_test.MsgExtensionRangeOrderingNoMerging")!
// Quick check of what should be in the proto file
XCTAssertEqual(msgDescriptor3.extensionRanges.count, 3)
XCTAssertEqual(msgDescriptor3.extensionRanges[0].start, 3)
XCTAssertEqual(msgDescriptor3.extensionRanges[1].start, 7)
XCTAssertEqual(msgDescriptor3.extensionRanges[2].start, 16)
XCTAssertEqual(msgDescriptor3.protoExtensionRanges.count, 3)
XCTAssertEqual(msgDescriptor3.protoExtensionRanges[0].start, 3)
XCTAssertEqual(msgDescriptor3.protoExtensionRanges[1].start, 7)
XCTAssertEqual(msgDescriptor3.protoExtensionRanges[2].start, 16)

// Check sorting/merging
XCTAssertEqual(msgDescriptor3.normalizedExtensionRanges.count, 3)
XCTAssertEqual(msgDescriptor3.normalizedExtensionRanges[0].lowerBound, 3)
XCTAssertEqual(msgDescriptor3.normalizedExtensionRanges[0].upperBound, 6)
XCTAssertEqual(msgDescriptor3.normalizedExtensionRanges[1].lowerBound, 7)
XCTAssertEqual(msgDescriptor3.normalizedExtensionRanges[1].upperBound, 13)
XCTAssertEqual(msgDescriptor3.normalizedExtensionRanges[2].lowerBound, 16)
XCTAssertEqual(msgDescriptor3.normalizedExtensionRanges[2].upperBound, 21)
XCTAssertEqual(msgDescriptor3._normalizedExtensionRanges.count, 3)
XCTAssertEqual(msgDescriptor3._normalizedExtensionRanges[0].lowerBound, 3)
XCTAssertEqual(msgDescriptor3._normalizedExtensionRanges[0].upperBound, 6)
XCTAssertEqual(msgDescriptor3._normalizedExtensionRanges[1].lowerBound, 7)
XCTAssertEqual(msgDescriptor3._normalizedExtensionRanges[1].upperBound, 13)
XCTAssertEqual(msgDescriptor3._normalizedExtensionRanges[2].lowerBound, 16)
XCTAssertEqual(msgDescriptor3._normalizedExtensionRanges[2].upperBound, 21)

// Check the "ambitious" merging.
XCTAssertEqual(msgDescriptor3.ambitiousExtensionRanges.count, 3)
XCTAssertEqual(msgDescriptor3.ambitiousExtensionRanges[0].lowerBound, 3)
XCTAssertEqual(msgDescriptor3.ambitiousExtensionRanges[0].upperBound, 6)
XCTAssertEqual(msgDescriptor3.ambitiousExtensionRanges[1].lowerBound, 7)
XCTAssertEqual(msgDescriptor3.ambitiousExtensionRanges[1].upperBound, 13)
XCTAssertEqual(msgDescriptor3.ambitiousExtensionRanges[2].lowerBound, 16)
XCTAssertEqual(msgDescriptor3.ambitiousExtensionRanges[2].upperBound, 21)
XCTAssertEqual(msgDescriptor3._ambitiousExtensionRanges.count, 3)
XCTAssertEqual(msgDescriptor3._ambitiousExtensionRanges[0].lowerBound, 3)
XCTAssertEqual(msgDescriptor3._ambitiousExtensionRanges[0].upperBound, 6)
XCTAssertEqual(msgDescriptor3._ambitiousExtensionRanges[1].lowerBound, 7)
XCTAssertEqual(msgDescriptor3._ambitiousExtensionRanges[1].upperBound, 13)
XCTAssertEqual(msgDescriptor3._ambitiousExtensionRanges[2].lowerBound, 16)
XCTAssertEqual(msgDescriptor3._ambitiousExtensionRanges[2].upperBound, 21)
}

func testEnumValueAliasing() throws {
Expand Down
Loading