Skip to content

Commit 5219315

Browse files
committed
fix #9474 (missing parens in expr show for ::, :, etc)
1 parent e478a6e commit 5219315

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

base/show.jl

+5-6
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ const uni_ops = Set{Symbol}([:(+), :(-), :(!), :(¬), :(~), :(<:), :(>:), :(√)
247247
const expr_infix_wide = Set([:(=), :(+=), :(-=), :(*=), :(/=), :(\=), :(&=),
248248
:(|=), :($=), :(>>>=), :(>>=), :(<<=), :(&&), :(||), :(<:), :(=>)])
249249
const expr_infix = Set([:(:), :(->), symbol("::")])
250+
const expr_infix_any = union(expr_infix, expr_infix_wide)
250251
const expr_calls = Dict(:call =>('(',')'), :calldecl =>('(',')'), :ref =>('[',']'), :curly =>('{','}'))
251252
const expr_parens = Dict(:tuple=>('(',')'), :vcat=>('[',']'), :cell1d=>("Any[","]"),
252253
:hcat =>('[',']'), :row =>('[',']'))
@@ -423,15 +424,13 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
423424
end
424425

425426
# infix (i.e. "x<:y" or "x = y")
426-
elseif (head in expr_infix && nargs==2) || (is(head,:(:)) && nargs==3)
427-
show_list(io, args, head, indent, 0, true)
428-
429-
elseif head in expr_infix_wide && nargs == 2
427+
elseif (head in expr_infix_any && nargs==2) || (is(head,:(:)) && nargs==3)
430428
func_prec = operator_precedence(head)
429+
head_ = head in expr_infix_wide ? " $head " : head
431430
if func_prec < prec
432-
show_enclosed_list(io, '(', args, " $head ", ')', indent, func_prec, true)
431+
show_enclosed_list(io, '(', args, head_, ')', indent, func_prec, true)
433432
else
434-
show_list(io, args, " $head ", indent, func_prec, true)
433+
show_list(io, args, head_, indent, func_prec, true)
435434
end
436435

437436
# list (i.e. "(1,2,3)" or "[1,2,3]")

test/show.jl

+5
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,8 @@ end"
191191
@test_repr "< :: T"
192192
@test_repr "S{< <: T}"
193193
@test_repr "+ + +"
194+
195+
# issue #9474
196+
for s in ("(1::Int64 == 1::Int64)::Bool", "(1:2:3) + 4", "x = 1:2:3")
197+
@test sprint(show, parse(s)) == ":("*s*")"
198+
end

0 commit comments

Comments
 (0)