It seems that a macro returning a :macrocall expression with escaped macro name does not work whereas a :call expression with escaped function name is no problem:
julia> macro a(ex::Expr) Expr(ex.head, esc.(ex.args)...) end
@a (macro with 1 method)
julia> @a one(0)
1
julia> @a @inbounds 0
ERROR: LoadError: syntax: "esc(...)" used outside of macro expansion
Stacktrace:
[1] top-level scope
@ REPL[3]:1
in expression starting at <macrocall>:0
Same example with dumped expressions
julia> macro a(ex::Expr) escex = Expr(ex.head, esc.(ex.args)...); dump(escex); escex end
@a (macro with 1 method)
julia> @a one(0)
Expr
head: Symbol call
args: Array{Any}((2,))
1: Expr
head: Symbol escape
args: Array{Any}((1,))
1: Symbol one
2: Expr
head: Symbol escape
args: Array{Any}((1,))
1: Int64 0
1
julia> @a @inbounds 0
Expr
head: Symbol macrocall
args: Array{Any}((3,))
1: Expr
head: Symbol escape
args: Array{Any}((1,))
1: Symbol @inbounds
2: Expr
head: Symbol escape
args: Array{Any}((1,))
1: LineNumberNode
line: Int64 1
file: Symbol REPL[3]
3: Expr
head: Symbol escape
args: Array{Any}((1,))
1: Int64 0
ERROR: LoadError: syntax: "esc(...)" used outside of macro expansion
Stacktrace:
[1] top-level scope
@ REPL[3]:1
in expression starting at <macrocall>:0
julia> @macroexpand1 @a @inbounds 0
Expr
head: Symbol macrocall
args: Array{Any}((3,))
1: Expr
head: Symbol escape
args: Array{Any}((1,))
1: Symbol @inbounds
2: Expr
head: Symbol escape
args: Array{Any}((1,))
1: LineNumberNode
line: Int64 1
file: Symbol REPL[4]
3: Expr
head: Symbol escape
args: Array{Any}((1,))
1: Int64 0
:($(Expr(:escape, Symbol("@inbounds"))) $(Expr(:escape, 0)))
Is there a reason for this?