square / square/wire

Swift recursive enums not marked as indirect

Open
#2,448 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Kotlin
Stars
4.4k
Forks
627
Avg merge
3d 15m
Merged PRs (30d)
20

Description

Given a recursive message containing an oneof, generated Swift does not compile because enums are not marked with indirect keyword.

Proto:

message RecursiveMessage {
  oneof value {
    string value1 = 1;
    RecursiveMessage value2 = 2;
  }
}

Generated Swift:

public struct RecursiveMessage {

    public var value: Value?
    public var unknownFields: Data = .init()

    public init(value: Value? = nil) {
        self.value = value
    }

    public enum Value { /// ❌ Recursive enum 'RecursiveMesage.Value' is not marked 'indirect'

        case value1(String)
        case value2(RecursiveMessage)

        fileprivate func encode(to writer: ProtoWriter) throws {
            switch self {
            case .value1(let value1): try writer.encode(tag: 1, value: value1)
            case .value2(let value2): try writer.encode(tag: 2, value: value2)
            }
        }

    }

}

Marking case value2(RecursiveMessage) as indirect fixes the error.

A function for checking for reference cycles exists in the Swift generator here, however it does not seem to be used for enums. (per @JakeWharton).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in wire-swift-generator/src/main/java/com/squareup/wire/swift/SwiftGenerator.kt, especially the reference-cycle check around line 202 and enum generation around line 1372. Run the Swift generator tests that cover recursive messages, then verify that the shown RecursiveMessage proto generates Swift that compiles and that existing non-recursive enums remain unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, swift
Domain
compilers, devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.