Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial fix for invalid syntax in emitted precompile statements #28808 #32610

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion contrib/generate_precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ function generate_precompile_statements()
include_time = @elapsed for statement in sort(collect(statements))
# println(statement)
# Workarounds for #28808
occursin("\"YYYY-mm-dd\\THH:MM:SS\"", statement) && continue
statement == "precompile(Tuple{typeof(Base.show), Base.IOContext{Base.TTY}, Type{Vararg{Any, N} where N}})" && continue
# check for `#x##s66` style variable names not in quotes
occursin(r"#\w", statement) &&
Expand Down
48 changes: 33 additions & 15 deletions src/rtutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,14 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt
n += jl_printf(out, "Symbol(\"");
else
n += jl_printf(out, ":");
n += jl_printf(out, "%s", sn);
char buf[64];
size_t i = 0, ninc = 0, sz = strlen(sn);
while (i < sz) {
ninc = u8_escape(buf, sizeof(buf), sn, &i, sz, 1, 0) - 1;
n += ninc;
jl_uv_puts(out, buf, ninc);
}
n += sz;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the n += ninc inside the loop sufficient to count the amount of output?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also would be good to use this code for showing strings as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like a good thing to put in a helper function

if (quoted)
n += jl_printf(out, "\")");
}
Expand Down Expand Up @@ -968,22 +975,33 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt
else if (jl_datatype_type && jl_is_datatype(vt)) {
int istuple = jl_is_tuple_type(vt), isnamedtuple = jl_is_namedtuple_type(vt);
size_t tlen = jl_datatype_nfields(vt);
if (isnamedtuple) {
if (tlen == 0)
n += jl_printf(out, "NamedTuple");
}
else if (!istuple) {
n += jl_static_show_x(out, (jl_value_t*)vt, depth);
}
n += jl_printf(out, "(");
size_t nb = jl_datatype_size(vt);
if (nb > 0 && tlen == 0) {
uint8_t *data = (uint8_t*)v;
n += jl_printf(out, "0x");
for(int i = nb - 1; i >= 0; --i)
n += jl_printf(out, "%02" PRIx8, data[i]);
}
else {
n += jl_printf(out, "reinterpret(");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
n += jl_printf(out, "reinterpret(");
n += jl_printf(out, "bitcast(");

n += jl_static_show_x(out, (jl_value_t*)vt, depth);
n += jl_printf(out, ", 0x");
uint32_t a = 0x01020304;
uint8_t *endian_bom = (uint8_t*) &a;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is undefined behavior (since it is observably different on different platforms). use the standard macro (BYTE_ORDER)

if (*endian_bom == 0x01) { // big endian
for(int i = 0; i < nb; ++i)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for(int i = 0; i < nb; ++i)
for (int i = 0; i < nb; ++i)

n += jl_printf(out, "%02" PRIx8, data[i]);
} else if (*endian_bom == 0x04) { // little endian
for(int i = nb - 1; i >= 0; --i)
n += jl_printf(out, "%02" PRIx8, data[i]);
} else {
assert(0); // unsupported endianness
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a compile-time error

}
n += jl_printf(out, ")");
} else {
if (isnamedtuple) {
if (tlen == 0)
n += jl_printf(out, "NamedTuple");
}
else if (!istuple) {
n += jl_static_show_x(out, (jl_value_t*)vt, depth);
}
n += jl_printf(out, "(");
size_t i = 0;
if (vt == jl_typemap_entry_type)
i = 1;
Expand Down Expand Up @@ -1014,8 +1032,8 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt
n += jl_printf(out, ", next=↩︎\n ");
n += jl_static_show_next_(out, (jl_value_t*)((jl_typemap_entry_t*)v)->next, v, depth);
}
n += jl_printf(out, ")");
}
n += jl_printf(out, ")");
}
else {
n += jl_printf(out, "<?#%p::", (void*)v);
Expand Down
2 changes: 1 addition & 1 deletion test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ function static_shown(x)
end

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

# PR #22160
@test static_shown(:aa) == ":aa"
Expand Down