Skip to content

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

@Liozou

Description

@Liozou

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions