From 52193154ec1abc60f2bf6736d38665aab286b306 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Tue, 30 Dec 2014 13:33:55 -0500 Subject: [PATCH] fix #9474 (missing parens in expr show for ::, :, etc) --- base/show.jl | 11 +++++------ test/show.jl | 5 +++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/base/show.jl b/base/show.jl index 6577b16f54d1c..4b79ff7f41cc7 100644 --- a/base/show.jl +++ b/base/show.jl @@ -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 =>('[',']')) @@ -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]") diff --git a/test/show.jl b/test/show.jl index 049c452804b3f..14ef6047e0d7a 100644 --- a/test/show.jl +++ b/test/show.jl @@ -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