swiftlang / swiftlang/swift-syntax

`assertMacroExpansion` should emit an error if member macro is applied to declaration that can’t have members

Open
#2,206 9 comments 0 reactions 0 assignees View on GitHub
good first issue Macros
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

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.