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 member macro is applied to declaration that can’t have members #2206

Open
ahoppen opened this issue Sep 16, 2023 · 9 comments
Labels
good first issue Good for newcomers Macros Issues in the SwiftSyntaxMacro… modules

Comments

@ahoppen
Copy link
Collaborator

ahoppen commented Sep 16, 2023

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

@ahoppen ahoppen added good first issue Good for newcomers Macros Issues in the SwiftSyntaxMacro… modules labels Sep 16, 2023
@Rajveer100
Copy link

Rajveer100 commented Nov 19, 2023

@ahoppen
It would be great if you could further describe a little about swift-syntax and this issue as well?

If I remember correctly, macros were introduced this year (WWDC 2023) from Swift 5.9 and earlier it was always hidden behind the implementation of many Swift features?

I am digging down to learn more about it, let me know if there any specific resources w.r.t this issue.

@Rajveer100
Copy link

Rajveer100 commented Nov 21, 2023

@ahoppen
I am facing an issue with the following command:

> swift run --package-path CodeGeneration

Error:

Fetching https://github.com/apple/swift-argument-parser.git
Fetched https://github.com/apple/swift-argument-parser.git (2.24s)
Computing version for https://github.com/apple/swift-argument-parser.git
Computed https://github.com/apple/swift-argument-parser.git at 1.2.3 (0.61s)
Creating working copy for https://github.com/apple/swift-argument-parser.git
Working copy of https://github.com/apple/swift-argument-parser.git resolved at 1.2.3
Building for debugging...
[272/272] Linking generate-swift-syntax
Build complete! (17.20s)
dyld[21618]: Library not loaded: @rpath/libc++.1.dylib <----------
  Referenced from: <F5104EA3-23D6-34EC-B647-516182F5AFE8> ../swift-project/swift-syntax/CodeGeneration/.build/arm64-apple-macosx/debug/generate-swift-syntax
  Reason: tried: '/usr/lib/swift/libc++.1.dylib' (no such file, not in dyld cache), '/System/Volumes/Preboot/Cryptexes/OS/usr/lib/swift/libc++.1.dylib' (no such file), '/Users/rajveersingh/GitHub-OpenSource/swift-project/swift-syntax/CodeGeneration/.build/arm64-apple-macosx/debug/libc++.1.dylib' (no such file), '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift-5.5/macosx/libc++.1.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift-5.5/macosx/libc++.1.dylib' (no such file), '/usr/lib/swift/libc++.1.dylib' (no such file, not in dyld cache), '/System/Volumes/Preboot/Cryptexes/OS/usr/lib/swift/libc++.1.dylib' (no such file), '/Users/rajveersingh/GitHub-OpenSource/swift-project/swift-syntax/CodeGeneration/.build/arm64-apple-macosx/debug/libc++.1.dylib' (no such file), '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift-5.5/macosx/libc++.1.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift-5.5/macosx/libc++.1.dylib' (no such file)
zsh: abort      swift run --package-path CodeGeneration

This issue regarding libc++ and dyld has been there for a while, not sure what caused it.

[It's also occurring in JetBrains IDEs in Debug Mode (normal mode works), but this was fixed by adding an ENVIRONMENT VARIABLE for DYLD_LIBRARY_PATH.]
(Not related to this issue, but it's exactly the same)

I have also tried adding a global env to zsh and bash but doesn't resolve.

PS: I have added a Swift Forums post as well.

@ahoppen
Copy link
Collaborator Author

ahoppen commented Nov 22, 2023

Looks like you figured out the libc++ issue with @hamishknight on the forum thread 👍🏽

If you have any concrete questions regarding this issue, I’m happy to answer them, otherwise I think the issue description describes the issue fairly well. We have some general documentation of how swift-syntax works hosted on https://swiftpackageindex.com/apple/swift-syntax/main/documentation/swiftsyntax

@Rajveer100
Copy link

Rajveer100 commented Nov 23, 2023

If I understand correctly, we need to add a check inside the MemberMacro expansion to emit a diagnostic whenever we attach a member macro to a variable.

As an example if I created my custom macro that's only constrained for struct, enum, etc, we should emit an error for other cases appropriately to avoid unexpected behaviour. So we potentially add a failing test case first and then make the changes and check again for it to pass.

If my above intuition is right, could you highlight the appropriate file directories we are targeting currently? Regarding tests I suppose they all go in Tests?

@ahoppen
Copy link
Collaborator Author

ahoppen commented Nov 23, 2023

If my above intuition is right, could you highlight the appropriate file directories we are targeting currently? Regarding tests I suppose they all go in Tests?

The check should be in MacroSystem.

I would suggest that you add the above test case to your swift-syntax checkout. If you run it, it should fail. And then you can debug how to fix it.

@Rajveer100
Copy link

Rajveer100 commented Nov 24, 2023

In MacroExpansion, I see the case for MemberMacro in expandAttachedMacroWithoutCollapsing which is called via MacroSystem during expansion.

So to check for declaration, do we guard declarationNode and throw MacroExpansionError in case it's a VariableDeclSyntax?

Also, I have added the snippet test as you described under MemberMacroTests where I see the list of various functions and before making any change, no errors are thrown when running tests which I presume is the intended issue as this is the case that is not being caught, right?

@Rajveer100
Copy link

Rajveer100 commented Nov 28, 2023

Is the following snippet the right way to do the check, as at the moment many other tests also fail due to this addition:

guard declGroup.syntaxNodeType == VariableDeclSyntax.self else {
  throw MacroExpansionError.declarationNotIdentified
}

DeclGroupSyntax check is already present before this.

@Rajveer100
Copy link

@ahoppen
Just wanted to know if I am on the right track based on the above context.

@ahoppen
Copy link
Collaborator Author

ahoppen commented Dec 2, 2023

From your description, that seems like the right direction but it’s usually easier to tell if you open a PR and there’s code to look at.

Rajveer100 added a commit to Rajveer100/swift-syntax that referenced this issue Dec 2, 2023
…macro to declaration that can’t have members

Resolves Issue apple#2206
Rajveer100 added a commit to Rajveer100/swift-syntax that referenced this issue Dec 13, 2023
…macro to declaration that can’t have members

Resolves Issue apple#2206
Rajveer100 added a commit to Rajveer100/swift-syntax that referenced this issue Feb 28, 2024
…r macro to declaration that can’t have members

Resolves apple#2206
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers Macros Issues in the SwiftSyntaxMacro… modules
Projects
None yet
Development

No branches or pull requests

2 participants