@@ -21,11 +21,12 @@ struct MacroExpansionContext{GraphType} <: AbstractLoweringContext
2121 scope_layers:: Vector{ScopeLayer}
2222 scope_layer_stack:: Vector{LayerId}
2323 expr_compat_mode:: Bool
24+ macro_world:: UInt
2425end
2526
26- function MacroExpansionContext (graph:: SyntaxGraph , mod:: Module , expr_compat_mode:: Bool )
27+ function MacroExpansionContext (graph:: SyntaxGraph , mod:: Module , expr_compat_mode:: Bool , world :: UInt )
2728 layers = ScopeLayer[ScopeLayer (1 , mod, 0 , false )]
28- MacroExpansionContext (graph, Bindings (), layers, LayerId[length (layers)], expr_compat_mode)
29+ MacroExpansionContext (graph, Bindings (), layers, LayerId[length (layers)], expr_compat_mode, world )
2930end
3031
3132current_layer (ctx:: MacroExpansionContext ) = ctx. scope_layers[last (ctx. scope_layer_stack)]
@@ -217,11 +218,10 @@ function expand_macro(ctx, ex)
217218 # age changes concurrently.
218219 #
219220 # TODO : Allow this to be passed in
220- macro_world = Base. get_world_counter ()
221- if hasmethod (macfunc, Tuple{typeof (mctx), typeof .(raw_args)... }; world= macro_world)
221+ if hasmethod (macfunc, Tuple{typeof (mctx), typeof .(raw_args)... }; world= ctx. macro_world)
222222 macro_args = prepare_macro_args (ctx, mctx, raw_args)
223223 expanded = try
224- Base. invoke_in_world (macro_world, macfunc, macro_args... )
224+ Base. invoke_in_world (ctx . macro_world, macfunc, macro_args... )
225225 catch exc
226226 newexc = exc isa MacroExpansionError ?
227227 MacroExpansionError (mctx, exc. ex, exc. msg, exc. position, exc. err) :
@@ -260,14 +260,14 @@ function expand_macro(ctx, ex)
260260 push! (macro_args, Expr (arg))
261261 end
262262 expanded = try
263- Base. invoke_in_world (macro_world, macfunc, macro_args... )
263+ Base. invoke_in_world (ctx . macro_world, macfunc, macro_args... )
264264 catch exc
265265 if exc isa MethodError && exc. f === macfunc
266- if ! isempty (methods_in_world (macfunc, Tuple{typeof (mctx), Vararg{Any}}, macro_world))
266+ if ! isempty (methods_in_world (macfunc, Tuple{typeof (mctx), Vararg{Any}}, ctx . macro_world))
267267 # If the macro has at least some methods implemented in the
268268 # new style, assume the user meant to call one of those
269269 # rather than any old-style macro methods which might exist
270- exc = MethodError (macfunc, (prepare_macro_args (ctx, mctx, raw_args)... , ), macro_world)
270+ exc = MethodError (macfunc, (prepare_macro_args (ctx, mctx, raw_args)... , ), ctx . macro_world)
271271 end
272272 end
273273 rethrow (MacroExpansionError (mctx, ex, " Error expanding macro" , :all , exc))
@@ -280,7 +280,7 @@ function expand_macro(ctx, ex)
280280 # Module scope for the returned AST is the module where this particular
281281 # method was defined (may be different from `parentmodule(macfunc)`)
282282 mod_for_ast = lookup_method_instance (macfunc, macro_args,
283- macro_world). def. module
283+ ctx . macro_world). def. module
284284 new_layer = ScopeLayer (length (ctx. scope_layers)+ 1 , mod_for_ast,
285285 current_layer_id (ctx), true )
286286 push! (ctx. scope_layers, new_layer)
@@ -460,18 +460,18 @@ function expand_forms_1(ctx::MacroExpansionContext, ex::SyntaxTree)
460460 end
461461end
462462
463- function expand_forms_1 (mod:: Module , ex:: SyntaxTree , expr_compat_mode:: Bool )
463+ function expand_forms_1 (mod:: Module , ex:: SyntaxTree , expr_compat_mode:: Bool , macro_world :: UInt )
464464 graph = ensure_attributes (syntax_graph (ex),
465465 var_id= IdTag,
466466 scope_layer= LayerId,
467467 __macro_ctx__= Nothing,
468468 meta= CompileHints)
469- ctx = MacroExpansionContext (graph, mod, expr_compat_mode)
469+ ctx = MacroExpansionContext (graph, mod, expr_compat_mode, macro_world )
470470 ex2 = expand_forms_1 (ctx, reparent (ctx, ex))
471471 graph2 = delete_attributes (graph, :__macro_ctx__ )
472472 # TODO : Returning the context with pass-specific mutable data is a bad way
473473 # to carry state into the next pass. We might fix this by attaching such
474474 # data to the graph itself as global attributes?
475- ctx2 = MacroExpansionContext (graph2, ctx. bindings, ctx. scope_layers, LayerId[], expr_compat_mode)
475+ ctx2 = MacroExpansionContext (graph2, ctx. bindings, ctx. scope_layers, LayerId[], expr_compat_mode, macro_world )
476476 return ctx2, reparent (ctx2, ex2)
477477end
0 commit comments