Skip to content

Commit

Permalink
fix #34343, missing type prefix in display of some nested containers (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Mar 17, 2020
1 parent 0c2b739 commit aa977cf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
32 changes: 21 additions & 11 deletions base/arrayshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,10 @@ _show_empty(io, X) = nothing # by default, we don't know this constructor
function show(io::IO, X::AbstractArray)
ndims(X) == 0 && return show_zero_dim(io, X)
ndims(X) == 1 && return show_vector(io, X)
prefix = typeinfo_prefix(io, X)
io = IOContext(io, :typeinfo => eltype(X))
prefix, implicit = typeinfo_prefix(io, X)
if !implicit
io = IOContext(io, :typeinfo => eltype(X))
end
isempty(X) ?
_show_empty(io, X) :
_show_nonempty(io, X, prefix)
Expand All @@ -453,9 +455,12 @@ end
# NOTE: v is not constrained to be a vector, as this function can work with iterables
# in general (it's used e.g. by show(::IO, ::Set))
function show_vector(io::IO, v, opn='[', cls=']')
print(io, typeinfo_prefix(io, v))
prefix, implicit = typeinfo_prefix(io, v)
print(io, prefix)
# directly or indirectly, the context now knows about eltype(v)
io = IOContext(io, :typeinfo => eltype(v))
if !implicit
io = IOContext(io, :typeinfo => eltype(v))
end
limited = get(io, :limit, false)

if limited && length(v) > 20
Expand Down Expand Up @@ -497,6 +502,7 @@ end
# X not constrained, can be any iterable (cf. show_vector)
function typeinfo_prefix(io::IO, X)
typeinfo = get(io, :typeinfo, Any)::Type

if !(X isa typeinfo)
typeinfo = Any
end
Expand All @@ -506,19 +512,23 @@ function typeinfo_prefix(io::IO, X)
eltype_X = eltype(X)

if X isa AbstractDict
if eltype_X == eltype_ctx || (!isempty(X) && typeinfo_implicit(keytype(X)) && typeinfo_implicit(valtype(X)))
string(typeof(X).name)
if eltype_X == eltype_ctx
string(typeof(X).name), false
elseif !isempty(X) && typeinfo_implicit(keytype(X)) && typeinfo_implicit(valtype(X))
string(typeof(X).name), true
else
string(typeof(X))
string(typeof(X)), false
end
else
# Types hard-coded here are those which are created by default for a given syntax
if eltype_X == eltype_ctx || (!isempty(X) && typeinfo_implicit(eltype_X))
""
if eltype_X == eltype_ctx
"", false
elseif !isempty(X) && typeinfo_implicit(eltype_X)
"", true
elseif print_without_params(eltype_X)
string(unwrap_unionall(eltype_X).name) # Print "Array" rather than "Array{T,N}"
string(unwrap_unionall(eltype_X).name), false # Print "Array" rather than "Array{T,N}"
else
string(eltype_X)
string(eltype_X), false
end
end
end
2 changes: 1 addition & 1 deletion base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function show(io::IO, t::AbstractDict{K,V}) where V where K

limit::Bool = get(io, :limit, false)
# show in a Julia-syntax-like form: Dict(k=>v, ...)
print(io, typeinfo_prefix(io, t))
print(io, typeinfo_prefix(io, t)[1])
print(io, '(')
if !isempty(t) && !show_circular(io, t)
first = true
Expand Down
4 changes: 4 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,10 @@ end
@test replstr(Vector[Any[1]]) == "1-element Array{Array{T,1} where T,1}:\n Any[1]"
@test replstr(AbstractDict{Integer,Integer}[Dict{Integer,Integer}(1=>2)]) ==
"1-element Array{AbstractDict{Integer,Integer},1}:\n Dict(1 => 2)"

# issue #34343
@test showstr([[1], Int[]]) == "[[1], $Int[]]"
@test showstr([Dict(1=>1), Dict{Int,Int}()]) == "[Dict(1 => 1), Dict{$Int,$Int}()]"
end

@testset "#14684: `display` should print associative types in full" begin
Expand Down

0 comments on commit aa977cf

Please sign in to comment.