Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix some cases of typeinfo printing #33290

Merged
merged 2 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix some cases of typeinfo printing
  • Loading branch information
JeffBezanson committed Sep 17, 2019
commit 95fbad6356895928801814acac5a0be806c0547b
15 changes: 13 additions & 2 deletions base/arrayshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,17 @@ typeinfo_eltype(typeinfo::Type{<:AbstractArray{T}}) where {T} = eltype(typeinfo)
typeinfo_eltype(typeinfo::Type{<:AbstractDict{K,V}}) where {K,V} = eltype(typeinfo)
typeinfo_eltype(typeinfo::Type{<:AbstractSet{T}}) where {T} = eltype(typeinfo)

# types that can be parsed back accurately from their un-decorated representations
function typeinfo_implicit(@nospecialize(T))
if T === Float64 || T === Int || T === Char || T === String || T === Symbol ||
issingletontype(T)
return true
end
return isconcretetype(T) &&
((T <: Array && typeinfo_implicit(eltype(T))) ||
((T <: Tuple || T <: Pair) && all(typeinfo_implicit, fieldtypes(T))) ||
(T <: AbstractDict && typeinfo_implicit(keytype(T)) && typeinfo_implicit(valtype(T))))
end

# X not constrained, can be any iterable (cf. show_vector)
function typeinfo_prefix(io::IO, X)
Expand All @@ -470,14 +481,14 @@ function typeinfo_prefix(io::IO, X)
eltype_X = eltype(X)

if X isa AbstractDict
if eltype_X == eltype_ctx || !isempty(X) && isconcretetype(keytype(X)) && isconcretetype(valtype(X))
if eltype_X == eltype_ctx || (!isempty(X) && typeinfo_implicit(keytype(X)) && typeinfo_implicit(valtype(X)))
string(typeof(X).name)
else
string(typeof(X))
end
else
# Types hard-coded here are those which are created by default for a given syntax
if eltype_X == eltype_ctx || !isempty(X) && eltype_X in (Float64, Int, Char, String)
if eltype_X == eltype_ctx || (!isempty(X) && typeinfo_implicit(eltype_X))
""
elseif print_without_params(eltype_X)
string(unwrap_unionall(eltype_X).name) # Print "Array" rather than "Array{T,N}"
Expand Down
2 changes: 1 addition & 1 deletion test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,7 @@ struct Foo2509; foo::Int; end
# issue #2517
struct Foo2517; end
@test repr(Foo2517()) == "$(curmod_prefix)Foo2517()"
@test repr(Vector{Foo2517}(undef, 1)) == "$(curmod_prefix)Foo2517[$(curmod_prefix)Foo2517()]"
@test repr(Vector{Foo2517}(undef, 1 )) == "[$(curmod_prefix)Foo2517()]"
JeffBezanson marked this conversation as resolved.
Show resolved Hide resolved
@test Foo2517() === Foo2517()

# issue #1474
Expand Down
2 changes: 1 addition & 1 deletion test/missing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ end
@testset "printing" begin
@test sprint(show, missing) == "missing"
@test sprint(show, missing, context=:compact => true) == "missing"
@test sprint(show, [missing]) == "$Missing[missing]"
@test sprint(show, [missing]) == "[missing]"
@test sprint(show, [1 missing]) == "$(Union{Int, Missing})[1 missing]"
b = IOBuffer()
display(TextDisplay(b), [missing])
Expand Down
14 changes: 8 additions & 6 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -787,8 +787,8 @@ let x = [], y = [], z = Base.ImmutableDict(x => y)
push!(x, y)
push!(y, x)
push!(y, z)
@test replstr(x) == "1-element Array{Any,1}:\n Any[Any[Any[#= circular reference @-2 =#]], Base.ImmutableDict([Any[#= circular reference @-3 =#]] => [#= circular reference @-2 =#])]"
@test repr(z) == "Base.ImmutableDict([Any[Any[#= circular reference @-2 =#], Base.ImmutableDict(#= circular reference @-3 =#)]] => [Any[Any[#= circular reference @-2 =#]], Base.ImmutableDict(#= circular reference @-2 =#)])"
@test replstr(x) == "1-element Array{Any,1}:\n Any[Any[Any[#= circular reference @-2 =#]], Base.ImmutableDict{Array{Any,1},Array{Any,1}}([Any[#= circular reference @-3 =#]] => [#= circular reference @-2 =#])]"
@test repr(z) == "Base.ImmutableDict{Array{Any,1},Array{Any,1}}([Any[Any[#= circular reference @-2 =#], Base.ImmutableDict{Array{Any,1},Array{Any,1}}(#= circular reference @-3 =#)]] => [Any[Any[#= circular reference @-2 =#]], Base.ImmutableDict{Array{Any,1},Array{Any,1}}(#= circular reference @-2 =#)])"
@test sprint(dump, x) == """
Array{Any}((1,))
1: Array{Any}((2,))
Expand Down Expand Up @@ -1058,7 +1058,7 @@ end

# issue #28327
d = Dict(Pair{Integer,Integer}(1,2)=>Pair{Integer,Integer}(1,2))
@test showstr(d) == "Dict((1 => 2) => (1 => 2))" # correct parenthesis
@test showstr(d) == "Dict{Pair{Integer,Integer},Pair{Integer,Integer}}((1 => 2) => (1 => 2))" # correct parenthesis

# issue #29536
d = Dict((+)=>1)
Expand Down Expand Up @@ -1263,8 +1263,8 @@ end

# issue #27680
@test replstr(Set([(1.0,1.0), (2.0,2.0), (3.0, 3.0)])) == (sizeof(Int) == 8 ?
"Set(Tuple{Float64,Float64}[(3.0, 3.0), (2.0, 2.0), (1.0, 1.0)])" :
"Set(Tuple{Float64,Float64}[(1.0, 1.0), (2.0, 2.0), (3.0, 3.0)])")
"Set([(3.0, 3.0), (2.0, 2.0), (1.0, 1.0)])" :
"Set([(1.0, 1.0), (2.0, 2.0), (3.0, 3.0)])")

# issue #27747
let t = (x = Integer[1, 2],)
Expand All @@ -1287,6 +1287,8 @@ end
@test showstr(Pair{Integer,Integer}(1, 2), :typeinfo => Pair{Integer,Integer}) == "1 => 2"
@test showstr([Pair{Integer,Integer}(1, 2)]) == "Pair{Integer,Integer}[1 => 2]"
@test showstr(Dict{Integer,Integer}(1 => 2)) == "Dict{Integer,Integer}(1 => 2)"
@test showstr(Dict(true=>false)) == "Dict{Bool,Bool}(1 => 0)"
@test showstr(Dict((1 => 2) => (3 => 4))) == "Dict((1 => 2) => (3 => 4))"

# issue #27979 (dislaying arrays of pairs containing arrays as first member)
@test replstr([[1.0]=>1.0]) == "1-element Array{Pair{Array{Float64,1},Float64},1}:\n [1.0] => 1.0"
Expand Down Expand Up @@ -1551,7 +1553,7 @@ Z = Array{Float64}(undef,0,0)
vec_undefined = Vector(undef, 2)
vec_initialisers = fill(undef, 2)
@test showstr(vec_undefined) == "Any[#undef, #undef]"
@test showstr(vec_initialisers) == "UndefInitializer[$undef, $undef]"
@test showstr(vec_initialisers) == "[$undef, $undef]"
@test replstr(vec_undefined) == "2-element Array{Any,1}:\n #undef\n #undef"
@test replstr(vec_initialisers) == "2-element Array{UndefInitializer,1}:\n $undef\n $undef"
end
Expand Down