Skip to content

assertMacroExpansion() gives different diagnostics than using a macro when attached to multiple variables #3320

@fizker

Description

@fizker

Description

This is reproduced with [email protected].

When testing macros attached to properties where there are multiple properties on the same var/let, assertMacroExpansion() gives an error before executing the macro. But when using the macro outside of assertMacroExpansion(), the macro is executed similarly to when there is only one property.


Testing macro
Result of testing macro

Using macro outside test
Result of using macro outside test

Steps to Reproduce

A minimal project showing the issue is here: https://github.com/fizker/swift-syntax-peer-macro-issue

To summarize:

We start with the following macro:

public struct MyMacro: PeerMacro {
	public static func expansion(
		of node: AttributeSyntax,
		providingPeersOf declaration: some DeclSyntaxProtocol,
		in context: some MacroExpansionContext,
	) throws -> [DeclSyntax] {
		return []
	}
}

We then use the macro

struct Foo {
  @MyMacro
  var a: Int, b: Int
}

If this code is placed in regularly executing Swift, it runs without issues and the return-statement is hit twice, for both a and b. But if it executed through assertMacroExpansion(), a diagnostic is raised stating that peer macro can only be applied to a single variable, and the macro code is never executed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions