Skip to content

Commit 86f5b21

Browse files
Make dump print const before const fields (#53492)
This provides more information to the user when dumping types, and also makes the output of dump slightly more similar to the type definition syntax. EDIT: This has been changed to print: * The kind of type before the type name `abstract type`, `mutable struct`, etc. * `const` only for `const` fields of `mutable struct` New behaviour ``` julia> dump(Float32) primitive type Float32 <: AbstractFloat julia> dump(Signed) abstract type Signed <: Integer julia> dump(Pair{Int, String}) struct Pair{Int64, String} <: Any first::Int64 second::String julia> dump(BitSet) mutable struct BitSet <: AbstractSet{Int64} const bits::Vector{UInt64} offset::Int64 julia> dump(Set) UnionAll var: TypeVar name: Symbol T lb: Union{} ub: abstract type Any body: struct Set{T} <: AbstractSet{T} dict::Dict{T, Nothing} ``` --------- Co-authored-by: Shuhei Kadowaki <[email protected]>
1 parent e3b2462 commit 86f5b21

File tree

3 files changed

+70
-61
lines changed

3 files changed

+70
-61
lines changed

base/show.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2986,6 +2986,13 @@ end
29862986

29872987
# Types
29882988
function dump(io::IOContext, x::DataType, n::Int, indent)
2989+
# For some reason, tuples are structs
2990+
is_struct = isstructtype(x) && !(x <: Tuple)
2991+
is_mut = is_struct && ismutabletype(x)
2992+
is_mut && print(io, "mutable ")
2993+
is_struct && print(io, "struct ")
2994+
isprimitivetype(x) && print(io, "primitive type ")
2995+
isabstracttype(x) && print(io, "abstract type ")
29892996
print(io, x)
29902997
if x !== Any
29912998
print(io, " <: ", supertype(x))
@@ -3007,7 +3014,9 @@ function dump(io::IOContext, x::DataType, n::Int, indent)
30073014
fieldtypes = datatype_fieldtypes(x)
30083015
for idx in 1:length(fields)
30093016
println(io)
3010-
print(io, indent, " ", fields[idx])
3017+
print(io, indent, " ")
3018+
is_mut && isconst(x, idx) && print(io, "const ")
3019+
print(io, fields[idx])
30113020
if isassigned(fieldtypes, idx)
30123021
print(io, "::")
30133022
print(tvar_io, fieldtypes[idx])

doc/src/devdocs/types.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ UnionAll
9393
var: TypeVar
9494
name: Symbol T
9595
lb: Union{}
96-
ub: Any
96+
ub: abstract type Any
9797
body: UnionAll
9898
var: TypeVar
9999
name: Symbol N
100100
lb: Union{}
101-
ub: Any
102-
body: Array{T, N} <: DenseArray{T, N}
101+
ub: abstract type Any
102+
body: mutable struct Array{T, N} <: DenseArray{T, N}
103103
ref::MemoryRef{T}
104104
size::NTuple{N, Int64}
105105
```
@@ -181,13 +181,13 @@ TypeName
181181
var: TypeVar
182182
name: Symbol T
183183
lb: Union{}
184-
ub: Any
184+
ub: abstract type Any
185185
body: UnionAll
186186
var: TypeVar
187187
name: Symbol N
188188
lb: Union{}
189-
ub: Any
190-
body: Array{T, N} <: DenseArray{T, N}
189+
ub: abstract type Any
190+
body: mutable struct Array{T, N} <: DenseArray{T, N}
191191
cache: SimpleVector
192192
...
193193

test/show.jl

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ let oldout = stdout, olderr = stderr
703703
redirect_stderr(olderr)
704704
close(wrout)
705705
close(wrerr)
706-
@test fetch(out) == "Int64 <: Signed\nTESTA\nTESTB\nΑ1Β2\"A\"\nA\n123\"C\"\n"
706+
@test fetch(out) == "primitive type Int64 <: Signed\nTESTA\nTESTB\nΑ1Β2\"A\"\nA\n123\"C\"\n"
707707
@test fetch(err) == "TESTA\nTESTB\nΑ1Β2\"A\"\n"
708708
finally
709709
redirect_stdout(oldout)
@@ -1259,64 +1259,64 @@ end
12591259
close(s2)
12601260
end
12611261

1262-
let repr = sprint(dump, :(x = 1))
1263-
@test repr == "Expr\n head: Symbol =\n args: Array{Any}((2,))\n 1: Symbol x\n 2: $Int 1\n"
1264-
end
1265-
let repr = sprint(dump, Pair{String,Int64})
1266-
@test repr == "Pair{String, Int64} <: Any\n first::String\n second::Int64\n"
1267-
end
1268-
let repr = sprint(dump, Tuple)
1269-
@test repr == "Tuple <: Any\n"
1270-
end
1271-
let repr = sprint(dump, Int64)
1272-
@test repr == "Int64 <: Signed\n"
1273-
end
1274-
let repr = sprint(dump, Any)
1275-
@test length(repr) == 4
1276-
@test occursin(r"^Any\n", repr)
1277-
@test endswith(repr, '\n')
1278-
end
1279-
let repr = sprint(dump, Integer)
1280-
@test occursin("Integer <: Real", repr)
1281-
@test !occursin("Any", repr)
1282-
end
1283-
let repr = sprint(dump, Union{Integer, Float32})
1284-
@test repr == "Union{Integer, Float32}\n" || repr == "Union{Float32, Integer}\n"
1285-
end
12861262
module M30442
12871263
struct T end
12881264
end
1289-
let repr = sprint(show, Union{String, M30442.T})
1290-
@test repr == "Union{$(curmod_prefix)M30442.T, String}" ||
1291-
repr == "Union{String, $(curmod_prefix)M30442.T}"
1292-
end
1293-
let repr = sprint(dump, Ptr{UInt8}(UInt(1)))
1294-
@test repr == "Ptr{UInt8} @$(Base.repr(UInt(1)))\n"
1295-
end
1296-
let repr = sprint(dump, Core.svec())
1297-
@test repr == "empty SimpleVector\n"
1298-
end
1299-
let repr = sprint(dump, sin)
1300-
@test repr == "sin (function of type typeof(sin))\n"
1301-
end
1302-
let repr = sprint(dump, Test)
1303-
@test repr == "Module Test\n"
1304-
end
1305-
let repr = sprint(dump, nothing)
1306-
@test repr == "Nothing nothing\n"
1307-
end
1308-
let a = Vector{Any}(undef, 10000)
1309-
a[2] = "elemA"
1310-
a[4] = "elemB"
1311-
a[11] = "elemC"
1312-
repr = sprint(dump, a; context=(:limit => true), sizehint=0)
1313-
@test repr == "Array{Any}((10000,))\n 1: #undef\n 2: String \"elemA\"\n 3: #undef\n 4: String \"elemB\"\n 5: #undef\n ...\n 9996: #undef\n 9997: #undef\n 9998: #undef\n 9999: #undef\n 10000: #undef\n"
1314-
end
1315-
@test occursin("NamedTuple", sprint(dump, NamedTuple))
1265+
@testset "Dump types" begin
1266+
let repr = sprint(dump, :(x = 1))
1267+
@test repr == "Expr\n head: Symbol =\n args: Array{Any}((2,))\n 1: Symbol x\n 2: $Int 1\n"
1268+
end
1269+
let repr = sprint(dump, Pair{String,Int64})
1270+
@test repr == "struct Pair{String, Int64} <: Any\n first::String\n second::Int64\n"
1271+
end
1272+
let repr = sprint(dump, Tuple)
1273+
@test repr == "Tuple <: Any\n"
1274+
end
1275+
let repr = sprint(dump, Int64)
1276+
@test repr == "primitive type Int64 <: Signed\n"
1277+
end
1278+
let repr = sprint(dump, Any)
1279+
@test repr == "abstract type Any\n"
1280+
end
1281+
let repr = sprint(dump, Integer)
1282+
@test occursin("abstract type Integer <: Real", repr)
1283+
@test !occursin("Any", repr)
1284+
end
1285+
let repr = sprint(dump, Union{Integer, Float32})
1286+
@test repr == "Union{Integer, Float32}\n" || repr == "Union{Float32, Integer}\n"
1287+
end
13161288

1317-
# issue 36495, dumping a partial NamedTupled shouldn't error
1318-
@test occursin("NamedTuple", sprint(dump, NamedTuple{(:foo,:bar)}))
1289+
let repr = sprint(show, Union{String, M30442.T})
1290+
@test repr == "Union{$(curmod_prefix)M30442.T, String}" ||
1291+
repr == "Union{String, $(curmod_prefix)M30442.T}"
1292+
end
1293+
let repr = sprint(dump, Ptr{UInt8}(UInt(1)))
1294+
@test repr == "Ptr{UInt8} @$(Base.repr(UInt(1)))\n"
1295+
end
1296+
let repr = sprint(dump, Core.svec())
1297+
@test repr == "empty SimpleVector\n"
1298+
end
1299+
let repr = sprint(dump, sin)
1300+
@test repr == "sin (function of type typeof(sin))\n"
1301+
end
1302+
let repr = sprint(dump, Test)
1303+
@test repr == "Module Test\n"
1304+
end
1305+
let repr = sprint(dump, nothing)
1306+
@test repr == "Nothing nothing\n"
1307+
end
1308+
let a = Vector{Any}(undef, 10000)
1309+
a[2] = "elemA"
1310+
a[4] = "elemB"
1311+
a[11] = "elemC"
1312+
repr = sprint(dump, a; context=(:limit => true), sizehint=0)
1313+
@test repr == "Array{Any}((10000,))\n 1: #undef\n 2: String \"elemA\"\n 3: #undef\n 4: String \"elemB\"\n 5: #undef\n ...\n 9996: #undef\n 9997: #undef\n 9998: #undef\n 9999: #undef\n 10000: #undef\n"
1314+
end
1315+
@test occursin("NamedTuple", sprint(dump, NamedTuple))
13191316

1317+
# issue 36495, dumping a partial NamedTupled shouldn't error
1318+
@test occursin("NamedTuple", sprint(dump, NamedTuple{(:foo,:bar)}))
1319+
end
13201320
# issue #17338
13211321
@test repr(Core.svec(1, 2)) == "svec(1, 2)"
13221322

0 commit comments

Comments
 (0)