From e427498b00db6bf360f7d82a710ce83e880bf2e7 Mon Sep 17 00:00:00 2001 From: Eric Hanson <5846501+ericphanson@users.noreply.github.com> Date: Thu, 31 Jul 2025 13:44:21 +0200 Subject: [PATCH 1/2] support old-style macros --- src/macro_expansion.jl | 12 ++++++++++-- test/macros.jl | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/macro_expansion.jl b/src/macro_expansion.jl index 1e4ac756..acd7e710 100644 --- a/src/macro_expansion.jl +++ b/src/macro_expansion.jl @@ -145,8 +145,16 @@ function expand_macro(ctx, ex) end macro_invocation_world = Base.get_world_counter() expanded = try - # TODO: Allow invoking old-style macros for compat - invokelatest(macfunc, macro_args...) + if applicable(macfunc, macro_args...) + invokelatest(macfunc, macro_args...) + else + # try old-style macro + args = [Expr(x) for x in macro_args[2:end]] + line, _ = source_location(macname) + file = filename(macname) + line_number_node = Base.LineNumberNode(line, file) + invokelatest(macfunc, line_number_node, ctx.current_layer.mod, args...) + end catch exc if exc isa MacroExpansionError # Add context to the error. diff --git a/test/macros.jl b/test/macros.jl index 6e25c326..21aacc81 100644 --- a/test/macros.jl +++ b/test/macros.jl @@ -142,4 +142,10 @@ end == [ "2" ] +# old-style generated macro, e.g. as lowered by pre-JuliaLowering lowering +@eval test_mod var"@hi"(__source__::LineNumberNode, __module__::Module) = "hi" + +# check that we can macroexpand it properly even though we don't have the new method +# that accepts a MacroExpansionContext +@test JuliaLowering.include_string(test_mod, "@hi") == "hi" end From 67b12f08c4e3ba74f3c5f20c9be96644e17265bf Mon Sep 17 00:00:00 2001 From: Eric Hanson <5846501+ericphanson@users.noreply.github.com> Date: Thu, 31 Jul 2025 14:16:14 +0200 Subject: [PATCH 2/2] Update src/macro_expansion.jl --- src/macro_expansion.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/macro_expansion.jl b/src/macro_expansion.jl index acd7e710..91c92f22 100644 --- a/src/macro_expansion.jl +++ b/src/macro_expansion.jl @@ -149,7 +149,7 @@ function expand_macro(ctx, ex) invokelatest(macfunc, macro_args...) else # try old-style macro - args = [Expr(x) for x in macro_args[2:end]] + args = (Expr(x) for x in macro_args[2:end]) line, _ = source_location(macname) file = filename(macname) line_number_node = Base.LineNumberNode(line, file)