Skip to content

Commit 5d0a7dc

Browse files
vtjnashKristofferC
authored andcommitted
show: consolidate wheres with {} in printing
Always a bit more compact in this form, and somewhat easier to implement too (thus keeping this consistent with the corrected typealias printing). (cherry picked from commit ee816ef)
1 parent 52fbe7f commit 5d0a7dc

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

base/show.jl

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ end
628628

629629
function make_wheres(io::IO, env::SimpleVector, @nospecialize(x::Type))
630630
seen = IdSet()
631-
wheres = []
631+
wheres = TypeVar[]
632632
# record things printed by the context
633633
if io isa IOContext
634634
for (key, val) in io.dict
@@ -827,22 +827,30 @@ function show(io::IO, @nospecialize(x::Type))
827827
end
828828

829829
x = x::UnionAll
830-
if x.var.name === :_ || io_has_tvar_name(io, x.var.name, x)
831-
counter = 1
832-
while true
833-
newname = Symbol(x.var.name, counter)
834-
if !io_has_tvar_name(io, newname, x)
835-
newtv = TypeVar(newname, x.var.lb, x.var.ub)
836-
x = UnionAll(newtv, x{newtv})
837-
break
830+
wheres = TypeVar[]
831+
let io = IOContext(io)
832+
while x isa UnionAll
833+
var = x.var
834+
if var.name === :_ || io_has_tvar_name(io, var.name, x)
835+
counter = 1
836+
while true
837+
newname = Symbol(var.name, counter)
838+
if !io_has_tvar_name(io, newname, x)
839+
var = TypeVar(newname, var.lb, var.ub)
840+
x = x{var}
841+
break
842+
end
843+
counter += 1
844+
end
845+
else
846+
x = x.body
838847
end
839-
counter += 1
848+
push!(wheres, var)
849+
io = IOContext(io, :unionall_env => var)
840850
end
851+
show(io, x)
841852
end
842-
843-
show(IOContext(io, :unionall_env => x.var), x.body)
844-
print(io, " where ")
845-
show(io, x.var)
853+
show_wheres(io, wheres)
846854
end
847855

848856
# Check whether 'sym' (defined in module 'parent') is visible from module 'from'

test/show.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ end
634634
# `where` syntax
635635
@test_repr "A where T<:B"
636636
@test_repr "A where T<:(Array{T} where T<:Real)"
637-
@test_repr "Array{T} where T<:Array{S} where S<:Real"
637+
@test_repr "Array{T} where {S<:Real, T<:Array{S}}"
638638
@test_repr "x::Array{T} where T"
639639
@test_repr "(a::b) where T"
640640
@test_repr "a::b where T"
@@ -1568,12 +1568,12 @@ end
15681568
end
15691569

15701570
let x = TypeVar(:_), y = TypeVar(:_)
1571-
@test repr(UnionAll(x, UnionAll(y, Pair{x,y}))) == "Pair{_1, _2} where _2 where _1"
1571+
@test repr(UnionAll(x, UnionAll(y, Pair{x,y}))) == "Pair{_1, _2} where {_1, _2}"
15721572
@test repr(UnionAll(x, UnionAll(y, Pair{UnionAll(x,Ref{x}),y}))) == "Pair{Ref{_1} where _1, _1} where _1"
15731573
x = TypeVar(:a)
15741574
y = TypeVar(:a)
15751575
z = TypeVar(:a)
1576-
@test repr(UnionAll(z, UnionAll(x, UnionAll(y, Tuple{x,y,z})))) == "Tuple{a1, a2, a} where a2 where a1 where a"
1576+
@test repr(UnionAll(z, UnionAll(x, UnionAll(y, Tuple{x,y,z})))) == "Tuple{a1, a2, a} where {a, a1, a2}"
15771577
end
15781578

15791579
@testset "showarg" begin

0 commit comments

Comments
 (0)