Skip to content

Commit

Permalink
Don't print bad Expr(:toplevel) misleadingly
Browse files Browse the repository at this point in the history
  • Loading branch information
Keno committed Mar 13, 2024
1 parent 494420c commit 929dc76
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2504,6 +2504,11 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int, quote_level::In
elseif head === :meta && nargs == 2 && args[1] === :pop_loc
print(io, "# meta: pop locations ($(args[2]::Int))")
# print anything else as "Expr(head, args...)"
elseif head === :toplevel
# Reset SOURCE_SLOTNAMES. Raw SlotNumbers are not valid in Expr(:toplevel), but
# we want to show bad ASTs reasonably to make errors understandable.
lambda_io = IOContext(io, :SOURCE_SLOTNAMES => false)
show_unquoted_expr_fallback(lambda_io, ex, indent, quote_level)
else
unhandled = true
end
Expand Down
11 changes: 11 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2683,3 +2683,14 @@ end
using .Issue49382
(::Type{Issue49382.Type49382})() = 1
@test sprint(show, methods(Issue49382.Type49382)) isa String

# Showing of bad SlotNumber in Expr(:toplevel)
let lowered = Meta.lower(Main, Expr(:let, Expr(:block), Expr(:block, Expr(:toplevel, :(x = 1)), :(y = 1))))
ci = lowered.args[1]
@assert isa(ci, Core.CodeInfo)
@test !isempty(ci.slotnames)
@assert ci.code[1].head === :toplevel
ci.code[1].args[1] = :($(Core.SlotNumber(1)) = 1)
# Check that this gets printed as `_1 = 1` not `y = 1`
@test contains(sprint(show, ci), "_1 = 1")
end

0 comments on commit 929dc76

Please sign in to comment.