Skip to content

Commit 8203cf1

Browse files
authored
Tokenizer uses static_atoms_encoder on quoted keyword keys (#11313)
Closes #11312
1 parent 5a2b24d commit 8203cf1

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

lib/elixir/src/elixir_tokenizer.erl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,15 @@ handle_strings(T, Line, Column, H, Scope, Tokens) ->
758758
end,
759759

760760
case unescape_tokens(Parts, Line, Column, NewScope) of
761+
{ok, [Part]} when is_binary(Part) ->
762+
case unsafe_to_atom(Part, Line, Column - 1, Scope) of
763+
{ok, Atom} ->
764+
Token = {kw_identifier, {Line, Column - 1, nil}, Atom},
765+
tokenize(Rest, NewLine, NewColumn + 1, NewScope, [Token | Tokens]);
766+
{error, Reason} ->
767+
{error, Reason, Rest, Tokens}
768+
end;
769+
761770
{ok, Unescaped} ->
762771
Key = case Scope#elixir_tokenizer.existing_atoms_only of
763772
true -> kw_identifier_safe;

lib/elixir/test/elixir/kernel/parser_test.exs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,23 @@ defmodule Kernel.ParserTest do
154154
Code.string_to_quoted("there_is_no_such_var", static_atoms_encoder: encoder)
155155
end
156156

157+
test "encodes quoted keyword keys" do
158+
ref = make_ref()
159+
160+
encoder = fn atom, meta ->
161+
assert atom == "there is no such key"
162+
assert meta[:line] == 1
163+
assert meta[:column] == 2
164+
assert meta[:file] == "nofile"
165+
{:ok, {:my, "atom", ref}}
166+
end
167+
168+
assert {:ok, [{{:my, "atom", ^ref}, true}]} =
169+
Code.string_to_quoted(~S(["there is no such key": true]),
170+
static_atoms_encoder: encoder
171+
)
172+
end
173+
157174
test "addresses ambiguities" do
158175
encoder = fn string, _meta -> {:ok, {:atom, string}} end
159176

lib/elixir/test/erlang/tokenizer_test.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ kw_test() ->
7979
[{kw_identifier, {1, 1, nil}, 'A@!'}] = tokenize("A@!: "),
8080
[{kw_identifier, {1, 1, nil}, 'a@!'}] = tokenize("a@!: "),
8181
[{kw_identifier, {1, 1, nil}, foo}, {bin_string, {1, 6, nil}, [<<"bar">>]}] = tokenize("foo: \"bar\""),
82-
[{kw_identifier_unsafe, {1, 1, nil}, [<<"+">>]}, {bin_string, {1, 6, nil}, [<<"bar">>]}] = tokenize("\"+\": \"bar\"").
82+
[{kw_identifier, {1, 1, nil}, '+'}, {bin_string, {1, 6, nil}, [<<"bar">>]}] = tokenize("\"+\": \"bar\"").
8383

8484
int_test() ->
8585
[{int, {1, 1, 123}, "123"}] = tokenize("123"),

0 commit comments

Comments
 (0)