Open
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).
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