Skip to content

SyntaxProtocol.expand() does not expand peer macros introduced by other macros #1805

Open
@tgoyne

Description

@tgoyne

Description

This test fails due to Foo not having the second bar field (while Bar passes). This works correctly when expanded by the compiler plugin (although in this example it obviously result in a compilation error due to the property being redeclared).

public struct DuplicateAll: MemberAttributeMacro {
    public static func expansion(
        of node: AttributeSyntax,
        attachedTo declaration: some DeclGroupSyntax,
        providingAttributesFor member: some DeclSyntaxProtocol,
        in context: some MacroExpansionContext
    ) throws -> [AttributeSyntax] {
        return [AttributeSyntax(attributeName: SimpleTypeIdentifierSyntax(name: .identifier("Duplicate")))]
    }
}

public struct Duplicate: PeerMacro {
    public static func expansion(of node: AttributeSyntax, providingPeersOf declaration: some DeclSyntaxProtocol, in context: some MacroExpansionContext) throws -> [DeclSyntax] {
        if var decl = declaration.as(VariableDeclSyntax.self) {
            decl.attributes = nil
            return [DeclSyntax(decl)]
        }
        return []
    }
}

final class reproTests: XCTestCase {
    func testMacro() {
        assertMacroExpansion(
            """
            @DuplicateAll
            class Foo {
                var bar: Int = 0
            }
            class Bar {
                @Duplicate
                var bar: Int = 0
            }
            """,
            expandedSource: """

            class Foo {
                var bar: Int = 0
                var bar: Int = 0
            }
            class Bar {
                var bar: Int = 0
                var bar: Int = 0
            }
            """,
            macros:  [
                "DuplicateAll": DuplicateAll.self,
                "Duplicate": Duplicate.self,
            ]
        )
    }
}

Steps to Reproduce

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    MacrosIssues in the SwiftSyntaxMacro… modulesbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions