Skip to content

name of method of a macro expansion is only "macro expansion" if the macro is defined in a different file from the function where it is expanded #44551

@KristofferC

Description

@KristofferC

Defining code like:

macros.jl:

macro foo()
    esc(:(x += 1))
end

macro bar()
    esc(quote
        x += 1
        error()
    end)
end

scratch.jl:

include("macros.jl")

function f(x)
    @foo()
    @bar()
end

gives (as expected) Symbol("macro expansion") in the linetable:

julia> include("scratch.jl");

julia> ci = Base.uncompressed_ast(@which f(1));

julia> ci.linetable
4-element Vector{Any}:
 Core.LineInfoNode(Main, :f, Symbol(".../scratch.jl"), 2, 0)
 Core.LineInfoNode(Main, Symbol("macro expansion"), Symbol(".../macros.jl"), 3, 1)
 Core.LineInfoNode(Main, :f, Symbol(".../scratch.jl"), 3, 0)
 Core.LineInfoNode(Main, Symbol("macro expansion"), Symbol(".../macros.jl"), 9, 3)

However, if the content of macros.jl is pasted into scratch.jl we instead get:

julia> ci.linetable
4-element Vector{Any}:
 Core.LineInfoNode(Main, :f, Symbol(".../scratch.jl"), 15, 0)
 Core.LineInfoNode(Main, :f, Symbol(".../scratch.jl"), 3, 0)
 Core.LineInfoNode(Main, :f, Symbol(".../scratch.jl"), 16, 0)
 Core.LineInfoNode(Main, :f, Symbol(".../scratch.jl"), 9, 0)

This also means that the "macro expansion" stack frame is lost in case there is an error in the macro. For example, running the first case:

julia> f(1)
ERROR: 
Stacktrace:
 [1] error()
   @ Base ./error.jl:44
 [2] macro expansion
   @ ~.../macros.jl:8 [inlined]
 [3] f(x::Int64)
   @ Main ~.../scratch.jl:5
 [4] top-level scope
   @ REPL[4]:1

However, in the second case there is no macro expansion frame:

julia> f(1)
ERROR: 
Stacktrace:
 [1] error()
   @ Base ./error.jl:44
 [2] f(x::Int64)
   @ Main ~.../scratch.jl:8
 [3] top-level scope
   @ REPL[6]:1

Metadata

Metadata

Assignees

No one assigned

    Labels

    parserLanguage parsing and surface syntax

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions