Skip to content

Commit d918a1d

Browse files
authored
Merge pull request #2141 from gohanlon/package-release/509
[509] Fix macro expansion diagnostic for multi-binding variable declarations in MacroSystem
2 parents 23be62c + 267691e commit d918a1d

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift

+4
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,10 @@ private class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
618618
}
619619

620620
override func visit(_ node: VariableDeclSyntax) -> DeclSyntax {
621+
guard !macroAttributes(attachedTo: DeclSyntax(node), ofType: AccessorMacro.Type.self).isEmpty else {
622+
return super.visit(node).cast(DeclSyntax.self)
623+
}
624+
621625
var node = super.visit(node).cast(VariableDeclSyntax.self)
622626
guard node.bindings.count == 1, let binding = node.bindings.first else {
623627
context.addDiagnostics(from: MacroApplicationError.accessorMacroOnVariableWithMultipleBindings, node: node)

Tests/SwiftSyntaxMacroExpansionTest/MacroSystemTests.swift

+53
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,19 @@ public struct DeclsFromStringsMacroNoAttrs: DeclarationMacro {
660660
}
661661
}
662662

663+
fileprivate struct NoOpMemberMacro: MemberMacro {
664+
public static func expansion<
665+
Declaration: DeclGroupSyntax,
666+
Context: MacroExpansionContext
667+
>(
668+
of node: AttributeSyntax,
669+
providingMembersOf declaration: Declaration,
670+
in context: Context
671+
) throws -> [DeclSyntax] {
672+
return []
673+
}
674+
}
675+
663676
// MARK: Tests
664677

665678
/// The set of test macros we use here.
@@ -1894,4 +1907,44 @@ final class MacroSystemTests: XCTestCase {
18941907
macros: ["decl": DeclMacro.self, "Peer": MyPeerMacro.self]
18951908
)
18961909
}
1910+
1911+
func testStructVariableDeclWithMultipleBindings() {
1912+
assertMacroExpansion(
1913+
"""
1914+
@Test
1915+
struct S {
1916+
var x: Int, y: Int
1917+
}
1918+
""",
1919+
expandedSource: """
1920+
struct S {
1921+
var x: Int, y: Int
1922+
}
1923+
""",
1924+
macros: ["Test": NoOpMemberMacro.self],
1925+
indentationWidth: indentationWidth
1926+
)
1927+
}
1928+
1929+
func testNestedStructVariableDeclWithMultipleBindings() {
1930+
assertMacroExpansion(
1931+
"""
1932+
@Test
1933+
struct Q {
1934+
struct R {
1935+
var i: Int, j: Int
1936+
}
1937+
}
1938+
""",
1939+
expandedSource: """
1940+
struct Q {
1941+
struct R {
1942+
var i: Int, j: Int
1943+
}
1944+
}
1945+
""",
1946+
macros: ["Test": NoOpMemberMacro.self],
1947+
indentationWidth: indentationWidth
1948+
)
1949+
}
18971950
}

0 commit comments

Comments
 (0)