-
Notifications
You must be signed in to change notification settings - Fork 501
Fix(MacroSystem): Validate MemberMacro application on non-group declarations (Issue #2206) #3257
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -623,6 +623,7 @@ let diagnosticDomain: String = "SwiftSyntaxMacroExpansion" | |
| private enum MacroApplicationError: DiagnosticMessage, Error { | ||
| case accessorMacroOnVariableWithMultipleBindings | ||
| case peerMacroOnVariableWithMultipleBindings | ||
| case memberMacroOnInvalidDecl(macroName: String) | ||
| case malformedAccessor | ||
|
|
||
| var diagnosticID: MessageID { | ||
|
|
@@ -637,6 +638,8 @@ private enum MacroApplicationError: DiagnosticMessage, Error { | |
| return "accessor macro can only be applied to a single variable" | ||
| case .peerMacroOnVariableWithMultipleBindings: | ||
| return "peer macro can only be applied to a single variable" | ||
| case .memberMacroOnInvalidDecl(let macroName): | ||
| return "macro '\(macroName)' can only be applied to a struct, enum, class, extension, or actor" | ||
| case .malformedAccessor: | ||
| return """ | ||
| macro returned a malformed accessor. Accessors should start with an introducer like 'get' or 'set'. | ||
|
|
@@ -702,6 +705,18 @@ private class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter { | |
| let attributedNode = node.asProtocol(WithAttributesSyntax.self), | ||
| !attributedNode.attributes.isEmpty | ||
| { | ||
| // Check if there are any member macros generated on a non-group decl. | ||
| if !(node.isProtocol(DeclGroupSyntax.self) || node.is(ExtensionDeclSyntax.self)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An |
||
| let memberMacros = self.macroAttributes(attachedTo: declSyntax, ofType: MemberMacro.Type.self) | ||
| for (attribute, _, _) in memberMacros { | ||
| let macroName = attribute.attributeName.trimmedDescription | ||
| contextGenerator(Syntax(node)).addDiagnostics( | ||
| from: MacroApplicationError.memberMacroOnInvalidDecl(macroName: macroName), | ||
| node: attribute | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| // Apply body and preamble macros. | ||
| if let nodeWithBody = node.asProtocol(WithOptionalCodeBlockSyntax.self), | ||
| let declNodeWithBody = nodeWithBody as? any DeclSyntaxProtocol & WithOptionalCodeBlockSyntax | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
|
|
||
| import SwiftDiagnostics | ||
| import SwiftSyntax | ||
| import SwiftSyntaxMacroExpansion | ||
| import SwiftSyntaxMacros | ||
| import SwiftSyntaxMacrosTestSupport | ||
| import XCTest | ||
|
|
||
| private struct NoOpMemberMacro: MemberMacro { | ||
| static func expansion( | ||
| of node: AttributeSyntax, | ||
| providingMembersOf declaration: some DeclGroupSyntax, | ||
| conformingTo protocols: [TypeSyntax], | ||
| in context: some MacroExpansionContext | ||
| ) throws -> [DeclSyntax] { | ||
| return [] | ||
| } | ||
| } | ||
|
|
||
| final class Issue2206Tests: XCTestCase { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be folded into the existing tests we have for member macros in |
||
| private let indentationWidth: Trivia = .spaces(2) | ||
|
|
||
| func testMemberMacroOnVariable() { | ||
| // Issue #2206: assertMacroExpansion should emit an error if member macro is applied to declaration that can’t have members | ||
| // Currently this is expected to FAIL because the diagnostic is swallowed. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Outdated comment? |
||
| assertMacroExpansion( | ||
| """ | ||
| @Test | ||
| var x: Int | ||
| """, | ||
| expandedSource: """ | ||
| var x: Int | ||
| """, | ||
| diagnostics: [ | ||
| DiagnosticSpec(message: "macro 'Test' can only be applied to a struct, enum, class, extension, or actor", line: 1, column: 1) | ||
| ], | ||
| macros: ["Test": NoOpMemberMacro.self], | ||
| indentationWidth: indentationWidth | ||
| ) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say
member macrohere for clarity, also I'm not sure it's worth listing every type decl kind (note you missedprotocol), maybe it would be better just to saytype declaration or extensioninstead?