@@ -125,36 +125,41 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
125125 override func visit( _ node: CodeBlockItemListSyntax ) -> CodeBlockItemListSyntax {
126126 var newItems : [ CodeBlockItemSyntax ] = [ ]
127127 for item in node {
128- // Expand declaration macros that were parsed as macro expansion
129- // expressions in this context.
130- if case let . expr( exprItem) = item. item,
131- let exprExpansion = exprItem. as ( MacroExpansionExprSyntax . self) ,
132- let macro = macroSystem. macros [ exprExpansion. macro. text]
128+ if let expansion = item. item. asProtocol ( FreestandingMacroExpansionSyntax . self) ,
129+ let macro = macroSystem. macros [ expansion. macro. text]
133130 {
134- do {
131+ func _expand ( expansion : FreestandingMacroExpansionSyntax ) throws {
135132 if let macro = macro as? CodeItemMacro . Type {
136133 let expandedItemList = try macro. expansion (
137- of: exprExpansion ,
134+ of: expansion ,
138135 in: context
139136 )
140137 newItems. append ( contentsOf: expandedItemList)
141138 } else if let macro = macro as? DeclarationMacro . Type {
142- let expandedItemList = try macro. expansion (
143- of: exprExpansion ,
139+ var expandedItemList = try macro. expansion (
140+ of: expansion ,
144141 in: context
145142 )
143+ if let declExpansion = expansion. as ( MacroExpansionDeclSyntax . self) {
144+ expandedItemList = expandedItemList. map {
145+ applyingAttrsModifiers ( to: $0, attributes: declExpansion. attributes, modifiers: declExpansion. modifiers)
146+ }
147+ }
146148 newItems. append (
147149 contentsOf: expandedItemList. map {
148150 CodeBlockItemSyntax ( item: . decl( $0) )
149151 }
150152 )
151153 } else if let macro = macro as? ExpressionMacro . Type {
152154 let expandedExpr = try macro. expansion (
153- of: exprExpansion ,
155+ of: expansion ,
154156 in: context
155157 )
156158 newItems. append ( CodeBlockItemSyntax ( item: . init( expandedExpr) ) )
157159 }
160+ }
161+ do {
162+ try _openExistential ( expansion, do: _expand)
158163 } catch {
159164 context. addDiagnostics ( from: error, node: node)
160165 }
@@ -189,10 +194,13 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
189194 let freestandingMacro = macro as? DeclarationMacro . Type
190195 {
191196 do {
192- let expandedList = try freestandingMacro. expansion (
197+ var expandedList = try freestandingMacro. expansion (
193198 of: declExpansion,
194199 in: context
195200 )
201+ expandedList = expandedList. map {
202+ applyingAttrsModifiers ( to: $0, attributes: declExpansion. attributes, modifiers: declExpansion. modifiers)
203+ }
196204
197205 newItems. append (
198206 contentsOf: expandedList. map { decl in
@@ -468,6 +476,40 @@ extension MacroApplication {
468476 }
469477}
470478
479+ // FIXME: This was copied from 'SwiftSyntaxMacroExpansion' module.
480+ // All expansion logics should be unified.
481+ private func applyingAttrsModifiers(
482+ to node: DeclSyntax ,
483+ attributes: AttributeListSyntax ? ,
484+ modifiers: ModifierListSyntax ?
485+ ) -> DeclSyntax {
486+ func _combine< C: SyntaxCollection > ( _ left: C , _ right: C ? ) -> C ? {
487+ guard let right = right else { return left }
488+ var elems : [ C . Element ] = [ ]
489+ elems. append ( contentsOf: left)
490+ elems. append ( contentsOf: right)
491+ return C ( elems)
492+ }
493+ var node = node
494+ if let attributes = attributes,
495+ let withAttrs = node. asProtocol ( WithAttributesSyntax . self)
496+ {
497+ node = withAttrs. with (
498+ \. attributes,
499+ _combine ( attributes, withAttrs. attributes)
500+ ) . as ( DeclSyntax . self) !
501+ }
502+ if let modifiers = modifiers,
503+ let withModifiers = node. asProtocol ( WithModifiersSyntax . self)
504+ {
505+ node = withModifiers. with (
506+ \. modifiers,
507+ _combine ( modifiers, withModifiers. modifiers)
508+ ) . as ( DeclSyntax . self) !
509+ }
510+ return node
511+ }
512+
471513extension SyntaxProtocol {
472514 /// Expand all uses of the given set of macros within this syntax
473515 /// node.
0 commit comments