Skip to content

Commit 30cb68e

Browse files
authored
Fix printing of var"keyword" identifiers (issue #56936) (#57268)
Fixes #56936. One quirk this change would introduce is that `:((1:10)[end])` will be shown as `:((1:10)[var"end"])`, which is not ideal, but still correct. It would make sense to fix this at the same time we fix `(let var"end" = 1; (1:10)[var"end"]; end) == 10` being true. I'll open a separate issue for this.
1 parent ffc96bc commit 30cb68e

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

base/show.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,9 +1757,15 @@ function show_enclosed_list(io::IO, op, items, sep, cl, indent, prec=0, quote_le
17571757
print(io, cl)
17581758
end
17591759

1760+
const keyword_syms = Set([
1761+
:baremodule, :begin, :break, :catch, :const, :continue, :do, :else, :elseif,
1762+
:end, :export, :false, :finally, :for, :function, :global, :if, :import,
1763+
:let, :local, :macro, :module, :public, :quote, :return, :struct, :true,
1764+
:try, :using, :while ])
1765+
17601766
function is_valid_identifier(sym)
1761-
return isidentifier(sym) || (
1762-
_isoperator(sym) &&
1767+
return (isidentifier(sym) && !(sym in keyword_syms)) ||
1768+
(_isoperator(sym) &&
17631769
!(sym in (Symbol("'"), :(::), :?)) &&
17641770
!is_syntactic_operator(sym)
17651771
)

test/show.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,9 +2356,9 @@ end
23562356

23572357
# begin/end indices
23582358
@weak_test_repr "a[begin, end, (begin; end)]"
2359-
@test repr(Base.remove_linenums!(:(a[begin, end, (begin; end)]))) == ":(a[begin, end, (begin;\n end)])"
2359+
@test_broken repr(Base.remove_linenums!(:(a[begin, end, (begin; end)]))) == ":(a[begin, end, (begin;\n end)])"
23602360
@weak_test_repr "a[begin, end, let x=1; (x+1;); end]"
2361-
@test repr(Base.remove_linenums!(:(a[begin, end, let x=1; (x+1;); end]))) ==
2361+
@test_broken repr(Base.remove_linenums!(:(a[begin, end, let x=1; (x+1;); end]))) ==
23622362
":(a[begin, end, let x = 1\n begin\n x + 1\n end\n end])"
23632363
@test_repr "a[(bla;)]"
23642364
@test_repr "a[(;;)]"
@@ -2795,3 +2795,9 @@ Base.setindex!(d::NoLengthDict, v, k) = d.dict[k] = v
27952795
@test contains(str, "NoLengthDict")
27962796
@test contains(str, "1 => 2")
27972797
end
2798+
2799+
# Issue 56936
2800+
@testset "code printing of var\"keyword\" identifiers" begin
2801+
@test_repr """:(var"do" = 1)"""
2802+
@weak_test_repr """:(let var"let" = 1; var"let"; end)"""
2803+
end

0 commit comments

Comments
 (0)