Are nested/recursive Macros supported in Swift 5.9?

I just had an idea for an architectural-level macro where my macro would help structure features of an app. I have not had the time to play around with the idea yet (to figure out if a Macro is even a good choice), but some of the "expanded" code of my Macro could potentially involve other macros, such as @Observable. I wonder if macro expansion works even with nested macros?

I haven't found anything on the topic in this forum or in the Macros Vison document (I searched for "nested" and "recursive" macros). Would be really useful if nested/recursive macros were supported. :crossed_fingers:

It's working.

As an example the Model macro from SwiftData expansion contains others macros like PersistedProperty macro.

Xcode also supports expanding nested macro in the IDE.


Awesome! :muscle::tada:

I think it's worth to mention that recursive macros are not supported. I asked about this at WWDC and received the following response:

The compiler will emit an error if there is a cycle in the expansion of macros.

Nested macro expansion is supported. You will definitely be interested in member-attribute macros which primarily exist to facilitate nested macro expansion like you're describing. This is what @Observable uses to apply accessor macros to all properties within a type.

1 Like

Are there any examples of this in action for reference?

For example, the @Model macro from SwiftData, among other things, adds the @PersistedProperty macro to all stored properties.


Is it possible for one macro to nest more macros on the declaration itself, for example, would it be possible to have something like this:

enum Fruit {
    case apple, banana

On expanding MacroA:

enum Fruit {
    case apple, banana


1 Like