From 68758b7e8e88f1454d96a20190af66c2653036ab Mon Sep 17 00:00:00 2001 From: Nathan Daly Date: Thu, 15 Oct 2020 23:49:31 -0400 Subject: [PATCH] Extend `static_show()` escaping to any illegal identifier via var"". Previously, it was only escaping names with a `#` in them, but now it will escape all names that are illegal identifiers. Before: ```julia julia> struct var"a%b+c" x::Int end julia> println(static_shown(var"a%b+c")) Main.a%b+c julia> println(static_shown(var"a%b+c"(1))) Main.a%b+c(x=1) ``` After: ```julia julia> struct var"a%b+c" x::Int end julia> println(static_shown(var"a%b+c")) Main.var"a%b+c" julia> println(static_shown(var"a%b+c"(1))) Main.var"a%b+c"(x=1) ``` --- src/rtutils.c | 5 ++++- test/show.jl | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/rtutils.c b/src/rtutils.c index 9e19c6f431f69..8ace4f0cb866b 100644 --- a/src/rtutils.c +++ b/src/rtutils.c @@ -625,7 +625,10 @@ static size_t jl_static_show_x_sym_escaped(JL_STREAM *out, jl_sym_t *name) JL_NO size_t n = 0; char *sn = jl_symbol_name(name); - int hidden = strchr(sn, '#') != 0; + int hidden = 0; + if (!jl_is_identifier(sn) || jl_is_operator(sn)) { + hidden = 1; + } if (hidden) { n += jl_printf(out, "var\""); diff --git a/test/show.jl b/test/show.jl index 8ce3aee3de18c..3350ef6647251 100644 --- a/test/show.jl +++ b/test/show.jl @@ -1339,6 +1339,7 @@ end # PR #38049 struct var"#X#" end var"#f#"() = 2 +struct var"%X%" end # Invalid name without '#' @test static_shown(var"#X#") == "$(@__MODULE__).var\"#X#\"" # (Just to make this test more sustainable,) for types, we don't necesssarily need to test @@ -1350,6 +1351,8 @@ var"#f#"() = 2 @testset for v in ( var"#X#", var"#X#"(), + var"%X%", + var"%X%"(), Vector, Vector{<:Any}, Vector{var"#X#"},