Skip to content

Escape @nospecialize macro output#61362

Merged
oscardssmith merged 2 commits intoJuliaLang:masterfrom
mlechu:esc-nospecialize
Mar 19, 2026
Merged

Escape @nospecialize macro output#61362
oscardssmith merged 2 commits intoJuliaLang:masterfrom
mlechu:esc-nospecialize

Conversation

@mlechu
Copy link
Copy Markdown
Member

@mlechu mlechu commented Mar 18, 2026

@nospecialize and @specialize refer to names passed as macro arguments, so
should theoretically escape them in their expansion. They currently work
without escaping because macroexpand.scm makes a special case for :meta forms.
Unfortunately, that special handling escapes :meta too aggressively:

julia> macro mac(x); x; end
@mac (macro with 1 method)

julia> ex = @macroexpand @mac function f(@nospecialize(a))
                  a
              end
:(function Main.f($(Expr(:meta, :nospecialize, :a)))
      #= REPL[2]:1 =#
      #= REPL[2]:2 =#
      var"#2#a"
  end)

julia> func = @mac function f(@nospecialize(a))
                  a
              end
f (generic function with 1 method)

julia> func(1)
ERROR: UndefVarError: `#3#a` not defined in `Main`

This PR doesn't fix the above or change any current behaviour, but at least
ensures that JuliaLowering doesn't need to introduce the same bug. There are
alternatives if this doesn't work, but all would be hacky.

@oscardssmith oscardssmith merged commit 78a0dc1 into JuliaLang:master Mar 19, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants