@@ -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,26 @@ 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
+ {
351
+ body = try attachedMacro. expansion (
352
+ of: attributeNode,
353
+ providingBodyFor: declToPass,
354
+ in: context
355
+ )
356
+ } else {
342
357
// Compiler error: declaration must have a body.
343
358
throw MacroExpansionError . declarationHasNoBody
344
359
}
345
360
346
- let body = try attachedMacro. expansion (
347
- of: attributeNode,
348
- providingBodyFor: declToPass,
349
- in: context
350
- )
351
361
return body. map {
352
362
$0. formattedExpansion ( definition. formatMode, indentationWidth: indentationWidth)
353
363
}
@@ -380,7 +390,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
380
390
definition: Macro . Type ,
381
391
macroRole: MacroRole ,
382
392
attributeNode: AttributeSyntax ,
383
- declarationNode: DeclSyntax ,
393
+ declarationNode: some SyntaxProtocol ,
384
394
parentDeclNode: DeclSyntax ? ,
385
395
extendedType: TypeSyntax ? ,
386
396
conformanceList: InheritedTypeListSyntax ? ,
0 commit comments