# \`@def\` macro generator broken on master

**URL:** <https://discourse.julialang.org/t/def-macro-generator-broken-on-master/1096>\
**Category:** General Usage\
**Created:** [December 21, 2016, 7:15pm UTC](https://discourse.julialang.org/t/def-macro-generator-broken-on-master/1096 "2016-12-21T19:15:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 21, 2016, 7:15pm UTC](https://discourse.julialang.org/t/def-macro-generator-broken-on-master/1096/1 "2016-12-21T19:15:58Z")

</div>

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

```julia
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
julia> @def test begin
         a = 2
       end
ERROR: syntax: invalid macro definition

```

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

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 21, 2016, 7:41pm UTC](https://discourse.julialang.org/t/def-macro-generator-broken-on-master/1096/2 "2016-12-21T19:41:20Z")

</div>

> <https://github.com/JuliaLang/julia/pull/15850>
>
> There is a surprising number of base code that relies on the wrong behavior....
> …
> One broken test/code that need to be fixed on this commit is the \[MacroGlobalFunction test\](https://github.com/JuliaLang/julia/commit/a8284df44d61bdeb3f03779ee9cbcb6fdd04a8c4#diff-75b23cc61d190dd99feab17b8e297ee3L3463). The test doesn't work if the macro is used outside the module so I think the fix is correct. However, should the macro expander detect this case and avoid modifying the function name that is defined to be global already?
> 
> Closes #14893
> 
> c.c. @JeffBezanson about the test mentioned above and since the code was added a long time ago (https://github.com/JuliaLang/julia/commit/605bc65c59451b567bd64e750c1e659424759fe2) I can't really find a explanation why the check is necessary.

Escape all user inputs.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 21, 2016, 7:56pm UTC](https://discourse.julialang.org/t/def-macro-generator-broken-on-master/1096/3 "2016-12-21T19:56:54Z")

</div>

I see, name wasn’t escaped:

```julia
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.
