Skip to content

Logging macros need to much escaping when called in other macros #41451

@giordano

Description

@giordano
julia> macro foo()
           quote
               msg = "this is an error"
               @error msg
           end
       end
@foo (macro with 1 method)

julia> @foo
┌ Error: Exception while generating log record in module Main at REPL[10]:4
│   exception =
│    UndefVarError: msg not defined
│    Stacktrace:
│      [1] logging_error(logger::Any, level::Any, _module::Any, group::Any, id::Any, filepath::Any, line::Any, err::Any, real::Bool)
│        @ Base.CoreLogging ./logging.jl:442
│      [2] macro expansion
│        @ logging.jl:332 [inlined]
│      [3] top-level scope
│        @ REPL[10]:4
│      [4] eval
│        @ ./boot.jl:360 [inlined]
│      [5] eval_user_input(ast::Any, backend::REPL.REPLBackend)
│        @ REPL /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/REPL/src/REPL.jl:139
│      [6] repl_backend_loop(backend::REPL.REPLBackend)
│        @ REPL /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/REPL/src/REPL.jl:200
│      [7] start_repl_backend(backend::REPL.REPLBackend, consumer::Any)
│        @ REPL /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/REPL/src/REPL.jl:185
│      [8] run_repl(repl::REPL.AbstractREPL, consumer::Any; backend_on_current_task::Bool)
│        @ REPL /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/REPL/src/REPL.jl:317
│      [9] run_repl(repl::REPL.AbstractREPL, consumer::Any)
│        @ REPL /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/REPL/src/REPL.jl:305
│     [10] (::Base.var"#874#876"{Bool, Bool, Bool})(REPL::Module)
│        @ Base ./client.jl:387
│     [11] #invokelatest#2
│        @ ./essentials.jl:708 [inlined]
│     [12] invokelatest
│        @ ./essentials.jl:706 [inlined]
│     [13] run_main_repl(interactive::Bool, quiet::Bool, banner::Bool, history_file::Bool, color_set::Bool)
│        @ Base ./client.jl:372
│     [14] exec_options(opts::Base.JLOptions)
│        @ Base ./client.jl:302
│     [15] _start()
│        @ Base ./client.jl:485
└ @ Main REPL[10]:4

julia> macro foo()
           esc(quote
               msg = "this is an error"
               @error msg
           end)
       end
@foo (macro with 1 method)

julia> @foo
┌ Error: this is an error
└ @ Main REPL[12]:4

Not escaping the expression by the macro calling @error results in an UndefVarError. On slack @vtjnash pointed out that the arguments of the logging macros are already escaped in

julia/base/logging.jl

Lines 409 to 422 in 4270d3b

if k === :_group
_group = esc(v)
elseif k === :_id
_id = esc(v)
elseif k === :_module
_module = esc(v)
elseif k === :_file
_file = esc(v)
elseif k === :_line
_line = esc(v)
else
# Copy across key value pairs for structured log records
push!(kwargs, Expr(:kw, k, esc(v)))
end
and the extra escape in the definition of the custom macro shouldn't be necessary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler:loweringSyntax lowering (compiler front end, 2nd stage)loggingThe logging frameworkmacros@macros

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions