If a Macro adds an enum case with an associated value, other cases are transformed at runtime. In the example below, the case foo is translated into the Macro-generated case bar(false).
There is lots of other strange behavior, such as:
Case declarations in the same file as the enum are not transformed. For example, in the code above, a function written in SimpleEnum.swift can produce SimpleEnum.foo, whereas functions in other files cannot (the declarations are transformed).
Manually-defined cases with associated values are not transformed. For example, a manually-defined case foobar(Bool) can be declared and used normally everywhere.
Additional enum cases without associated values get transformed to different cases. For example, if SimpleEnum has manual cases foo, foobar, and foobarfoo, and a Macro-generated bar(Bool), these transformations take place:
foo -> bar(false)
foobar -> bar(true)
foobarfoo -> foo
etc.
Not all associated value types in the Macro-generated case are treated the same. String associated values seems to work properly. Int yields incrementing transformations (e.g. foo -> bar(0), foobar -> bar(1), foobarfoo -> bar(2))
Replacing return ["case bar(Bool)"] with return [DeclSyntax(try EnumCaseDeclSyntax("case bar(Int)"))] seems to have no effect.
In some cases, referencing bar in the same file as you reference foo will make foo start behaving correctly.
Reproduction
Download the sample project here. The main code snippets are included below.
Description
If a Macro adds an enum case with an associated value, other cases are transformed at runtime. In the example below, the case
foo
is translated into the Macro-generated casebar(false)
.There is lots of other strange behavior, such as:
SimpleEnum.swift
can produceSimpleEnum.foo
, whereas functions in other files cannot (the declarations are transformed).case foobar(Bool)
can be declared and used normally everywhere.SimpleEnum
has manual casesfoo
,foobar
, andfoobarfoo
, and a Macro-generatedbar(Bool)
, these transformations take place:Int
yields incrementing transformations (e.g. foo -> bar(0), foobar -> bar(1), foobarfoo -> bar(2))return ["case bar(Bool)"]
withreturn [DeclSyntax(try EnumCaseDeclSyntax("case bar(Int)"))]
seems to have no effect.bar
in the same file as you referencefoo
will makefoo
start behaving correctly.Reproduction
Download the sample project here. The main code snippets are included below.
The program prints "bar(false)"
Expected behavior
The program prints "foo".
Environment
SwiftSyntax version 509.0.2
Additional information
Reproduced in Xcode versions 15.0.1 and 15.1 beta 3. Also discussed on the Swift Forums and Apple Developer Forums.