Skip to content

Commit 090331a

Browse files
kpamnanyvtjnashxal-0
authored
Improvements to static-show (#239)
* static-show: improve accuracy of some printings (JuliaLang#52799) - Show strings with escaping, rather than trying to output the text unmodified. - Show symbols with the same formatting as Strings - Avoid accidentally defining a broken Core.show method for NamedTuple * Make more types jl_static_show unambiguously (JuliaLang#58512) Makes more types survive `jl_static_show` unambiguously: - Symbols - Symbols printed in the `:var"foo"` form use raw string escaping, fixing `:var"a\b"`, `:var"a\\"`, `:var"$a"`, etc. - Symbols that require parens use parens (`:(=)`, ...) - Signed integers: Except for `Int`, signed integers print like `Int8(1)`. - Floats: floats are printed in a naive but reversible (TODO: double check) way. `Inf(16|32|)` and `NaN(16|32|)` are printed, and `Float16`/`Float32` print the type (`Float32(1.5)`). `Float64`s are printed with a trailing `.0` if it is necessary to disambiguate from `Int`. Fixes JuliaLang#52677, JuliaLang#58484 (comment), JuliaLang#58484 (comment), and the specific case mentioned in JuliaLang#58484. Improves the situation for round-trip (inexhaustive list): - Non-canonical NaNs - BFloat16 - User-defined primitive types. This one is tricky, because they can have a size different from any type we have literals for. * Use `julia__gnu_h2f_ieee` instead of `julia_half_to_float` `julia_half_to_float` came in with an LLVM version upgrade after v1.10. --------- Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Sam Schweigel <[email protected]>
1 parent 010d301 commit 090331a

File tree

10 files changed

+272
-87
lines changed

10 files changed

+272
-87
lines changed

base/namedtuple.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,8 @@ function convert(::Type{NT}, nt::NamedTuple{names}) where {names, T<:Tuple, NT<:
197197
end
198198

199199
if nameof(@__MODULE__) === :Base
200-
Tuple(nt::NamedTuple) = (nt...,)
201-
(::Type{T})(nt::NamedTuple) where {T <: Tuple} = (t = Tuple(nt); t isa T ? t : convert(T, t)::T)
202-
end
200+
Tuple(nt::NamedTuple) = (nt...,)
201+
(::Type{T})(nt::NamedTuple) where {T <: Tuple} = (t = Tuple(nt); t isa T ? t : convert(T, t)::T)
203202

204203
function show(io::IO, t::NamedTuple)
205204
n = nfields(t)
@@ -233,6 +232,7 @@ function show(io::IO, t::NamedTuple)
233232
print(io, ")")
234233
end
235234
end
235+
end
236236

237237
eltype(::Type{T}) where T<:NamedTuple = nteltype(T)
238238
nteltype(::Type) = Any

base/show.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,7 @@ function show_sym(io::IO, sym::Symbol; allow_macroname=false)
17441744
print(io, '@')
17451745
show_sym(io, Symbol(sym_str[2:end]))
17461746
else
1747-
print(io, "var", repr(string(sym)))
1747+
print(io, "var", repr(string(sym))) # TODO: this is not quite right, since repr uses String escaping rules, and Symbol uses raw string rules
17481748
end
17491749
end
17501750

src/ast.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ JL_DLLEXPORT jl_value_t *jl_copy_ast(jl_value_t *expr)
978978
return expr;
979979
}
980980

981-
JL_DLLEXPORT int jl_is_operator(char *sym)
981+
JL_DLLEXPORT int jl_is_operator(const char *sym)
982982
{
983983
jl_ast_context_t *ctx = jl_ast_ctx_enter(NULL);
984984
fl_context_t *fl_ctx = &ctx->fl;
@@ -987,7 +987,7 @@ JL_DLLEXPORT int jl_is_operator(char *sym)
987987
return res;
988988
}
989989

990-
JL_DLLEXPORT int jl_is_unary_operator(char *sym)
990+
JL_DLLEXPORT int jl_is_unary_operator(const char *sym)
991991
{
992992
jl_ast_context_t *ctx = jl_ast_ctx_enter(NULL);
993993
fl_context_t *fl_ctx = &ctx->fl;
@@ -996,7 +996,7 @@ JL_DLLEXPORT int jl_is_unary_operator(char *sym)
996996
return res;
997997
}
998998

999-
JL_DLLEXPORT int jl_is_unary_and_binary_operator(char *sym)
999+
JL_DLLEXPORT int jl_is_unary_and_binary_operator(const char *sym)
10001000
{
10011001
jl_ast_context_t *ctx = jl_ast_ctx_enter(NULL);
10021002
fl_context_t *fl_ctx = &ctx->fl;
@@ -1005,7 +1005,7 @@ JL_DLLEXPORT int jl_is_unary_and_binary_operator(char *sym)
10051005
return res;
10061006
}
10071007

1008-
JL_DLLEXPORT int jl_is_syntactic_operator(char *sym)
1008+
JL_DLLEXPORT int jl_is_syntactic_operator(const char *sym)
10091009
{
10101010
jl_ast_context_t *ctx = jl_ast_ctx_enter(NULL);
10111011
fl_context_t *fl_ctx = &ctx->fl;
@@ -1014,7 +1014,7 @@ JL_DLLEXPORT int jl_is_syntactic_operator(char *sym)
10141014
return res;
10151015
}
10161016

1017-
JL_DLLEXPORT int jl_operator_precedence(char *sym)
1017+
JL_DLLEXPORT int jl_operator_precedence(const char *sym)
10181018
{
10191019
jl_ast_context_t *ctx = jl_ast_ctx_enter(NULL);
10201020
fl_context_t *fl_ctx = &ctx->fl;

src/flisp/print.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ static void print_string(fl_context_t *fl_ctx, ios_t *f, char *str, size_t sz)
518518
}
519519
else {
520520
while (i < sz) {
521-
size_t n = u8_escape(buf, sizeof(buf), str, &i, sz, 1, 0);
521+
size_t n = u8_escape(buf, sizeof(buf), str, &i, sz, "\"", 0);
522522
outsn(fl_ctx, buf, f, n-1);
523523
}
524524
}

src/julia.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,11 +1961,11 @@ JL_DLLEXPORT jl_array_t *jl_uncompress_argnames(jl_value_t *syms);
19611961
JL_DLLEXPORT jl_value_t *jl_uncompress_argname_n(jl_value_t *syms, size_t i);
19621962

19631963

1964-
JL_DLLEXPORT int jl_is_operator(char *sym);
1965-
JL_DLLEXPORT int jl_is_unary_operator(char *sym);
1966-
JL_DLLEXPORT int jl_is_unary_and_binary_operator(char *sym);
1967-
JL_DLLEXPORT int jl_is_syntactic_operator(char *sym);
1968-
JL_DLLEXPORT int jl_operator_precedence(char *sym);
1964+
JL_DLLEXPORT int jl_is_operator(const char *sym);
1965+
JL_DLLEXPORT int jl_is_unary_operator(const char *sym);
1966+
JL_DLLEXPORT int jl_is_unary_and_binary_operator(const char *sym);
1967+
JL_DLLEXPORT int jl_is_syntactic_operator(const char *sym);
1968+
JL_DLLEXPORT int jl_operator_precedence(const char *sym);
19691969

19701970
STATIC_INLINE int jl_vinfo_sa(uint8_t vi)
19711971
{

0 commit comments

Comments
 (0)