swiftlang / swiftlang/swift-syntax
`assertMacroExpansion` should emit an error if member macro is applied to declaration that can’t have members
- Dominant language
- Swift
- Stars
- 3.7k
- Forks
- 553
- Avg merge
- 5d 13h
- Merged PRs (30d)
- 16
Description
When applying an attached member macro to e.g. a variable, `assertMacroExpansion` will currently happily swallow the attribute. It should, however, emit an error that member macros can’t be applied to variables (specifically, if the declaration is not a `DeclGroupSyntax`). Ie. the following test case should emit an error.
```swift
func testMemberMacroOnVar() {
struct TestMacro: MemberMacro {
static func expansion(
of node: AttributeSyntax,
providingMembersOf declaration: some DeclGroupSyntax,
conformingTo protocols: [TypeSyntax],
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
return []
}
}
assertMacroExpansion(
"""
@Test var x: Int
""",
expandedSource: """
var x: Int
""",
macros: [
"Test": TestMacro.self
]
)
}
```
rdar://115562663
Contributor guide
Assessment
This issue has not been assessed yet.