@@ -167,17 +167,25 @@ func evaluateMacro(
167
167
}
168
168
169
169
macroName = parentExpansion. macro. text
170
- evaluatedSyntax = Syntax (
171
- try exprMacro. expansion (
170
+
171
+ func expandExpressionMacro< Node: FreestandingMacroExpansionSyntax > (
172
+ _ node: Node
173
+ ) throws -> ExprSyntax {
174
+ return try exprMacro. expansion (
172
175
of: sourceManager. detach (
173
- parentExpansion , in: context,
176
+ node , in: context,
174
177
foldingWith: OperatorTable . standardOperators
175
178
) ,
176
179
in: context
177
180
)
181
+ }
182
+
183
+ evaluatedSyntax = Syntax (
184
+ try _openExistential ( parentExpansion, do: expandExpressionMacro)
178
185
)
179
186
180
- // Handle expression macro. The resulting decls are wrapped in a `CodeBlockItemListSyntax`.
187
+ // Handle declaration macro. The resulting decls are wrapped in a
188
+ // `CodeBlockItemListSyntax`.
181
189
case let declMacro as DeclarationMacro . Type :
182
190
guard let parentExpansion = parentSyntax. as ( MacroExpansionDeclSyntax . self) else {
183
191
print ( " not on a macro expansion node: \( token. recursiveDescription) " )
@@ -354,11 +362,23 @@ func expandAttachedMacro(
354
362
return 1
355
363
}
356
364
357
- let attributes = try attachedMacro. expansion (
358
- of: context. detach ( customAttrNode) ,
359
- attachedTo: context. detach ( parentDeclGroup) ,
360
- providingAttributesFor: context. detach ( declarationNode) ,
361
- in: context
365
+ // Local function to expand a member atribute macro once we've opened up
366
+ // the existential.
367
+ func expandMemberAttributeMacro< Node: DeclGroupSyntax > (
368
+ _ node: Node
369
+ ) throws -> [ AttributeSyntax ] {
370
+ return try attachedMacro. expansion (
371
+ of: context. detach ( customAttrNode) ,
372
+ attachedTo: sourceManager. detach ( node, in: context) ,
373
+ providingAttributesFor: sourceManager. detach (
374
+ declarationNode, in: context
375
+ ) ,
376
+ in: context
377
+ )
378
+ }
379
+
380
+ let attributes = try _openExistential (
381
+ parentDeclGroup, do: expandMemberAttributeMacro
362
382
)
363
383
364
384
// Form a buffer containing an attribute list to return to the caller.
@@ -372,11 +392,19 @@ func expandAttachedMacro(
372
392
return 1
373
393
}
374
394
375
- let members = try attachedMacro. expansion (
376
- of: context. detach ( customAttrNode) ,
377
- providingMembersOf: context. detach ( declGroup) ,
378
- in: context
379
- )
395
+ // Local function to expand a member macro once we've opened up
396
+ // the existential.
397
+ func expandMemberMacro< Node: DeclGroupSyntax > (
398
+ _ node: Node
399
+ ) throws -> [ DeclSyntax ] {
400
+ return try attachedMacro. expansion (
401
+ of: sourceManager. detach ( customAttrNode, in: context) ,
402
+ providingMembersOf: sourceManager. detach ( node, in: context) ,
403
+ in: context
404
+ )
405
+ }
406
+
407
+ let members = try _openExistential ( declGroup, do: expandMemberMacro)
380
408
381
409
// Form a buffer of member declarations to return to the caller.
382
410
evaluatedSyntaxStr = members. map {
0 commit comments