@@ -930,23 +930,30 @@ private class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
930
930
}
931
931
932
932
override func visit( _ node: VariableDeclSyntax ) -> DeclSyntax {
933
- var node = super. visit ( node) . cast ( VariableDeclSyntax . self)
933
+ var rewrittenNode = super. visit ( node) . cast ( VariableDeclSyntax . self)
934
934
935
- guard !macroAttributes( attachedTo: DeclSyntax ( node ) , ofType: AccessorMacro . Type. self) . isEmpty else {
936
- return DeclSyntax ( node )
935
+ guard !macroAttributes( attachedTo: DeclSyntax ( rewrittenNode ) , ofType: AccessorMacro . Type. self) . isEmpty else {
936
+ return DeclSyntax ( rewrittenNode )
937
937
}
938
938
939
- guard node . bindings. count == 1 ,
940
- var binding = node . bindings. first
939
+ guard rewrittenNode . bindings. count == 1 ,
940
+ var binding = rewrittenNode . bindings. first
941
941
else {
942
942
contextGenerator ( Syntax ( node) ) . addDiagnostics (
943
943
from: MacroApplicationError . accessorMacroOnVariableWithMultipleBindings,
944
- node: node
944
+ node: rewrittenNode
945
945
)
946
- return DeclSyntax ( node )
946
+ return DeclSyntax ( rewrittenNode )
947
947
}
948
948
949
- var expansion = expandAccessors ( of: node, existingAccessors: binding. accessorBlock)
949
+ // Generate the context based on the node before it was rewritten by calling `super.visit`. If the node was modified
950
+ // by `super.visit`, it will not have any parents, which would cause the lexical context to be empty.
951
+ let context = contextGenerator ( Syntax ( node) )
952
+ var expansion = expandAccessors (
953
+ of: rewrittenNode,
954
+ context: context,
955
+ existingAccessors: binding. accessorBlock
956
+ )
950
957
951
958
if expansion. accessors != binding. accessorBlock {
952
959
if binding. accessorBlock == nil {
@@ -966,16 +973,25 @@ private class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
966
973
binding. accessorBlock = expansion. accessors
967
974
}
968
975
969
- node . bindings = [ binding]
976
+ rewrittenNode . bindings = [ binding]
970
977
}
971
978
972
- return DeclSyntax ( node )
979
+ return DeclSyntax ( rewrittenNode )
973
980
}
974
981
975
982
override func visit( _ node: SubscriptDeclSyntax ) -> DeclSyntax {
976
- var node = super. visit ( node) . cast ( SubscriptDeclSyntax . self)
977
- node. accessorBlock = expandAccessors ( of: node, existingAccessors: node. accessorBlock) . accessors
978
- return DeclSyntax ( node)
983
+ var rewrittenNode = super. visit ( node) . cast ( SubscriptDeclSyntax . self)
984
+ // Generate the context based on the node before it was rewritten by calling `super.visit`. If the node was modified
985
+ // by `super.visit`, it will not have any parents, which would cause the lexical context to be empty.
986
+ let context = contextGenerator ( Syntax ( node) )
987
+ rewrittenNode. accessorBlock =
988
+ expandAccessors (
989
+ of: rewrittenNode,
990
+ context: context,
991
+ existingAccessors: rewrittenNode. accessorBlock
992
+ )
993
+ . accessors
994
+ return DeclSyntax ( rewrittenNode)
979
995
}
980
996
}
981
997
@@ -1160,6 +1176,7 @@ extension MacroApplication {
1160
1176
/// removed).
1161
1177
private func expandAccessors(
1162
1178
of storage: some DeclSyntaxProtocol ,
1179
+ context: Context ,
1163
1180
existingAccessors: AccessorBlockSyntax ?
1164
1181
) -> ( accessors: AccessorBlockSyntax ? , expandsGetSet: Bool ) {
1165
1182
let accessorMacros = macroAttributes ( attachedTo: DeclSyntax ( storage) , ofType: AccessorMacro . Type. self)
@@ -1184,7 +1201,7 @@ extension MacroApplication {
1184
1201
definition: macro. definition,
1185
1202
attributeNode: macro. attributeNode,
1186
1203
attachedTo: DeclSyntax ( storage) ,
1187
- in: contextGenerator ( Syntax ( storage ) ) ,
1204
+ in: context ,
1188
1205
indentationWidth: indentationWidth
1189
1206
) {
1190
1207
checkExpansions ( newAccessors)
@@ -1201,7 +1218,7 @@ extension MacroApplication {
1201
1218
definition: macro. definition,
1202
1219
attributeNode: macro. attributeNode,
1203
1220
attachedTo: DeclSyntax ( storage) ,
1204
- in: contextGenerator ( Syntax ( storage ) ) ,
1221
+ in: context ,
1205
1222
indentationWidth: indentationWidth
1206
1223
) {
1207
1224
guard case . accessors( let accessorList) = newAccessors. accessors else {
@@ -1220,7 +1237,7 @@ extension MacroApplication {
1220
1237
}
1221
1238
}
1222
1239
} catch {
1223
- contextGenerator ( Syntax ( storage ) ) . addDiagnostics ( from: error, node: macro. attributeNode)
1240
+ context . addDiagnostics ( from: error, node: macro. attributeNode)
1224
1241
}
1225
1242
}
1226
1243
return ( newAccessorsBlock, expandsGetSet)
0 commit comments