Skip to content

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

Open
@ahoppen

Description

@ahoppen

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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    MacrosIssues in the SwiftSyntaxMacro… modulesgood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions