Skip to content

Commit

Permalink
fix JuliaLang#9474 (missing parens in expr show for ::, :, etc)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Dec 30, 2014
1 parent e478a6e commit 5219315
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 5 additions & 6 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ const uni_ops = Set{Symbol}([:(+), :(-), :(!), :(¬), :(~), :(<:), :(>:), :(√)
const expr_infix_wide = Set([:(=), :(+=), :(-=), :(*=), :(/=), :(\=), :(&=),
:(|=), :($=), :(>>>=), :(>>=), :(<<=), :(&&), :(||), :(<:), :(=>)])
const expr_infix = Set([:(:), :(->), symbol("::")])
const expr_infix_any = union(expr_infix, expr_infix_wide)
const expr_calls = Dict(:call =>('(',')'), :calldecl =>('(',')'), :ref =>('[',']'), :curly =>('{','}'))
const expr_parens = Dict(:tuple=>('(',')'), :vcat=>('[',']'), :cell1d=>("Any[","]"),
:hcat =>('[',']'), :row =>('[',']'))
Expand Down Expand Up @@ -423,15 +424,13 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
end

# infix (i.e. "x<:y" or "x = y")
elseif (head in expr_infix && nargs==2) || (is(head,:(:)) && nargs==3)
show_list(io, args, head, indent, 0, true)

elseif head in expr_infix_wide && nargs == 2
elseif (head in expr_infix_any && nargs==2) || (is(head,:(:)) && nargs==3)
func_prec = operator_precedence(head)
head_ = head in expr_infix_wide ? " $head " : head
if func_prec < prec
show_enclosed_list(io, '(', args, " $head ", ')', indent, func_prec, true)
show_enclosed_list(io, '(', args, head_, ')', indent, func_prec, true)
else
show_list(io, args, " $head ", indent, func_prec, true)
show_list(io, args, head_, indent, func_prec, true)
end

# list (i.e. "(1,2,3)" or "[1,2,3]")
Expand Down
5 changes: 5 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,8 @@ end"
@test_repr "< :: T"
@test_repr "S{< <: T}"
@test_repr "+ + +"

# issue #9474
for s in ("(1::Int64 == 1::Int64)::Bool", "(1:2:3) + 4", "x = 1:2:3")
@test sprint(show, parse(s)) == ":("*s*")"
end

0 comments on commit 5219315

Please sign in to comment.