Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assertMacroExpansion should emit an error if macro is applied to node that is not a variable #2207

Open
ahoppen opened this issue Sep 16, 2023 · 2 comments · May be fixed by #2624
Open

assertMacroExpansion should emit an error if macro is applied to node that is not a variable #2207

ahoppen opened this issue Sep 16, 2023 · 2 comments · May be fixed by #2624
Labels
Macros Issues in the SwiftSyntaxMacro… modules

Comments

@ahoppen
Copy link
Member

ahoppen commented Sep 16, 2023

When applying an accessor macro to e.g. a struct, assertMacroExpansion will currently happily swallow the attribute. It should, however, emit an error that accessor macros can’t be applied to structs. Ie. the following test case should emit an error.

func testAccessorOnStruct() {
  struct TestMacro: AccessorMacro {
    static func expansion(
      of node: AttributeSyntax,
      providingAccessorsOf declaration: some DeclSyntaxProtocol,
      in context: some MacroExpansionContext
    ) throws -> [AccessorDeclSyntax] {
      return []
    }
  }

  assertMacroExpansion(
    "@Test struct Foo {}",
    expandedSource: "struct Foo {}",
    macros: ["Test": TestMacro.self]
  )
}

rdar://115562563

@ahoppen ahoppen added the Macros Issues in the SwiftSyntaxMacro… modules label Sep 16, 2023
@RayZhao1998
Copy link

I want to work on this. But I found in the testTypeWrapperTransform, CustomTypeWrapperMacro is also an AccessorMacro but it's applied to struct.

@RayZhao1998
Copy link

Also I've made a draft PR and I want to know whether my solution is correct. It only fails on this test testTypeWrapperTransform now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Macros Issues in the SwiftSyntaxMacro… modules
Projects
None yet
2 participants