Skip to content

Commit 9c6d749

Browse files
authored
fix some cases of typeinfo printing (#33290)
1 parent 6c2c940 commit 9c6d749

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

base/arrayshow.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,17 @@ typeinfo_eltype(typeinfo::Type{<:AbstractArray{T}}) where {T} = eltype(typeinfo)
457457
typeinfo_eltype(typeinfo::Type{<:AbstractDict{K,V}}) where {K,V} = eltype(typeinfo)
458458
typeinfo_eltype(typeinfo::Type{<:AbstractSet{T}}) where {T} = eltype(typeinfo)
459459

460+
# types that can be parsed back accurately from their un-decorated representations
461+
function typeinfo_implicit(@nospecialize(T))
462+
if T === Float64 || T === Int || T === Char || T === String || T === Symbol ||
463+
issingletontype(T)
464+
return true
465+
end
466+
return isconcretetype(T) &&
467+
((T <: Array && typeinfo_implicit(eltype(T))) ||
468+
((T <: Tuple || T <: Pair) && all(typeinfo_implicit, fieldtypes(T))) ||
469+
(T <: AbstractDict && typeinfo_implicit(keytype(T)) && typeinfo_implicit(valtype(T))))
470+
end
460471

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

472483
if X isa AbstractDict
473-
if eltype_X == eltype_ctx || !isempty(X) && isconcretetype(keytype(X)) && isconcretetype(valtype(X))
484+
if eltype_X == eltype_ctx || (!isempty(X) && typeinfo_implicit(keytype(X)) && typeinfo_implicit(valtype(X)))
474485
string(typeof(X).name)
475486
else
476487
string(typeof(X))
477488
end
478489
else
479490
# Types hard-coded here are those which are created by default for a given syntax
480-
if eltype_X == eltype_ctx || !isempty(X) && eltype_X in (Float64, Int, Char, String)
491+
if eltype_X == eltype_ctx || (!isempty(X) && typeinfo_implicit(eltype_X))
481492
""
482493
elseif print_without_params(eltype_X)
483494
string(unwrap_unionall(eltype_X).name) # Print "Array" rather than "Array{T,N}"

test/core.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,7 @@ struct Foo2509; foo::Int; end
14471447
# issue #2517
14481448
struct Foo2517; end
14491449
@test repr(Foo2517()) == "$(curmod_prefix)Foo2517()"
1450-
@test repr(Vector{Foo2517}(undef, 1)) == "$(curmod_prefix)Foo2517[$(curmod_prefix)Foo2517()]"
1450+
@test repr(Vector{Foo2517}(undef, 1)) == "[$(curmod_prefix)Foo2517()]"
14511451
@test Foo2517() === Foo2517()
14521452

14531453
# issue #1474

test/missing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ end
221221
@testset "printing" begin
222222
@test sprint(show, missing) == "missing"
223223
@test sprint(show, missing, context=:compact => true) == "missing"
224-
@test sprint(show, [missing]) == "$Missing[missing]"
224+
@test sprint(show, [missing]) == "[missing]"
225225
@test sprint(show, [1 missing]) == "$(Union{Int, Missing})[1 missing]"
226226
b = IOBuffer()
227227
display(TextDisplay(b), [missing])

test/show.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -787,8 +787,8 @@ let x = [], y = [], z = Base.ImmutableDict(x => y)
787787
push!(x, y)
788788
push!(y, x)
789789
push!(y, z)
790-
@test replstr(x) == "1-element Array{Any,1}:\n Any[Any[Any[#= circular reference @-2 =#]], Base.ImmutableDict([Any[#= circular reference @-3 =#]] => [#= circular reference @-2 =#])]"
791-
@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 =#)])"
790+
@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 =#])]"
791+
@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 =#)])"
792792
@test sprint(dump, x) == """
793793
Array{Any}((1,))
794794
1: Array{Any}((2,))
@@ -1058,7 +1058,7 @@ end
10581058

10591059
# issue #28327
10601060
d = Dict(Pair{Integer,Integer}(1,2)=>Pair{Integer,Integer}(1,2))
1061-
@test showstr(d) == "Dict((1 => 2) => (1 => 2))" # correct parenthesis
1061+
@test showstr(d) == "Dict{Pair{Integer,Integer},Pair{Integer,Integer}}((1 => 2) => (1 => 2))" # correct parenthesis
10621062

10631063
# issue #29536
10641064
d = Dict((+)=>1)
@@ -1263,8 +1263,8 @@ end
12631263

12641264
# issue #27680
12651265
@test replstr(Set([(1.0,1.0), (2.0,2.0), (3.0, 3.0)])) == (sizeof(Int) == 8 ?
1266-
"Set(Tuple{Float64,Float64}[(3.0, 3.0), (2.0, 2.0), (1.0, 1.0)])" :
1267-
"Set(Tuple{Float64,Float64}[(1.0, 1.0), (2.0, 2.0), (3.0, 3.0)])")
1266+
"Set([(3.0, 3.0), (2.0, 2.0), (1.0, 1.0)])" :
1267+
"Set([(1.0, 1.0), (2.0, 2.0), (3.0, 3.0)])")
12681268

12691269
# issue #27747
12701270
let t = (x = Integer[1, 2],)
@@ -1287,6 +1287,8 @@ end
12871287
@test showstr(Pair{Integer,Integer}(1, 2), :typeinfo => Pair{Integer,Integer}) == "1 => 2"
12881288
@test showstr([Pair{Integer,Integer}(1, 2)]) == "Pair{Integer,Integer}[1 => 2]"
12891289
@test showstr(Dict{Integer,Integer}(1 => 2)) == "Dict{Integer,Integer}(1 => 2)"
1290+
@test showstr(Dict(true=>false)) == "Dict{Bool,Bool}(1 => 0)"
1291+
@test showstr(Dict((1 => 2) => (3 => 4))) == "Dict((1 => 2) => (3 => 4))"
12901292

12911293
# issue #27979 (dislaying arrays of pairs containing arrays as first member)
12921294
@test replstr([[1.0]=>1.0]) == "1-element Array{Pair{Array{Float64,1},Float64},1}:\n [1.0] => 1.0"
@@ -1551,7 +1553,7 @@ Z = Array{Float64}(undef,0,0)
15511553
vec_undefined = Vector(undef, 2)
15521554
vec_initialisers = fill(undef, 2)
15531555
@test showstr(vec_undefined) == "Any[#undef, #undef]"
1554-
@test showstr(vec_initialisers) == "UndefInitializer[$undef, $undef]"
1556+
@test showstr(vec_initialisers) == "[$undef, $undef]"
15551557
@test replstr(vec_undefined) == "2-element Array{Any,1}:\n #undef\n #undef"
15561558
@test replstr(vec_initialisers) == "2-element Array{UndefInitializer,1}:\n $undef\n $undef"
15571559
end

0 commit comments

Comments
 (0)