Skip to content

Commit

Permalink
restrict shorter printing of wheres to gensym names
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Mar 18, 2021
1 parent 9472ed9 commit e593839
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
10 changes: 6 additions & 4 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ function make_typealias(@nospecialize(x::Type))
end
end

isgensym(s::Symbol) = '#' in string(s)

function show_can_elide(p::TypeVar, wheres::Vector, elide::Int, env::SimpleVector, skip::Int)
elide == 0 && return false
wheres[elide] === p || return false
Expand Down Expand Up @@ -612,9 +614,9 @@ function show_typeparams(io::IO, env::SimpleVector, orig::SimpleVector, wheres::
if i == n && egal_var(p, orig[i]) && show_can_elide(p, wheres, elide, env, i)
n -= 1
elide -= 1
elseif p.lb === Union{} && show_can_elide(p, wheres, elide, env, i)
elseif p.lb === Union{} && isgensym(p.name) && show_can_elide(p, wheres, elide, env, i)
elide -= 1
elseif p.ub === Any && show_can_elide(p, wheres, elide, env, i)
elseif p.ub === Any && isgensym(p.name) && show_can_elide(p, wheres, elide, env, i)
elide -= 1
end
end
Expand All @@ -624,10 +626,10 @@ function show_typeparams(io::IO, env::SimpleVector, orig::SimpleVector, wheres::
for i = 1:n
p = env[i]
if p isa TypeVar
if p.lb === Union{} && something(findfirst(w -> w === p, wheres), 0) > elide
if p.lb === Union{} && something(findfirst(@nospecialize(w) -> w === p, wheres), 0) > elide
print(io, "<:")
show(io, p.ub)
elseif p.ub === Any && something(findfirst(w -> w === p, wheres), 0) > elide
elseif p.ub === Any && something(findfirst(@nospecialize(w) -> w === p, wheres), 0) > elide
print(io, ">:")
show(io, p.lb)
else
Expand Down
2 changes: 1 addition & 1 deletion doc/src/manual/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ consider the two types created by the following declarations:

```jldoctest
julia> const T1 = Array{Array{T, 1} where T, 1}
Vector{Vector} (alias for Array{Array{<:Any, 1}, 1})
Vector{Vector} (alias for Array{Array{T, 1} where T, 1})
julia> const T2 = Array{Array{T, 1}, 1} where T
Array{Vector{T}, 1} where T
Expand Down
6 changes: 3 additions & 3 deletions test/docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1157,9 +1157,9 @@ No documentation found.
# Union Composed of Types
- `$(curmod_prefix)Undocumented.at1{<:Any}`
- `$(curmod_prefix)Undocumented.pt2{<:Any}`
- `$(curmod_prefix)Undocumented.st3{<:Any}`
- `$(curmod_prefix)Undocumented.at1{T} where T`
- `$(curmod_prefix)Undocumented.pt2{T} where T`
- `$(curmod_prefix)Undocumented.st3{T} where T`
- `$(curmod_prefix)Undocumented.st4`
""")
@test docstrings_equal(@doc(Undocumented.ut9), doc"$doc_str")
Expand Down
2 changes: 1 addition & 1 deletion test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ let err_str,
@test startswith(sprint(show, which(FunctionLike(), Tuple{})),
"(::$(curmod_prefix)FunctionLike)() in $curmod_str at $sp:$(method_defs_lineno + 7)")
@test startswith(sprint(show, which(StructWithUnionAllMethodDefs{<:Integer}, (Any,))),
"($(curmod_prefix)StructWithUnionAllMethodDefs{<:Integer})(x)")
"($(curmod_prefix)StructWithUnionAllMethodDefs{T} where T<:Integer)(x)")
@test repr("text/plain", FunctionLike()) == "(::$(curmod_prefix)FunctionLike) (generic function with 1 method)"
@test repr("text/plain", Core.arraysize) == "arraysize (built-in function)"

Expand Down
14 changes: 7 additions & 7 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,7 @@ end

let x = TypeVar(:_), y = TypeVar(:_)
@test repr(UnionAll(x, UnionAll(y, Pair{x,y}))) == "Pair"
@test repr(UnionAll(y, UnionAll(x, Pair{x,y}))) == "Pair{<:Any, _1} where _1"
@test repr(UnionAll(y, UnionAll(x, Pair{x,y}))) == "Pair{_2, _1} where {_1, _2}"
@test repr(UnionAll(x, UnionAll(y, Pair{UnionAll(x,Ref{x}),y}))) == "Pair{Ref}"
@test repr(UnionAll(y, UnionAll(x, Pair{UnionAll(y,Ref{x}),y}))) == "Pair{Ref{_2}, _1} where {_1, _2}"
end
Expand All @@ -1584,9 +1584,9 @@ let x, y, x
end

let x = TypeVar(:_, Number), y = TypeVar(:_, Number)
@test repr(UnionAll(x, UnionAll(y, Pair{x,y}))) == "Pair{<:Number, <:Number}"
@test repr(UnionAll(y, UnionAll(x, Pair{x,y}))) == "Pair{<:Number, _1} where _1<:Number"
@test repr(UnionAll(x, UnionAll(y, Pair{UnionAll(x,Ref{x}),y}))) == "Pair{Ref{<:Number}, <:Number}"
@test repr(UnionAll(x, UnionAll(y, Pair{x,y}))) == "Pair{_1, _2} where {_1<:Number, _2<:Number}"
@test repr(UnionAll(y, UnionAll(x, Pair{x,y}))) == "Pair{_2, _1} where {_1<:Number, _2<:Number}"
@test repr(UnionAll(x, UnionAll(y, Pair{UnionAll(x,Ref{x}),y}))) == "Pair{Ref{_1} where _1<:Number, _1} where _1<:Number"
@test repr(UnionAll(y, UnionAll(x, Pair{UnionAll(y,Ref{x}),y}))) == "Pair{Ref{_2}, _1} where {_1<:Number, _2<:Number}"
end

Expand Down Expand Up @@ -1771,7 +1771,7 @@ end
@test replstr([[1.0]=>1.0]) == "1-element Vector{Pair{Vector{Float64}, Float64}}:\n [1.0] => 1.0"

# issue #28159
@test replstr([(a=1, b=2), (a=3,c=4)]) == "2-element Vector{NamedTuple{<:Any, Tuple{$Int, $Int}}}:\n (a = 1, b = 2)\n (a = 3, c = 4)"
@test replstr([(a=1, b=2), (a=3,c=4)]) == "2-element Vector{NamedTuple{names, Tuple{$Int, $Int}} where names}:\n (a = 1, b = 2)\n (a = 3, c = 4)"

@test replstr(Vector[Any[1]]) == "1-element Vector{Vector}:\n Any[1]"
@test replstr(AbstractDict{Integer,Integer}[Dict{Integer,Integer}(1=>2)]) ==
Expand Down Expand Up @@ -2166,8 +2166,8 @@ end
@test string(Union{Nothing, AbstractVecOrMat}) == "Union{Nothing, AbstractVecOrMat}"
@test string(Union{Nothing, AbstractVecOrMat{<:Integer}}) == "Union{Nothing, AbstractVecOrMat{<:Integer}}"
@test string(M37012.BStruct{T, T} where T) == "$(curmod_prefix)M37012.B2{T, T} where T"
@test string(M37012.BStruct{T, S} where {T<:Unsigned, S<:Signed}) == "$(curmod_prefix)M37012.B2{<:Signed, T} where T<:Unsigned"
@test string(M37012.BStruct{T, S} where {T<:Signed, S<:T}) == "$(curmod_prefix)M37012.B2{<:T, T} where T<:Signed"
@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, $(curmod_prefix)M37012.SimpleU, T} where T"
@test string(Union{AbstractVector{T}, T} where T) == "Union{AbstractVector{T}, T} where T"
Expand Down

0 comments on commit e593839

Please sign in to comment.