Skip to content

LineNumberNode in expressions do not print in a way that makes them parse back correctly #57876

@KristofferC

Description

@KristofferC

Consider

julia> using Test

julia> Base.remove_linenums!(@macroexpand1 @test 1 == 2)
quote
    if false
        Test.record(Test.get_testset(), Test.Broken(:skipped, $(QuoteNode(:(1 == 2)))))
    else
        let var"#10#_do" = if false
                    Test.do_broken_test
                else
                    Test.do_test
                end
            var"#10#_do"(begin
                    try
                        Test.eval_test(Test.Expr(:comparison, 1, ==, 2), Test.Expr(:comparison, $(QuoteNode(1)), :(==), $(QuoteNode(2))), $(QuoteNode(:(#= REPL[19]:1 =#))), $(QuoteNode(false)))
                    catch var"#12#_e"
                        var"#12#_e" isa Test.InterruptException && Test.rethrow()
                        Test.Threw(var"#12#_e", (Test.Base).current_exceptions(), $(QuoteNode(:(#= REPL[19]:1 =#)))) # <----------------
                    end
                end, $(QuoteNode(:(1 == 2))))
        end
    end
end

If one tries to copy paste this expression and eval it one gets:

julia> eval(q)
ERROR: MethodError: Cannot `convert` an object of type Expr to an object of type LineNumberNode
The function `convert` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  convert(::Type{T}, ::T) where T
   @ Base Base_compiler.jl:134

Stacktrace:
 [1] Test.Threw(exception::Any, backtrace::Any, source::Any)
   @ Test ~/.julia/juliaup/julia-1.12-nightly/share/julia/stdlib/v1.12/Test/src/Test.jl:345
 [2] top-level scope
   @ REPL[20]:15
 [3] eval(m::Module, e::Any)
   @ Core ./boot.jl:489
 [4] top-level scope
   @ REPL[21]:1

Because the $(QuoteNode(:(#= REPL[19]:1 =#)))) printing part gets parsed as

$(QuoteNode(:(())))

Metadata

Metadata

Assignees

No one assigned

    Labels

    display and printingAesthetics and correctness of printed representations of objects.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions