swiftlang / swiftlang/swift-syntax
SyntaxProtocol.expand() does not expand peer macros introduced by other macros
- Dominant language
- Swift
- Stars
- 3.7k
- Forks
- 553
- Avg merge
- 5d 13h
- Merged PRs (30d)
- 16
Description
### 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).
```swift
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_
Contributor guide
Assessment
This issue has not been assessed yet.