Skip to content

Commit 8d7e4ed

Browse files
c42fmlechu
andcommitted
Apply suggestions from code review
Co-authored-by: Em Chu <[email protected]>
1 parent e24528c commit 8d7e4ed

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/kinds.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ function _register_kinds()
66
# expansion, and known to lowering. These are part of the AST API but
77
# without having surface syntax.
88
"BEGIN_EXTENSION_KINDS"
9-
# Used for converting `esc()`'d expressions arising from old macro
10-
# invocations during macro expansion
11-
"escape"
129
# atomic fields or accesses (see `@atomic`)
1310
"atomic"
1411
# Flag for @generated parts of a functon

src/macro_expansion.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,22 @@ function expand_forms_1(ctx::MacroExpansionContext, ex::SyntaxTree)
315315
expand_forms_1(ctx, ex[1])
316316
elseif k == K"escape"
317317
# For processing of old-style macros
318+
@chk numchildren(ex) >= 1 "`escape` requires an argument"
319+
if length(ctx.scope_layer_stack) === 1
320+
throw(MacroExpansionError(ex, "`escape` node in outer context"))
321+
end
318322
top_layer = pop!(ctx.scope_layer_stack)
319323
escaped_ex = expand_forms_1(ctx, ex[1])
320324
push!(ctx.scope_layer_stack, top_layer)
321325
escaped_ex
326+
elseif k == K"hygienic_scope"
327+
@chk numchildren(ex) >= 2 && ex[2].value isa Module "`hygienic_scope` requires an AST and a module"
328+
new_layer = ScopeLayer(length(ctx.scope_layers)+1, ex[2].value, true)
329+
push!(ctx.scope_layers, new_layer)
330+
push!(ctx.scope_layer_stack, new_layer.id)
331+
hyg_ex = expand_forms_1(ctx, ex[1])
332+
pop!(ctx.scope_layer_stack)
333+
hyg_ex
322334
elseif k == K"juxtapose"
323335
layerid = get(ex, :scope_layer, current_layer_id(ctx))
324336
@chk numchildren(ex) == 2

test/macros.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,27 @@ end
246246
@oldstyle_non_Expr
247247
""") === 42
248248

249+
@testset "calling with old/new macro signatures" begin
250+
# Old defined with 1 arg, new with 2 args, both with 3 (but with different values)
251+
Base.eval(test_mod, :(macro sig_mismatch(x); x; end))
252+
Base.eval(test_mod, :(macro sig_mismatch(x, y, z); z; end))
253+
JuliaLowering.include_string(test_mod, "macro sig_mismatch(x, y); x; end")
254+
JuliaLowering.include_string(test_mod, "macro sig_mismatch(x, y, z); x; end")
255+
256+
@test JuliaLowering.include_string(test_mod, "@sig_mismatch(1)") === 1
257+
@test JuliaLowering.include_string(test_mod, "@sig_mismatch(1, 2)") === 1
258+
@test JuliaLowering.include_string(test_mod, "@sig_mismatch(1, 2, 3)") === 1 # 3 if we prioritize old sig
259+
err = try
260+
JuliaLowering.include_string(test_mod, "@sig_mismatch(1, 2, 3, 4)") === 1
261+
catch exc
262+
sprint(showerror, exc)
263+
end
264+
@test startswith(err, """
265+
MacroExpansionError while expanding @sig_mismatch in module Main.macros.test_mod:
266+
@sig_mismatch(1, 2, 3, 4)
267+
└───────────────────────┘ ── Error expanding macro
268+
Caused by:
269+
MethodError: no method matching var"@sig_mismatch"(::LineNumberNode, ::Module, ::Int64, ::Int64, ::Int64, ::Int64)
270+
""")
271+
end
249272
end # module macros

0 commit comments

Comments
 (0)