Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 18 additions & 2 deletions src/rtutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1348,16 +1348,29 @@ 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");
}
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;
Expand Down Expand Up @@ -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, "<?#%p::", (void*)v);
Expand Down
7 changes: 5 additions & 2 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,7 @@ function static_shown(x)
end

# Test for PR 17803
@test static_shown(Int128(-1)) == "Int128(0xffffffffffffffffffffffffffffffff)"
@test static_shown(Int128(-1)) == "Base.reinterpret(Int128, 0xffffffffffffffffffffffffffffffff)"

# PR #22160
@test static_shown(:aa) == ":aa"
Expand Down Expand Up @@ -1618,9 +1618,12 @@ struct var"%X%" end # Invalid name without '#'
Val(1), Val(Int8(1)), Val(Int16(1)), Val(Int32(1)), Val(Int64(1)), Val(Int128(1)),
Val(UInt(1)), Val(UInt8(1)), Val(UInt16(1)), Val(UInt32(1)), Val(UInt64(1)), Val(UInt128(1)),

# Primitive types should round-trip via reinterpret(...)
Val(Char('u')), Val(Char('\0')),

# BROKEN
# Symbol("a\xffb"),
# User-defined primitive types
# User-defined primitive types with size not a power-of-two
# Non-canonical NaNs
# BFloat16
)
Expand Down