Skip to content

Commit

Permalink
show: consolidate wheres with {} in printing
Browse files Browse the repository at this point in the history
Always a bit more compact in this form, and somewhat easier to implement
too (thus keeping this consistent with the corrected typealias printing).
  • Loading branch information
vtjnash committed Jan 22, 2021
1 parent b6df6c2 commit ee816ef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
36 changes: 22 additions & 14 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down
6 changes: 3 additions & 3 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ee816ef

Please sign in to comment.