From 3de7e152d7b6e62b8b2063287c90f76f0f348cdd Mon Sep 17 00:00:00 2001 From: Cody Tapscott Date: Tue, 3 Jun 2025 15:49:47 -0400 Subject: [PATCH] static_show: Print `primitive` types with `Base.reinterpret(...)` --- src/rtutils.c | 20 ++++++++++++++++++-- test/show.jl | 7 +++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/rtutils.c b/src/rtutils.c index e3672ab5d887e..41342e2f2ce2d 100644 --- a/src/rtutils.c +++ b/src/rtutils.c @@ -1348,8 +1348,18 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt // typeof(v) isa DataType, so v is an *instance of* a type that is a Datatype, // meaning v is e.g. an instance of a struct. These are printed as a call to a // type constructor, such as e.g. `Base.UnitRange{Int64}(start=1, stop=2)` + int istuple = jl_is_tuple_type(vt), isnamedtuple = jl_is_namedtuple_type(vt); + int isprimitivetype = jl_is_primitivetype(vt); + + // In many cases, default constructors may not be available (e.g. primitive types + // never have them), so print these as "reinterpret(Foo, ...)" to make sure that + // these round-trip as expected. + int reinterpret = isprimitivetype; + size_t tlen = jl_datatype_nfields(vt); + if (reinterpret) + n += jl_printf(out, "Base.reinterpret("); if (isnamedtuple) { if (tlen == 0) n += jl_printf(out, "NamedTuple"); @@ -1357,7 +1367,10 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt else if (!istuple) { n += jl_static_show_x(out, (jl_value_t*)vt, depth, ctx); } - n += jl_printf(out, "("); + if (reinterpret) + n += jl_printf(out, ", "); + if (!isprimitivetype) + n += jl_printf(out, "("); size_t nb = jl_datatype_size(vt); if (nb > 0 && tlen == 0) { 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 n += jl_static_show_next_(out, next, v, depth, ctx); } } - n += jl_printf(out, ")"); + if (!isprimitivetype) + n += jl_printf(out, ")"); + if (reinterpret) + n += jl_printf(out, ")"); } else { n += jl_printf(out, "