Skip to content

Macroexpand of globals slightly broken #59008

@oscardssmith

Description

@oscardssmith
julia> count = 0
julia> macro counter(f)
           :(begin
               global count += 1
               println(count)
               $f
           end)
       end;

julia> @macroexpand @counter sin(1.1)
quote
    global Main.count += 1
    Main.println(Main.count)
    Main.sin(1.1)
end

julia> @counter cos(1.1)
ERROR: syntax: invalid assignment location "Main.count" around REPL[34]:3
Stacktrace:
 [1] top-level scope
   @ REPL[35]:1

On the other hand, manually expanding the global count += 1 line makes this work

julia> macro counter(f)
           :(begin
               global count = count + 1
               println(count)
               $f
           end)
       end;

julia> @macroexpand @counter sin(1.1)
quote
    global count = Main.count + 1
    Main.println(Main.count)
    Main.sin(1.1)
end

julia> @counter sin(1.1)
1
0.8912073600614354

This reproduces on multiple versions (1.11 and nightly at the very least)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behaviorcompiler:loweringSyntax lowering (compiler front end, 2nd stage)macros@macros

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions