`@def` macro generator broken on master

I use this @def on v0.5 to generate macros for inlining code.

julia>   macro def(name, definition)
             return quote
                 macro $name()
                     esc($(Expr(:quote, definition)))
                 end
             end
         end
@def (macro with 1 method)

However, on the most current nightly I get an error:

julia> @def test begin
         a = 2
       end
ERROR: syntax: invalid macro definition

Can someone point me to what changed and what the workaround is?

Escape all user inputs.

I see, name wasn’t escaped:

macro def(name, definition)
    return quote
        macro $(esc(name))()
            esc($(Expr(:quote, definition)))
        end
    end
end
@def test begin
         a = 2
       end

# Now @test "pastes" the code `a=2`

is good now.

3 Likes