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

Macro hygiene pass is not aware of const fields structs #51899

Closed
Liozou opened this issue Oct 27, 2023 · 0 comments · Fixed by #51980
Closed

Macro hygiene pass is not aware of const fields structs #51899

Liozou opened this issue Oct 27, 2023 · 0 comments · Fixed by #51980
Labels
macros @macros

Comments

@Liozou
Copy link
Member

Liozou commented Oct 27, 2023

Consider a macro that defines a simple struct:

julia> macro bar()
           Expr(:struct, true, :Bar, Expr(:block, Expr(:(::), :field, :Int)))
       end
@bar (macro with 1 method)

julia> @macroexpand @bar
:(mutable struct Bar
      field::Main.Int
  end)

julia> @bar

julia> Bar(4)
Bar(4)

No problem.

Mark the field as const and the hygiene pass starts believing field is a variable instead of a field:

julia> macro foo()
           Expr(:struct, true, :Foo, Expr(:block, Expr(:const, Expr(:(::), :field, :Int))))
       end
@foo (macro with 1 method)

julia> @macroexpand @foo
:(mutable struct Foo
      const Main.field::Main.Int
  end)

resulting in the confusing error message:

julia> @foo
ERROR: syntax: expected assignment after "const" around REPL[7]:1
Stacktrace:
 [1] top-level scope
   @ REPL[7]:1

Of course a solution consists in wrapping :field in an esc call, but that shouldn't be necessary (since it's not necessary in the first case).

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

Successfully merging a pull request may close this issue.

1 participant