Skip to content

Commit 4f6e091

Browse files
authored
Cleaned up generated code for closed Enums (apple#1751)
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.
1 parent f79accc commit 4f6e091

File tree

59 files changed

+2556
-13830
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2556
-13830
lines changed

CompileTests/MultiModule/Sources/ModuleA/a.pb.swift

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,15 @@ fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAP
2020
typealias Version = _2
2121
}
2222

23-
public enum E: SwiftProtobuf.Enum, Swift.CaseIterable {
24-
public typealias RawValue = Int
25-
case unset // = 0
26-
case a // = 1
27-
case b // = 2
23+
public enum E: Int, SwiftProtobuf.Enum, Swift.CaseIterable {
24+
case unset = 0
25+
case a = 1
26+
case b = 2
2827

2928
public init() {
3029
self = .unset
3130
}
3231

33-
public init?(rawValue: Int) {
34-
switch rawValue {
35-
case 0: self = .unset
36-
case 1: self = .a
37-
case 2: self = .b
38-
default: return nil
39-
}
40-
}
41-
42-
public var rawValue: Int {
43-
switch self {
44-
case .unset: return 0
45-
case .a: return 1
46-
case .b: return 2
47-
}
48-
}
49-
5032
}
5133

5234
public struct A: SwiftProtobuf.ExtensibleMessage, Sendable {

FuzzTesting/Sources/FuzzCommon/fuzz_testing.pb.swift

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,16 @@ fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAP
3535
typealias Version = _2
3636
}
3737

38-
public enum SwiftProtoTesting_Fuzz_AnEnum: SwiftProtobuf.Enum, Swift.CaseIterable {
39-
public typealias RawValue = Int
40-
case zero // = 0
41-
case one // = 1
42-
case two // = 2
43-
case three // = 3
38+
public enum SwiftProtoTesting_Fuzz_AnEnum: Int, SwiftProtobuf.Enum, Swift.CaseIterable {
39+
case zero = 0
40+
case one = 1
41+
case two = 2
42+
case three = 3
4443

4544
public init() {
4645
self = .zero
4746
}
4847

49-
public init?(rawValue: Int) {
50-
switch rawValue {
51-
case 0: self = .zero
52-
case 1: self = .one
53-
case 2: self = .two
54-
case 3: self = .three
55-
default: return nil
56-
}
57-
}
58-
59-
public var rawValue: Int {
60-
switch self {
61-
case .zero: return 0
62-
case .one: return 1
63-
case .two: return 2
64-
case .three: return 3
65-
}
66-
}
67-
6848
}
6949

7050
public struct SwiftProtoTesting_Fuzz_Message: SwiftProtobuf.ExtensibleMessage, @unchecked Sendable {

Reference/CompileTests/MultiModule/Sources/ModuleA/a.pb.swift

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,15 @@ fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAP
2020
typealias Version = _2
2121
}
2222

23-
public enum E: SwiftProtobuf.Enum, Swift.CaseIterable {
24-
public typealias RawValue = Int
25-
case unset // = 0
26-
case a // = 1
27-
case b // = 2
23+
public enum E: Int, SwiftProtobuf.Enum, Swift.CaseIterable {
24+
case unset = 0
25+
case a = 1
26+
case b = 2
2827

2928
public init() {
3029
self = .unset
3130
}
3231

33-
public init?(rawValue: Int) {
34-
switch rawValue {
35-
case 0: self = .unset
36-
case 1: self = .a
37-
case 2: self = .b
38-
default: return nil
39-
}
40-
}
41-
42-
public var rawValue: Int {
43-
switch self {
44-
case .unset: return 0
45-
case .a: return 1
46-
case .b: return 2
47-
}
48-
}
49-
5032
}
5133

5234
public struct A: SwiftProtobuf.ExtensibleMessage, Sendable {

Reference/Conformance/editions/test_messages_proto2_editions.pb.swift

Lines changed: 17 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,15 @@ fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAP
3434
typealias Version = _2
3535
}
3636

37-
enum ProtobufTestMessages_Editions_Proto2_ForeignEnumProto2: SwiftProtobuf.Enum, Swift.CaseIterable {
38-
typealias RawValue = Int
39-
case foreignFoo // = 0
40-
case foreignBar // = 1
41-
case foreignBaz // = 2
37+
enum ProtobufTestMessages_Editions_Proto2_ForeignEnumProto2: Int, SwiftProtobuf.Enum, Swift.CaseIterable {
38+
case foreignFoo = 0
39+
case foreignBar = 1
40+
case foreignBaz = 2
4241

4342
init() {
4443
self = .foreignFoo
4544
}
4645

47-
init?(rawValue: Int) {
48-
switch rawValue {
49-
case 0: self = .foreignFoo
50-
case 1: self = .foreignBar
51-
case 2: self = .foreignBaz
52-
default: return nil
53-
}
54-
}
55-
56-
var rawValue: Int {
57-
switch self {
58-
case .foreignFoo: return 0
59-
case .foreignBar: return 1
60-
case .foreignBaz: return 2
61-
}
62-
}
63-
6446
}
6547

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

10341016
}
10351017

1036-
enum NestedEnum: SwiftProtobuf.Enum, Swift.CaseIterable {
1037-
typealias RawValue = Int
1038-
case foo // = 0
1039-
case bar // = 1
1040-
case baz // = 2
1018+
enum NestedEnum: Int, SwiftProtobuf.Enum, Swift.CaseIterable {
1019+
case foo = 0
1020+
case bar = 1
1021+
case baz = 2
10411022

10421023
/// Intentionally negative.
1043-
case neg // = -1
1024+
case neg = -1
10441025

10451026
init() {
10461027
self = .foo
10471028
}
10481029

1049-
init?(rawValue: Int) {
1050-
switch rawValue {
1051-
case -1: self = .neg
1052-
case 0: self = .foo
1053-
case 1: self = .bar
1054-
case 2: self = .baz
1055-
default: return nil
1056-
}
1057-
}
1058-
1059-
var rawValue: Int {
1060-
switch self {
1061-
case .neg: return -1
1062-
case .foo: return 0
1063-
case .bar: return 1
1064-
case .baz: return 2
1065-
}
1066-
}
1067-
10681030
}
10691031

10701032
struct NestedMessage: @unchecked Sendable {
@@ -1374,30 +1336,14 @@ struct ProtobufTestMessages_Editions_Proto2_EnumOnlyProto2: Sendable {
13741336

13751337
var unknownFields = SwiftProtobuf.UnknownStorage()
13761338

1377-
enum BoolEnum: SwiftProtobuf.Enum, Swift.CaseIterable {
1378-
typealias RawValue = Int
1379-
case kFalse // = 0
1380-
case kTrue // = 1
1339+
enum BoolEnum: Int, SwiftProtobuf.Enum, Swift.CaseIterable {
1340+
case kFalse = 0
1341+
case kTrue = 1
13811342

13821343
init() {
13831344
self = .kFalse
13841345
}
13851346

1386-
init?(rawValue: Int) {
1387-
switch rawValue {
1388-
case 0: self = .kFalse
1389-
case 1: self = .kTrue
1390-
default: return nil
1391-
}
1392-
}
1393-
1394-
var rawValue: Int {
1395-
switch self {
1396-
case .kFalse: return 0
1397-
case .kTrue: return 1
1398-
}
1399-
}
1400-
14011347
}
14021348

14031349
init() {}
@@ -1817,38 +1763,18 @@ struct ProtobufTestMessages_Editions_Proto2_TestAllRequiredTypesProto2: SwiftPro
18171763

18181764
var unknownFields = SwiftProtobuf.UnknownStorage()
18191765

1820-
enum NestedEnum: SwiftProtobuf.Enum, Swift.CaseIterable {
1821-
typealias RawValue = Int
1822-
case foo // = 0
1823-
case bar // = 1
1824-
case baz // = 2
1766+
enum NestedEnum: Int, SwiftProtobuf.Enum, Swift.CaseIterable {
1767+
case foo = 0
1768+
case bar = 1
1769+
case baz = 2
18251770

18261771
/// Intentionally negative.
1827-
case neg // = -1
1772+
case neg = -1
18281773

18291774
init() {
18301775
self = .foo
18311776
}
18321777

1833-
init?(rawValue: Int) {
1834-
switch rawValue {
1835-
case -1: self = .neg
1836-
case 0: self = .foo
1837-
case 1: self = .bar
1838-
case 2: self = .baz
1839-
default: return nil
1840-
}
1841-
}
1842-
1843-
var rawValue: Int {
1844-
switch self {
1845-
case .neg: return -1
1846-
case .foo: return 0
1847-
case .bar: return 1
1848-
case .baz: return 2
1849-
}
1850-
}
1851-
18521778
}
18531779

18541780
struct NestedMessage: @unchecked Sendable {

0 commit comments

Comments
 (0)