Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/macro_expansion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right. So this approach doesn't work if the old macro returns an AST, because that AST will be Expr but the macro expander (and subsequent lowering) is expecting a SyntaxTree.

In that case we need full bidirectional Expr <--> SyntaxTree support. And also need a solution for handling hygiene which is represented rather differently in the two cases. See my WIP branch for how to do those :-)

end
catch exc
if exc isa MacroExpansionError
# Add context to the error.
Expand Down
6 changes: 6 additions & 0 deletions test/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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