Skip to content

Commit ad93407

Browse files
committed
[JuliaLowering] only hygienic-scope-wrap toplevel if unescaped
1 parent 602e5df commit ad93407

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

JuliaLowering/src/macro_expansion.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ function expand_macro(ctx, ex)
335335
# method was defined (may be different from `parentmodule(macfunc)`)
336336
mod_for_ast = lookup_method_instance(macfunc, macro_args,
337337
ctx.macro_world).def.module
338-
expanded = fix_toplevel_expansion(ctx, expanded, mod_for_ast, macro_loc)
339338
new_layer = ScopeLayer(length(ctx.scope_layers)+1, mod_for_ast,
340339
current_layer_id(ctx), true)
341340
push_layer!(ctx, mod_for_ast, true)
@@ -454,6 +453,9 @@ function expand_forms_1(ctx::MacroExpansionContext, ex::SyntaxTree)
454453
end
455454
elseif k == K"macrocall"
456455
expand_macro(ctx, ex)
456+
elseif k == K"toplevel" && length(ctx.scope_layer_stack) > 1
457+
fix_toplevel_expansion(ctx, ex, current_layer(ctx).mod,
458+
source_location(LineNumberNode, ex))
457459
elseif k == K"module" || k == K"toplevel" || k == K"inert"
458460
# Remove scope layer information from any inert syntax which survives
459461
# macro expansion so that it doesn't contaminate lowering passes which

JuliaLowering/test/macros.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,21 @@ end
483483
@test_broken JuliaLowering.eval(test_mod, code) == ("outer x", "inner x")
484484
end
485485

486+
@testset "toplevel macro hygiene" begin
487+
@eval test_mod module MacroMod
488+
macro escaped_toplevel()
489+
esc(Expr(:toplevel, :(@__MODULE__)))
490+
end
491+
macro inner_escaped_toplevel()
492+
Expr(:toplevel, esc(:(@__MODULE__)))
493+
end
494+
macro unescaped_toplevel()
495+
Expr(:toplevel, :(@__MODULE__))
496+
end
497+
end
498+
@test JuliaLowering.include_string(test_mod, "MacroMod.@escaped_toplevel") === test_mod
499+
@test JuliaLowering.include_string(test_mod, "MacroMod.@inner_escaped_toplevel") === test_mod
500+
@test JuliaLowering.include_string(test_mod, "MacroMod.@unescaped_toplevel") === test_mod.MacroMod
501+
end
502+
486503
end

0 commit comments

Comments
 (0)