Skip to content

Commit 2b205bc

Browse files
committed
[Macro expansion] Detach arguments to macros consistently
1 parent 9481c6d commit 2b205bc

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

Sources/SwiftSyntaxMacros/MacroSystem.swift

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
134134
do {
135135
if let macro = macro as? CodeItemMacro.Type {
136136
let expandedItemList = try macro.expansion(
137-
of: exprExpansion,
137+
of: exprExpansion.detach(in: context),
138138
in: context
139139
)
140140
newItems.append(contentsOf: expandedItemList)
141141
} else if let macro = macro as? DeclarationMacro.Type {
142142
let expandedItemList = try macro.expansion(
143-
of: exprExpansion,
143+
of: exprExpansion.detach(in: context),
144144
in: context
145145
)
146146
newItems.append(
@@ -150,7 +150,7 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
150150
)
151151
} else if let macro = macro as? ExpressionMacro.Type {
152152
let expandedExpr = try macro.expansion(
153-
of: exprExpansion,
153+
of: exprExpansion.detach(in: context),
154154
in: context
155155
)
156156
newItems.append(CodeBlockItemSyntax(item: .init(expandedExpr)))
@@ -190,7 +190,7 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
190190
{
191191
do {
192192
let expandedList = try freestandingMacro.expansion(
193-
of: declExpansion,
193+
of: declExpansion.detach(in: context),
194194
in: context
195195
)
196196

@@ -297,8 +297,8 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
297297
for (accessorAttr, accessorMacro) in accessorMacroAttributes {
298298
do {
299299
let newAccessors = try accessorMacro.expansion(
300-
of: accessorAttr,
301-
providingAccessorsOf: visitedNode,
300+
of: accessorAttr.detach(in: context),
301+
providingAccessorsOf: visitedNode.detach(in: context),
302302
in: context
303303
)
304304

@@ -367,7 +367,11 @@ extension MacroApplication {
367367
let macroAttributes = getMacroAttributes(attachedTo: decl, ofType: PeerMacro.Type.self)
368368
for (attribute, peerMacro) in macroAttributes {
369369
do {
370-
let newPeers = try peerMacro.expansion(of: attribute, providingPeersOf: decl, in: context)
370+
let newPeers = try peerMacro.expansion(
371+
of: attribute.detach(in: context),
372+
providingPeersOf: decl.detach(in: context),
373+
in: context
374+
)
371375
peers.append(contentsOf: newPeers)
372376
} catch {
373377
context.addDiagnostics(from: error, node: attribute)
@@ -388,8 +392,8 @@ extension MacroApplication {
388392
do {
389393
try newMembers.append(
390394
contentsOf: memberMacro.expansion(
391-
of: attribute,
392-
providingMembersOf: decl,
395+
of: attribute.detach(in: context),
396+
providingMembersOf: decl.detach(in: context),
393397
in: context
394398
)
395399
)
@@ -417,9 +421,9 @@ extension MacroApplication {
417421
#if false
418422
_openExistential(decl) { d in
419423
return try! macro.expansion(
420-
of: attribute,
421-
attachedTo: d,
422-
annotating: member,
424+
of: attribute.detach(in: context),
425+
attachedTo: d.detach(in: context),
426+
annotating: member.detach(in: context),
423427
in: context
424428
)
425429
}
@@ -444,9 +448,9 @@ extension MacroApplication {
444448

445449
func expand<Decl: DeclGroupSyntax>(_ decl: Decl) throws -> [AttributeSyntax] {
446450
return try attributeMacro.expansion(
447-
of: attribute,
448-
attachedTo: decl,
449-
providingAttributesFor: member.decl,
451+
of: attribute.detach(in: context),
452+
attachedTo: decl.detach(in: context),
453+
providingAttributesFor: member.decl.detach(in: context),
450454
in: context
451455
)
452456
}

Sources/SwiftSyntaxMacros/Syntax+MacroEvaluation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import SwiftSyntax
1616
extension SyntaxProtocol {
1717
/// Detach the current node and inform the macro expansion context,
1818
/// if it needs to know.
19-
fileprivate func detach(in context: MacroExpansionContext) -> Self {
19+
func detach(in context: MacroExpansionContext) -> Self {
2020
if let basicContext = context as? BasicMacroExpansionContext {
2121
return basicContext.detach(self)
2222
}

Tests/SwiftSyntaxMacrosTest/MacroSystemTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ public struct AddCompletionHandler: PeerMacro {
392392
return true
393393
}
394394

395-
return attribute != node
395+
return attribute.attributeName.description != node.attributeName.description
396396
} ?? []
397397
)
398398

0 commit comments

Comments
 (0)