Skip to content

Commit a7700cd

Browse files
committed
static_show: Print primitive types with reinterpret(...)
1 parent 1ea959a commit a7700cd

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/rtutils.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,16 +1348,29 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt
13481348
// typeof(v) isa DataType, so v is an *instance of* a type that is a Datatype,
13491349
// meaning v is e.g. an instance of a struct. These are printed as a call to a
13501350
// type constructor, such as e.g. `Base.UnitRange{Int64}(start=1, stop=2)`
1351+
13511352
int istuple = jl_is_tuple_type(vt), isnamedtuple = jl_is_namedtuple_type(vt);
1353+
int isprimitivetype = jl_is_primitivetype(vt);
1354+
1355+
// In many cases, default constructors may not be available (e.g. primitive types
1356+
// never have them), so print these as "reinterpret(Foo, ...)" to make sure that
1357+
// these round-trip as expected.
1358+
int reinterpret = isprimitivetype;
1359+
13521360
size_t tlen = jl_datatype_nfields(vt);
1361+
if (reinterpret)
1362+
n += jl_printf(out, "reinterpret(");
13531363
if (isnamedtuple) {
13541364
if (tlen == 0)
13551365
n += jl_printf(out, "NamedTuple");
13561366
}
13571367
else if (!istuple) {
13581368
n += jl_static_show_x(out, (jl_value_t*)vt, depth, ctx);
13591369
}
1360-
n += jl_printf(out, "(");
1370+
if (reinterpret)
1371+
n += jl_printf(out, ", ");
1372+
if (!isprimitivetype)
1373+
n += jl_printf(out, "(");
13611374
size_t nb = jl_datatype_size(vt);
13621375
if (nb > 0 && tlen == 0) {
13631376
uint8_t *data = (uint8_t*)v;
@@ -1405,7 +1418,10 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt
14051418
n += jl_static_show_next_(out, next, v, depth, ctx);
14061419
}
14071420
}
1408-
n += jl_printf(out, ")");
1421+
if (!isprimitivetype)
1422+
n += jl_printf(out, ")");
1423+
if (reinterpret)
1424+
n += jl_printf(out, ")");
14091425
}
14101426
else {
14111427
n += jl_printf(out, "<?#%p::", (void*)v);

test/show.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1618,9 +1618,12 @@ struct var"%X%" end # Invalid name without '#'
16181618
Val(1), Val(Int8(1)), Val(Int16(1)), Val(Int32(1)), Val(Int64(1)), Val(Int128(1)),
16191619
Val(UInt(1)), Val(UInt8(1)), Val(UInt16(1)), Val(UInt32(1)), Val(UInt64(1)), Val(UInt128(1)),
16201620

1621+
# Primitive types should round-trip via reinterpret(...)
1622+
Val(Char('u')), Val(Char('\0')),
1623+
16211624
# BROKEN
16221625
# Symbol("a\xffb"),
1623-
# User-defined primitive types
1626+
# User-defined primitive types with size not a power-of-two
16241627
# Non-canonical NaNs
16251628
# BFloat16
16261629
)

0 commit comments

Comments
 (0)