@@ -669,6 +669,7 @@ private class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
669
669
/// Store expanded extension while visiting member decls. This should be
670
670
/// added to top-level 'CodeBlockItemList'.
671
671
var extensions : [ CodeBlockItemSyntax ] = [ ]
672
+ var expandedAttributes : [ AttributeSyntax ] = [ ]
672
673
673
674
init (
674
675
macroSystem: MacroSystem ,
@@ -720,16 +721,21 @@ private class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
720
721
721
722
let attributesToRemove = self . macroAttributes ( attachedTo: visitedNode)
722
723
attributesToRemove. forEach { ( attribute, spec) in
723
- if let _ = spec. type as? AccessorMacro . Type ,
724
- !declSyntax. is ( VariableDeclSyntax . self) && !declSyntax. is ( SubscriptDeclSyntax . self)
725
- {
726
- contextGenerator ( node) . addDiagnostics (
727
- from: MacroApplicationError . accessorMacroNotOnVariableOrSubscript,
728
- node: declSyntax
729
- )
724
+ if let index = self . expandedAttributes. firstIndex ( where: { expandedAttribute in
725
+ expandedAttribute. id. indexInTree == attribute. id. indexInTree
726
+ } ) {
727
+ self . expandedAttributes. remove ( at: index)
728
+ } else {
729
+ if let _ = spec. type as? AccessorMacro . Type ,
730
+ !declSyntax. is ( VariableDeclSyntax . self) && !declSyntax. is ( SubscriptDeclSyntax . self)
731
+ {
732
+ contextGenerator ( node) . addDiagnostics (
733
+ from: MacroApplicationError . accessorMacroNotOnVariableOrSubscript,
734
+ node: declSyntax
735
+ )
736
+ }
730
737
}
731
738
}
732
-
733
739
return AttributeRemover ( removingWhere: { attributesToRemove. map ( \. attributeNode) . contains ( $0) } ) . rewrite (
734
740
visitedNode
735
741
)
@@ -1038,6 +1044,7 @@ extension MacroApplication {
1038
1044
macroAttribute. conformanceList
1039
1045
) {
1040
1046
result += expanded
1047
+ self . expandedAttributes. append ( macroAttribute. attributeNode)
1041
1048
}
1042
1049
} catch {
1043
1050
contextGenerator ( Syntax ( decl) ) . addDiagnostics ( from: error, node: macroAttribute. attributeNode)
@@ -1185,6 +1192,7 @@ extension MacroApplication {
1185
1192
from: newAccessors,
1186
1193
indentationWidth: self . indentationWidth
1187
1194
)
1195
+ self . expandedAttributes. append ( macro. attributeNode)
1188
1196
}
1189
1197
} else if let newAccessors = try expandAccessorMacroWithoutExistingAccessors (
1190
1198
definition: macro. definition,
@@ -1207,11 +1215,13 @@ extension MacroApplication {
1207
1215
} else {
1208
1216
newAccessorsBlock = newAccessors
1209
1217
}
1218
+ self . expandedAttributes. append ( macro. attributeNode)
1210
1219
}
1211
1220
} catch {
1212
1221
contextGenerator ( Syntax ( storage) ) . addDiagnostics ( from: error, node: macro. attributeNode)
1213
1222
}
1214
1223
}
1224
+
1215
1225
return ( newAccessorsBlock, expandsGetSet)
1216
1226
}
1217
1227
}
0 commit comments