Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
aplavin opened this issue May 13, 2024 · 0 comments
Open

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

aplavin opened this issue May 13, 2024 · 0 comments
Labels
macros @macros

Comments

@aplavin
Copy link
Contributor

aplavin commented May 13, 2024

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).

@inkydragon inkydragon added the macros @macros label May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
macros @macros
Projects
None yet
Development

No branches or pull requests

2 participants