Does macro hygiene only protect you in different modules?

113 views Asked by At

As for Julia 0.4.2

macro g(y)
    :((x, $y, $(esc(y))))
end

x = 1
function t()
    x = 2
    println(macroexpand(:(@g(x))))
    println(@g(x))
end

t()
println(x)

>>>
(x,x,x)
(2,2,2)
1

I had expected the result to be (1, 1, 2).

However, if I define the macro in a different module, it works as expected.

module A
macro g(y)
    :((x, $y, $(esc(y))))
end
x = 1
end

x = 3

function t()
    x = 2
    println(macroexpand(:(A.@g(x))))
    println(A.@g(x))
end

t()

>>>
(A.x,A.x,x)
(1,1,2)

Seems the hygiene simply prefix the symbols with the module namespace. Therefore it is impossible for the macro expander to distinguish different scopes in the first case.

Is this the intended behaviour?

1

There are 1 answers

1
Toivo Henningsson On

Seems like a bug. Please report it at Julia's github page.