Skip to content

Commit

Permalink
when showing union aliases, keep TypeVars last
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash authored and ElOceanografo committed May 4, 2021
1 parent dfddd66 commit a25040d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
27 changes: 20 additions & 7 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -749,16 +749,21 @@ end
function show_unionaliases(io::IO, x::Union)
properx = makeproper(io, x)
aliases, applied = make_typealiases(properx)
isempty(aliases) && return false
first = true
tvar = false
for typ in uniontypes(x)
if !isa(typ, TypeVar) && rewrap_unionall(typ, properx) <: applied
if isa(typ, TypeVar)
tvar = true # sort bare TypeVars to the end
continue
elseif rewrap_unionall(typ, properx) <: applied
continue
end
print(io, first ? "Union{" : ", ")
first = false
show(io, typ)
end
if first && length(aliases) == 1
if first && !tvar && length(aliases) == 1
alias = aliases[1]
wheres = make_wheres(io, alias[2], x)
show_typealias(io, alias[1], x, alias[2], wheres)
Expand All @@ -772,8 +777,17 @@ function show_unionaliases(io::IO, x::Union)
show_typealias(io, alias[1], x, alias[2], wheres)
show_wheres(io, wheres)
end
if tvar
for typ in uniontypes(x)
if isa(typ, TypeVar)
print(io, ", ")
show(io, typ)
end
end
end
print(io, "}")
end
return true
end

function show(io::IO, ::MIME"text/plain", @nospecialize(x::Type))
Expand Down Expand Up @@ -813,12 +827,11 @@ function _show_type(io::IO, @nospecialize(x::Type))
show_datatype(io, x)
return
elseif x isa Union
if get(io, :compact, true)
show_unionaliases(io, x)
else
print(io, "Union")
show_delim_array(io, uniontypes(x), '{', ',', '}', false)
if get(io, :compact, true) && show_unionaliases(io, x)
return
end
print(io, "Union")
show_delim_array(io, uniontypes(x), '{', ',', '}', false)
return
end

Expand Down
4 changes: 2 additions & 2 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2088,9 +2088,9 @@ end
@test string(M37012.BStruct{T, S} where {T<:Unsigned, S<:Signed}) == "$(curmod_prefix)M37012.B2{S, T} where {T<:Unsigned, S<:Signed}"
@test string(M37012.BStruct{T, S} where {T<:Signed, S<:T}) == "$(curmod_prefix)M37012.B2{S, T} where {T<:Signed, S<:T}"
@test string(Union{M37012.SimpleU, Nothing}) == "Union{Nothing, $(curmod_prefix)M37012.SimpleU}"
@test string(Union{M37012.SimpleU, Nothing, T} where T) == "Union{Nothing, T, $(curmod_prefix)M37012.SimpleU} where T"
@test string(Union{M37012.SimpleU, Nothing, T} where T) == "Union{Nothing, $(curmod_prefix)M37012.SimpleU, T} where T"
@test string(Union{AbstractVector{T}, T} where T) == "Union{AbstractVector{T}, T} where T"
@test string(Union{AbstractVector, T} where T) == "Union{T, AbstractVector{T} where T} where T"
@test string(Union{AbstractVector, T} where T) == "Union{AbstractVector{T} where T, T} where T"

@test sprint(show, :(./)) == ":((./))"
@test sprint(show, :((.|).(.&, b))) == ":((.|).((.&), b))"
Expand Down

0 comments on commit a25040d

Please sign in to comment.