Skip to content

Commit

Permalink
Cleaned up generated code for closed Enums (apple#1751)
Browse files Browse the repository at this point in the history
For closed enums there no special `UNRECOGNIZED` case, so a plain Swift
`Int` enum with explicit values can be used directly.

This results in the generated `init?(rawValue:)` and `var rawValue`
functions not being needed, since the compiler will create them
automatically.
  • Loading branch information
nicked authored Feb 5, 2025
1 parent f79accc commit 4f6e091
Show file tree
Hide file tree
Showing 59 changed files with 2,556 additions and 13,830 deletions.
26 changes: 4 additions & 22 deletions CompileTests/MultiModule/Sources/ModuleA/a.pb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,15 @@ fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAP
typealias Version = _2
}

public enum E: SwiftProtobuf.Enum, Swift.CaseIterable {
public typealias RawValue = Int
case unset // = 0
case a // = 1
case b // = 2
public enum E: Int, SwiftProtobuf.Enum, Swift.CaseIterable {
case unset = 0
case a = 1
case b = 2

public init() {
self = .unset
}

public init?(rawValue: Int) {
switch rawValue {
case 0: self = .unset
case 1: self = .a
case 2: self = .b
default: return nil
}
}

public var rawValue: Int {
switch self {
case .unset: return 0
case .a: return 1
case .b: return 2
}
}

}

public struct A: SwiftProtobuf.ExtensibleMessage, Sendable {
Expand Down
30 changes: 5 additions & 25 deletions FuzzTesting/Sources/FuzzCommon/fuzz_testing.pb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,16 @@ fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAP
typealias Version = _2
}

public enum SwiftProtoTesting_Fuzz_AnEnum: SwiftProtobuf.Enum, Swift.CaseIterable {
public typealias RawValue = Int
case zero // = 0
case one // = 1
case two // = 2
case three // = 3
public enum SwiftProtoTesting_Fuzz_AnEnum: Int, SwiftProtobuf.Enum, Swift.CaseIterable {
case zero = 0
case one = 1
case two = 2
case three = 3

public init() {
self = .zero
}

public init?(rawValue: Int) {
switch rawValue {
case 0: self = .zero
case 1: self = .one
case 2: self = .two
case 3: self = .three
default: return nil
}
}

public var rawValue: Int {
switch self {
case .zero: return 0
case .one: return 1
case .two: return 2
case .three: return 3
}
}

}

public struct SwiftProtoTesting_Fuzz_Message: SwiftProtobuf.ExtensibleMessage, @unchecked Sendable {
Expand Down
26 changes: 4 additions & 22 deletions Reference/CompileTests/MultiModule/Sources/ModuleA/a.pb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,15 @@ fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAP
typealias Version = _2
}

public enum E: SwiftProtobuf.Enum, Swift.CaseIterable {
public typealias RawValue = Int
case unset // = 0
case a // = 1
case b // = 2
public enum E: Int, SwiftProtobuf.Enum, Swift.CaseIterable {
case unset = 0
case a = 1
case b = 2

public init() {
self = .unset
}

public init?(rawValue: Int) {
switch rawValue {
case 0: self = .unset
case 1: self = .a
case 2: self = .b
default: return nil
}
}

public var rawValue: Int {
switch self {
case .unset: return 0
case .a: return 1
case .b: return 2
}
}

}

public struct A: SwiftProtobuf.ExtensibleMessage, Sendable {
Expand Down
108 changes: 17 additions & 91 deletions Reference/Conformance/editions/test_messages_proto2_editions.pb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,15 @@ fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAP
typealias Version = _2
}

enum ProtobufTestMessages_Editions_Proto2_ForeignEnumProto2: SwiftProtobuf.Enum, Swift.CaseIterable {
typealias RawValue = Int
case foreignFoo // = 0
case foreignBar // = 1
case foreignBaz // = 2
enum ProtobufTestMessages_Editions_Proto2_ForeignEnumProto2: Int, SwiftProtobuf.Enum, Swift.CaseIterable {
case foreignFoo = 0
case foreignBar = 1
case foreignBaz = 2

init() {
self = .foreignFoo
}

init?(rawValue: Int) {
switch rawValue {
case 0: self = .foreignFoo
case 1: self = .foreignBar
case 2: self = .foreignBaz
default: return nil
}
}

var rawValue: Int {
switch self {
case .foreignFoo: return 0
case .foreignBar: return 1
case .foreignBaz: return 2
}
}

}

/// This proto includes every type of field in both singular and repeated
Expand Down Expand Up @@ -1033,38 +1015,18 @@ struct ProtobufTestMessages_Editions_Proto2_TestAllTypesProto2: SwiftProtobuf.Ex

}

enum NestedEnum: SwiftProtobuf.Enum, Swift.CaseIterable {
typealias RawValue = Int
case foo // = 0
case bar // = 1
case baz // = 2
enum NestedEnum: Int, SwiftProtobuf.Enum, Swift.CaseIterable {
case foo = 0
case bar = 1
case baz = 2

/// Intentionally negative.
case neg // = -1
case neg = -1

init() {
self = .foo
}

init?(rawValue: Int) {
switch rawValue {
case -1: self = .neg
case 0: self = .foo
case 1: self = .bar
case 2: self = .baz
default: return nil
}
}

var rawValue: Int {
switch self {
case .neg: return -1
case .foo: return 0
case .bar: return 1
case .baz: return 2
}
}

}

struct NestedMessage: @unchecked Sendable {
Expand Down Expand Up @@ -1374,30 +1336,14 @@ struct ProtobufTestMessages_Editions_Proto2_EnumOnlyProto2: Sendable {

var unknownFields = SwiftProtobuf.UnknownStorage()

enum BoolEnum: SwiftProtobuf.Enum, Swift.CaseIterable {
typealias RawValue = Int
case kFalse // = 0
case kTrue // = 1
enum BoolEnum: Int, SwiftProtobuf.Enum, Swift.CaseIterable {
case kFalse = 0
case kTrue = 1

init() {
self = .kFalse
}

init?(rawValue: Int) {
switch rawValue {
case 0: self = .kFalse
case 1: self = .kTrue
default: return nil
}
}

var rawValue: Int {
switch self {
case .kFalse: return 0
case .kTrue: return 1
}
}

}

init() {}
Expand Down Expand Up @@ -1817,38 +1763,18 @@ struct ProtobufTestMessages_Editions_Proto2_TestAllRequiredTypesProto2: SwiftPro

var unknownFields = SwiftProtobuf.UnknownStorage()

enum NestedEnum: SwiftProtobuf.Enum, Swift.CaseIterable {
typealias RawValue = Int
case foo // = 0
case bar // = 1
case baz // = 2
enum NestedEnum: Int, SwiftProtobuf.Enum, Swift.CaseIterable {
case foo = 0
case bar = 1
case baz = 2

/// Intentionally negative.
case neg // = -1
case neg = -1

init() {
self = .foo
}

init?(rawValue: Int) {
switch rawValue {
case -1: self = .neg
case 0: self = .foo
case 1: self = .bar
case 2: self = .baz
default: return nil
}
}

var rawValue: Int {
switch self {
case .neg: return -1
case .foo: return 0
case .bar: return 1
case .baz: return 2
}
}

}

struct NestedMessage: @unchecked Sendable {
Expand Down
Loading

0 comments on commit 4f6e091

Please sign in to comment.