julia icon indicating copy to clipboard operation
julia copied to clipboard

Keyword arguments emitted by macros don't get gensym'd

Open aplavin opened this issue 1 year ago • 0 comments

Positional and keyword arguments are treated differently in code emitted by macros:

# @m1 defines function f with position argument
julia> macro m1()
       :(function f(x=1)
       x = x * 10
       end)
       end

# @m2 defines function g with keyword argument
julia> macro m2()
       :(function g(;x=1)
       x = x * 10
       end)
       end

# in @m1, all occurrences of x are properly replaced by the same gensymmed name
julia> @macroexpand @m1
:(function Main.f(var"#84#x" = 1)
      #= REPL[11]:2 =#
      #= REPL[11]:3 =#
      var"#84#x" = var"#84#x" * 10
  end)

# in @m2, the x symbol in signature remains as-is, ...
julia> @macroexpand @m2
:(function Main.g(; x = 1)
      #= REPL[12]:2 =#
      #= REPL[12]:3 =#
      var"#85#x" = var"#85#x" * 10
  end)

# ..., leading to the obvious error:
julia> @m2

julia> g()
ERROR: UndefVarError: `x` not defined

I believe the positional argument handling is correct (m1 above), but not kwargs handling (m2 above).

aplavin avatar May 13 '24 10:05 aplavin