Skip to content

Commit

Permalink
Move CaseIterable conformance to library base Enum type
Browse files Browse the repository at this point in the history
Update generation to also add allCases (when needed) in that main decl for the
enum.

Progress on apple#1210
  • Loading branch information
thomasvl committed Apr 7, 2022
1 parent 12c26b8 commit 4148be4
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Sources/SwiftProtobuf/Enum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// -----------------------------------------------------------------------------

/// Generated enum types conform to this protocol.
public protocol Enum: RawRepresentable, Hashable, _ProtoSendable {
public protocol Enum: RawRepresentable, Hashable, CaseIterable, _ProtoSendable {
/// Creates a new instance of the enum initialized to its default value.
init()

Expand Down
28 changes: 10 additions & 18 deletions Sources/protoc-gen-swift/EnumGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,33 +80,25 @@ class EnumGenerator {
p.print("\n")
generateRawValueProperty(printer: &p)

maybeGenerateCaseIterable(printer: &p)

p.outdent()
p.print("\n")
p.print("}\n")
}

func generateCaseIterable(printer p: inout CodePrinter) {
// NOTE: When we can assume Swift 4.2, this should move from an extension
// to being directly done when declaring the type.
func maybeGenerateCaseIterable(printer p: inout CodePrinter) {
guard enumDescriptor.hasUnknownPreservingSemantics else { return }

let visibility = generatorOptions.visibilitySourceSnippet

p.print("\n")
p.print("extension \(swiftFullName): CaseIterable {\n")
p.indent()
if enumDescriptor.hasUnknownPreservingSemantics {
p.print("// The compiler won't synthesize support with the \(unrecognizedCaseName) case.\n")
p.print("\(visibility)static var allCases: [\(swiftFullName)] = [\n")
for v in mainEnumValueDescriptors {
let dottedName = namer.dottedRelativeName(enumValue: v)
p.print(" \(dottedName),\n")
}
p.print("]\n")
} else {
p.print("// Support synthesized by the compiler.\n")
p.print("// The compiler won't synthesize support with the \(unrecognizedCaseName) case.\n")
p.print("\(visibility)static var allCases: [\(swiftFullName)] = [\n")
for v in mainEnumValueDescriptors {
let dottedName = namer.dottedRelativeName(enumValue: v)
p.print(" \(dottedName),\n")
}
p.outdent()
p.print("}\n")
p.print("]\n")
}

func generateRuntimeSupport(printer p: inout CodePrinter) {
Expand Down
2 changes: 0 additions & 2 deletions Sources/protoc-gen-swift/FileGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,10 @@ class FileGenerator {

for e in enums {
e.generateMainEnum(printer: &p)
e.generateCaseIterable(printer: &p)
}

for m in messages {
m.generateMainStruct(printer: &p, parent: nil, errorString: &errorString)
m.generateEnumCaseIterable(printer: &p)
}

var sendablePrinter = CodePrinter()
Expand Down
9 changes: 0 additions & 9 deletions Sources/protoc-gen-swift/MessageGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,6 @@ class MessageGenerator {
p.print("}\n")
}

func generateEnumCaseIterable(printer p: inout CodePrinter) {
for e in enums {
e.generateCaseIterable(printer: &p)
}
for m in messages {
m.generateEnumCaseIterable(printer: &p)
}
}

func generateSendable(printer p: inout CodePrinter) {
// Once our minimum supported version has Data be Sendable, @unchecked
// will not be needed for all messages, provided that the extension types
Expand Down

0 comments on commit 4148be4

Please sign in to comment.