@@ -91,41 +91,38 @@ public func expandFreestandingMacro(
9191 in context: some MacroExpansionContext
9292) -> String ? {
9393 do {
94- func _expand( node: some FreestandingMacroExpansionSyntax ) throws -> String {
95- let expandedSyntax : Syntax
96- switch ( macroRole, definition) {
97- case ( . expression, let exprMacroDef as ExpressionMacro . Type ) :
98- expandedSyntax = try Syntax ( exprMacroDef. expansion ( of: node, in: context) )
99-
100- case ( . declaration, let declMacroDef as DeclarationMacro . Type ) :
101- var rewritten = try declMacroDef. expansion ( of: node, in: context)
102- // Copy attributes and modifiers to the generated decls.
103- if let expansionDecl = node. as ( MacroExpansionDeclSyntax . self) {
104- let attributes = declMacroDef. propagateFreestandingMacroAttributes ? expansionDecl. attributes : nil
105- let modifiers = declMacroDef. propagateFreestandingMacroModifiers ? expansionDecl. modifiers : nil
106- rewritten = rewritten. map {
107- $0. applying ( attributes: attributes, modifiers: modifiers)
108- }
94+ let expandedSyntax : Syntax
95+ switch ( macroRole, definition) {
96+ case ( . expression, let exprMacroDef as ExpressionMacro . Type ) :
97+ expandedSyntax = try Syntax ( exprMacroDef. expansion ( of: node, in: context) )
98+
99+ case ( . declaration, let declMacroDef as DeclarationMacro . Type ) :
100+ var rewritten = try declMacroDef. expansion ( of: node, in: context)
101+ // Copy attributes and modifiers to the generated decls.
102+ if let expansionDecl = node. as ( MacroExpansionDeclSyntax . self) {
103+ let attributes = declMacroDef. propagateFreestandingMacroAttributes ? expansionDecl. attributes : nil
104+ let modifiers = declMacroDef. propagateFreestandingMacroModifiers ? expansionDecl. modifiers : nil
105+ rewritten = rewritten. map {
106+ $0. applying ( attributes: attributes, modifiers: modifiers)
109107 }
110- expandedSyntax = Syntax (
111- CodeBlockItemListSyntax (
112- rewritten . map {
113- CodeBlockItemSyntax ( item : . decl ( $0 ) )
114- }
115- )
108+ }
109+ expandedSyntax = Syntax (
110+ CodeBlockItemListSyntax (
111+ rewritten . map {
112+ CodeBlockItemSyntax ( item : . decl ( $0 ) )
113+ }
116114 )
115+ )
117116
118- case ( . codeItem, let codeItemMacroDef as CodeItemMacro . Type ) :
119- let rewritten = try codeItemMacroDef. expansion ( of: node, in: context)
120- expandedSyntax = Syntax ( CodeBlockItemListSyntax ( rewritten) )
117+ case ( . codeItem, let codeItemMacroDef as CodeItemMacro . Type ) :
118+ let rewritten = try codeItemMacroDef. expansion ( of: node, in: context)
119+ expandedSyntax = Syntax ( CodeBlockItemListSyntax ( rewritten) )
121120
122- case ( . accessor, _) , ( . memberAttribute, _) , ( . member, _) , ( . peer, _) , ( . conformance, _) , ( . extension, _) , ( . expression, _) , ( . declaration, _) ,
123- ( . codeItem, _) :
124- throw MacroExpansionError . unmatchedMacroRole ( definition, macroRole)
125- }
126- return expandedSyntax. formattedExpansion ( definition. formatMode)
121+ case ( . accessor, _) , ( . memberAttribute, _) , ( . member, _) , ( . peer, _) , ( . conformance, _) , ( . extension, _) , ( . expression, _) , ( . declaration, _) ,
122+ ( . codeItem, _) :
123+ throw MacroExpansionError . unmatchedMacroRole ( definition, macroRole)
127124 }
128- return try _openExistential ( node , do : _expand )
125+ return expandedSyntax . formattedExpansion ( definition . formatMode )
129126 } catch {
130127 context. addDiagnostics ( from: error, node: node)
131128 return nil
@@ -208,22 +205,11 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
208205 throw MacroExpansionError . parentDeclGroupNil
209206 }
210207
211- // Local function to expand a member attribute macro once we've opened up
212- // the existential.
213- func expandMemberAttributeMacro(
214- _ node: some DeclGroupSyntax
215- ) throws -> [ AttributeSyntax ] {
216- return try attachedMacro. expansion (
217- of: attributeNode,
218- attachedTo: node,
219- providingAttributesFor: declarationNode,
220- in: context
221- )
222- }
223-
224- let attributes = try _openExistential (
225- parentDeclGroup,
226- do: expandMemberAttributeMacro
208+ let attributes = try attachedMacro. expansion (
209+ of: attributeNode,
210+ attachedTo: parentDeclGroup,
211+ providingAttributesFor: declarationNode,
212+ in: context
227213 )
228214
229215 // Form a buffer containing an attribute list to return to the caller.
@@ -238,19 +224,11 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
238224 throw MacroExpansionError . declarationNotDeclGroup
239225 }
240226
241- // Local function to expand a member macro once we've opened up
242- // the existential.
243- func expandMemberMacro(
244- _ node: some DeclGroupSyntax
245- ) throws -> [ DeclSyntax ] {
246- return try attachedMacro. expansion (
247- of: attributeNode,
248- providingMembersOf: node,
249- in: context
250- )
251- }
252-
253- let members = try _openExistential ( declGroup, do: expandMemberMacro)
227+ let members = try attachedMacro. expansion (
228+ of: attributeNode,
229+ providingMembersOf: declGroup,
230+ in: context
231+ )
254232
255233 // Form a buffer of member declarations to return to the caller.
256234 return members. map { $0. formattedExpansion ( definition. formatMode) }
@@ -279,23 +257,12 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
279257
280258 let protocols = conformanceList? . map ( \. typeName) ?? [ ]
281259
282- // Local function to expand an extension macro once we've opened up
283- // the existential.
284- func expandExtensionMacro(
285- _ node: some DeclGroupSyntax
286- ) throws -> [ ExtensionDeclSyntax ] {
287- return try attachedMacro. expansion (
288- of: attributeNode,
289- attachedTo: node,
290- providingExtensionsOf: extendedType,
291- conformingTo: protocols,
292- in: context
293- )
294- }
295-
296- let extensions = try _openExistential (
297- declGroup,
298- do: expandExtensionMacro
260+ let extensions = try attachedMacro. expansion (
261+ of: attributeNode,
262+ attachedTo: declGroup,
263+ providingExtensionsOf: extendedType,
264+ conformingTo: protocols,
265+ in: context
299266 )
300267
301268 // Form a buffer of peer declarations to return to the caller.
0 commit comments