From 5d0a7dc06608e34c3c77dab81eec746aff07b97b Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Fri, 22 Jan 2021 16:07:39 -0500 Subject: [PATCH] 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 ee816ef4f27c7e1eb531083fcb57933d1e5af1e5) --- base/show.jl | 36 ++++++++++++++++++++++-------------- test/show.jl | 6 +++--- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/base/show.jl b/base/show.jl index 5653ba8b5128e..2fbc8f50ee737 100644 --- a/base/show.jl +++ b/base/show.jl @@ -628,7 +628,7 @@ end function make_wheres(io::IO, env::SimpleVector, @nospecialize(x::Type)) seen = IdSet() - wheres = [] + wheres = TypeVar[] # record things printed by the context if io isa IOContext for (key, val) in io.dict @@ -827,22 +827,30 @@ function show(io::IO, @nospecialize(x::Type)) end x = x::UnionAll - if x.var.name === :_ || io_has_tvar_name(io, x.var.name, x) - counter = 1 - while true - newname = Symbol(x.var.name, counter) - if !io_has_tvar_name(io, newname, x) - newtv = TypeVar(newname, x.var.lb, x.var.ub) - x = UnionAll(newtv, x{newtv}) - break + wheres = TypeVar[] + let io = IOContext(io) + while x isa UnionAll + var = x.var + if var.name === :_ || io_has_tvar_name(io, var.name, x) + counter = 1 + while true + newname = Symbol(var.name, counter) + if !io_has_tvar_name(io, newname, x) + var = TypeVar(newname, var.lb, var.ub) + x = x{var} + break + end + counter += 1 + end + else + x = x.body end - counter += 1 + push!(wheres, var) + io = IOContext(io, :unionall_env => var) end + show(io, x) end - - show(IOContext(io, :unionall_env => x.var), x.body) - print(io, " where ") - show(io, x.var) + show_wheres(io, wheres) end # Check whether 'sym' (defined in module 'parent') is visible from module 'from' diff --git a/test/show.jl b/test/show.jl index 7d63e099daa95..b0bc4455d990a 100644 --- a/test/show.jl +++ b/test/show.jl @@ -634,7 +634,7 @@ end # `where` syntax @test_repr "A where T<:B" @test_repr "A where T<:(Array{T} where T<:Real)" -@test_repr "Array{T} where T<:Array{S} where S<:Real" +@test_repr "Array{T} where {S<:Real, T<:Array{S}}" @test_repr "x::Array{T} where T" @test_repr "(a::b) where T" @test_repr "a::b where T" @@ -1568,12 +1568,12 @@ end end let x = TypeVar(:_), y = TypeVar(:_) - @test repr(UnionAll(x, UnionAll(y, Pair{x,y}))) == "Pair{_1, _2} where _2 where _1" + @test repr(UnionAll(x, UnionAll(y, Pair{x,y}))) == "Pair{_1, _2} where {_1, _2}" @test repr(UnionAll(x, UnionAll(y, Pair{UnionAll(x,Ref{x}),y}))) == "Pair{Ref{_1} where _1, _1} where _1" x = TypeVar(:a) y = TypeVar(:a) z = TypeVar(:a) - @test repr(UnionAll(z, UnionAll(x, UnionAll(y, Tuple{x,y,z})))) == "Tuple{a1, a2, a} where a2 where a1 where a" + @test repr(UnionAll(z, UnionAll(x, UnionAll(y, Tuple{x,y,z})))) == "Tuple{a1, a2, a} where {a, a1, a2}" end @testset "showarg" begin