swiftlang / swiftlang/swift-syntax

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

Open
#3,320 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
3.7k
Forks
553
Avg merge
5d 13h
Merged PRs (30d)
16

Description

### Description

This is reproduced with swift-syntax@603.0.0.

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:

```swift
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
```swift
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.