Skip to content

Commit b8e3122

Browse files
committed
Fix macro expansion diagnostic for multi-binding variable declarations in MacroSystem
When there are no macro attributes attached to the visited `VariableDeclSyntax` node, proceed without performing any additional checks or transformations. - Addressed the erroneous triggering of `MacroApplicationError.accessorMacroOnVariableWithMultipleBindings` diagnostic for nested multi-binding variable declarations. - Added corresponding tests to validate the fix. - Introduce fileprivate `NoOpMemberMacro`, extracted from `testCommentAroundeAttachedMacro` and used in 2 new tests. Fixes #2133 rdar://114836887
1 parent 71b7378 commit b8e3122

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,10 @@ private class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
606606
}
607607

608608
override func visit(_ node: VariableDeclSyntax) -> DeclSyntax {
609+
guard !macroAttributes(attachedTo: DeclSyntax(node), ofType: AccessorMacro.Type.self).isEmpty else {
610+
return super.visit(node).cast(DeclSyntax.self)
611+
}
612+
609613
var node = super.visit(node).cast(VariableDeclSyntax.self)
610614
guard node.bindings.count == 1, let binding = node.bindings.first else {
611615
context.addDiagnostics(from: MacroApplicationError.accessorMacroOnVariableWithMultipleBindings, node: node)

Tests/SwiftSyntaxMacroExpansionTest/MemberMacroTests.swift

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ import SwiftSyntaxMacroExpansion
2424
import SwiftSyntaxMacrosTestSupport
2525
import XCTest
2626

27+
fileprivate struct NoOpMemberMacro: MemberMacro {
28+
static func expansion(
29+
of node: AttributeSyntax,
30+
providingMembersOf declaration: some DeclGroupSyntax,
31+
in context: some MacroExpansionContext
32+
) throws -> [DeclSyntax] {
33+
return []
34+
}
35+
}
36+
2737
final class MemberMacroTests: XCTestCase {
2838
private let indentationWidth: Trivia = .spaces(2)
2939

@@ -103,16 +113,6 @@ final class MemberMacroTests: XCTestCase {
103113
}
104114

105115
func testCommentAroundeAttachedMacro() {
106-
struct TestMacro: MemberMacro {
107-
static func expansion(
108-
of node: AttributeSyntax,
109-
providingMembersOf declaration: some DeclGroupSyntax,
110-
in context: some MacroExpansionContext
111-
) throws -> [DeclSyntax] {
112-
return []
113-
}
114-
}
115-
116116
assertMacroExpansion(
117117
"""
118118
/// Some doc comment
@@ -128,7 +128,47 @@ final class MemberMacroTests: XCTestCase {
128128
var value: Int
129129
}
130130
""",
131-
macros: ["Test": TestMacro.self],
131+
macros: ["Test": NoOpMemberMacro.self],
132+
indentationWidth: indentationWidth
133+
)
134+
}
135+
136+
func testStructVariableDeclWithMultipleBindings() {
137+
assertMacroExpansion(
138+
"""
139+
@Test
140+
struct S {
141+
var x: Int, y: Int
142+
}
143+
""",
144+
expandedSource: """
145+
struct S {
146+
var x: Int, y: Int
147+
}
148+
""",
149+
macros: ["Test": NoOpMemberMacro.self],
150+
indentationWidth: indentationWidth
151+
)
152+
}
153+
154+
func testNestedStructVariableDeclWithMultipleBindings() {
155+
assertMacroExpansion(
156+
"""
157+
@Test
158+
struct Q {
159+
struct R {
160+
var i: Int, j: Int
161+
}
162+
}
163+
""",
164+
expandedSource: """
165+
struct Q {
166+
struct R {
167+
var i: Int, j: Int
168+
}
169+
}
170+
""",
171+
macros: ["Test": NoOpMemberMacro.self],
132172
indentationWidth: indentationWidth
133173
)
134174
}

0 commit comments

Comments
 (0)