@@ -215,7 +215,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
215
215
definition: Macro . Type ,
216
216
macroRole: MacroRole ,
217
217
attributeNode: AttributeSyntax ,
218
- declarationNode: DeclSyntax ,
218
+ declarationNode: some SyntaxProtocol ,
219
219
parentDeclNode: DeclSyntax ? ,
220
220
extendedType: TypeSyntax ? ,
221
221
conformanceList: InheritedTypeListSyntax ? ,
@@ -225,6 +225,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
225
225
do {
226
226
switch ( definition, macroRole) {
227
227
case ( let attachedMacro as AccessorMacro . Type , . accessor) :
228
+ let declarationNode = declarationNode. cast ( DeclSyntax . self)
228
229
let accessors = try attachedMacro. expansion (
229
230
of: attributeNode,
230
231
providingAccessorsOf: declarationNode,
@@ -235,6 +236,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
235
236
}
236
237
237
238
case ( let attachedMacro as MemberAttributeMacro . Type , . memberAttribute) :
239
+ let declarationNode = declarationNode. cast ( DeclSyntax . self)
238
240
guard
239
241
let parentDeclGroup = parentDeclNode? . asProtocol ( DeclGroupSyntax . self)
240
242
else {
@@ -274,6 +276,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
274
276
}
275
277
276
278
case ( let attachedMacro as PeerMacro . Type , . peer) :
279
+ let declarationNode = declarationNode. cast ( DeclSyntax . self)
277
280
let peers = try attachedMacro. expansion (
278
281
of: attributeNode,
279
282
providingPeersOf: declarationNode,
@@ -335,19 +338,25 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
335
338
}
336
339
337
340
case ( let attachedMacro as BodyMacro . Type , . body) :
338
- guard
339
- let declToPass = Syntax ( declarationNode) . asProtocol ( SyntaxProtocol . self)
340
- as? ( DeclSyntaxProtocol & WithOptionalCodeBlockSyntax )
341
- else {
341
+ let body : [ CodeBlockItemSyntax ]
342
+ if let closureSyntax = declarationNode. as ( ClosureExprSyntax . self) {
343
+ body = try attachedMacro. expansion (
344
+ of: attributeNode,
345
+ providingBodyFor: closureSyntax,
346
+ in: context
347
+ )
348
+ } else if let declToPass = Syntax ( declarationNode) . asProtocol ( SyntaxProtocol . self)
349
+ as? ( DeclSyntaxProtocol & WithOptionalCodeBlockSyntax ) {
350
+ body = try attachedMacro. expansion (
351
+ of: attributeNode,
352
+ providingBodyFor: declToPass,
353
+ in: context
354
+ )
355
+ } else {
342
356
// Compiler error: declaration must have a body.
343
357
throw MacroExpansionError . declarationHasNoBody
344
358
}
345
359
346
- let body = try attachedMacro. expansion (
347
- of: attributeNode,
348
- providingBodyFor: declToPass,
349
- in: context
350
- )
351
360
return body. map {
352
361
$0. formattedExpansion ( definition. formatMode, indentationWidth: indentationWidth)
353
362
}
@@ -380,7 +389,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
380
389
definition: Macro . Type ,
381
390
macroRole: MacroRole ,
382
391
attributeNode: AttributeSyntax ,
383
- declarationNode: DeclSyntax ,
392
+ declarationNode: some SyntaxProtocol ,
384
393
parentDeclNode: DeclSyntax ? ,
385
394
extendedType: TypeSyntax ? ,
386
395
conformanceList: InheritedTypeListSyntax ? ,
0 commit comments