Module interface missing raw values for public enums

If a swift module has a public enum type with raw values, the generated swiftinterface file will be missing the raw values. However, they can be obtained during runtime.
Is this by design?

One issue with this is that the raw values can't be looked up during development.

For example, if the module contains the following code

public enum Foo: Int {
    case bar = 1
    case baz = 2
    case foo = 50
}

the corresponding arm64-apple-ios.swiftinterface will contain

public enum Foo : Swift.Int {
  case bar
  case baz
  case foo
  public init?(rawValue: Swift.Int)
  public typealias RawValue = Swift.Int
  public var rawValue: Swift.Int {
    get
  }
}
1 Like

It makes sense to me that it works this way because the enum Foo: Int syntax is just shorthand for an explicit RawRepresentable conformance that implements the protocol requirements manually. You can see that this is what the compiler is doing by telling it to print the syntax tree after compiler-synthesized protocol conformances:

$ cat enum-raw-values.swift
public enum Foo: Int {
    case bar = 1
    case baz = 2
    case foo = 50
}

$ swiftc -print-ast enum-raw-values.swift
public enum Foo : Int {
  case bar
  case baz
  case foo
  @inlinable public init?(rawValue: Int) {
    switch rawValue {
    case 1:
      self = Foo.bar
    case 2:
      self = Foo.baz
    case 50:
      self = Foo.foo
    default:
      return nil
    }
  }
  public typealias RawValue = Int
  public var rawValue: Int {
    @inlinable get {
      switch self {
      case .bar:
        return 1
      case .baz:
        return 2
      case .foo:
        return 50
      }
    }
  }
}

In other words, the actual raw values only appear in the bodies of init(rawValue:) and var rawValue; they are not part of the type's interface.

5 Likes

thanks for a very swift response!
this makes total sense now, thank you for the detailed explanation.

1 Like