Fix(MacroSystem): Validate MemberMacro application on non-group declarations (Issue #2206) #3257
+56
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix(MacroSystem): Validate MemberMacro application on non-group declarations (Issue #2206)
Description
This PR fixes an issue where
MemberMacroattributes applied to invalid declarations (such asvarproperties or functions) were silently ignored by theMacroApplicationwalker.Before this change, the following code would compile successfully with 0 diagnostics, even though
@Testis aMemberMacro:After this change, it correctly produces the expected diagnostic:
error: macro 'Test' can only be applied to a struct, enum, class, extension, or actorFixes: #2206
Motivation
The
visit(_ : MemberBlockSyntax)method handles member macros correctly for container types. However, when aMemberMacrois applied to a standalone declaration (like a VariableDecl), the walker visits it viavisitAnyor specific node visitors which previously lacked logic to check for member macro attributes. This resulted in silent failures where the user would expect either an expansion or an error.Detailed Design
MacroApplicationError.memberMacroOnInvalidDecl(macroName: String).MacroApplication.visitAny(_:)to inspectDeclSyntaxnodes.MemberMacrobut does not conform toDeclGroupSyntax(and is not anExtensionDeclSyntax), a diagnostic is now added to the context.Verification
Tests/SwiftSyntaxMacroExpansionTest/Issue2206Tests.swift.SwiftSyntaxMacroExpansion.Checklist