Skip to content

Commit

Permalink
Fix show of non-identifier Symbols in Exprs
Browse files Browse the repository at this point in the history
Fix #32354
  • Loading branch information
c42f committed Jul 3, 2019
1 parent 4b1e9e6 commit 8be46c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
19 changes: 15 additions & 4 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ end
function show_call(io::IO, head, func, func_args, indent)
op, cl = expr_calls[head]
if (isa(func, Symbol) && func !== :(:) && !(head === :. && isoperator(func))) ||
(isa(func, Expr) && (func.head == :. || func.head == :curly)) ||
(isa(func, Expr) && (func.head == :. || func.head == :curly || func.head == :macroname)) ||
isa(func, GlobalRef)
show_unquoted(io, func, indent)
else
Expand All @@ -1007,7 +1007,13 @@ end
## AST printing ##

show_unquoted(io::IO, val::SSAValue, ::Int, ::Int) = print(io, "%", val.id)
show_unquoted(io::IO, sym::Symbol, ::Int, ::Int) = print(io, sym)
function show_unquoted(io::IO, sym::Symbol, ::Int, ::Int)
if isidentifier(sym) || isoperator(sym)
print(io, sym)
else
print(io, "var", repr(string(sym)))
end
end
show_unquoted(io::IO, ex::LineNumberNode, ::Int, ::Int) = show_linenumber(io, ex.line, ex.file)
show_unquoted(io::IO, ex::GotoNode, ::Int, ::Int) = print(io, "goto %", ex.label)
function show_unquoted(io::IO, ex::GlobalRef, ::Int, ::Int)
Expand Down Expand Up @@ -1356,15 +1362,20 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
end
# Use the functional syntax unless specifically designated with prec=-1
# and hide the line number argument from the argument list
mname = args[1] isa Symbol ? Expr(:macroname, args[1]) : args[1]
if prec >= 0
show_call(io, :call, args[1], args[3:end], indent)
show_call(io, :call, mname, args[3:end], indent)
else
show_args = Vector{Any}(undef, nargs - 1)
show_args[1] = args[1]
show_args[1] = mname
show_args[2:end] = args[3:end]
show_list(io, show_args, ' ', indent)
end

elseif head === :macroname && nargs == 1
# `Expr(:macroname, mname)` => print `mname` symbol without escaping `@`
print(io, string(args[1]))

elseif head === :line && 1 <= nargs <= 2
show_linenumber(io, args...)

Expand Down
5 changes: 5 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ end
@test sprint(show, :+) == ":+"
@test sprint(show, :end) == ":end"

# issue #32354
@test sprint(show, Expr(:call, :foo, Symbol("@bar"))) == ":(foo(var\"@bar\"))"
@test sprint(show, Expr(:call, :foo, Symbol("##"))) == ":(foo(var\"##\"))"
@test sprint(show, Expr(:call, :foo, Symbol("a-b"))) == ":(foo(var\"a-b\"))"

# issue #12477
@test sprint(show, Union{Int64, Int32, Int16, Int8, Float64}) == "Union{Float64, Int16, Int32, Int64, Int8}"

Expand Down

0 comments on commit 8be46c4

Please sign in to comment.