Skip to content

Commit a5e9c39

Browse files
committed
Do not raise when checking for ambiguity with static_atoms_encoder, closes #10501
1 parent 6d918f5 commit a5e9c39

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

lib/elixir/src/elixir_tokenizer.erl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,8 @@ tokenize([$:, H | T] = Original, Line, Column, Scope, Tokens) when ?is_quote(H)
477477

478478
tokenize([$: | String] = Original, Line, Column, Scope, Tokens) ->
479479
case tokenize_identifier(String, Line, Column, Scope, false) of
480-
{_Kind, Atom, Rest, Length, _Ascii, _Special} ->
481-
NewScope = maybe_warn_for_ambiguous_bang_before_equals(atom, Atom, Rest, Scope, Line),
480+
{_Kind, Unencoded, Atom, Rest, Length, _Ascii, _Special} ->
481+
NewScope = maybe_warn_for_ambiguous_bang_before_equals(atom, Unencoded, Rest, Scope, Line),
482482
Token = {atom, {Line, Column, nil}, Atom},
483483
tokenize(Rest, Line, Column + 1 + Length, NewScope, [Token | Tokens]);
484484
empty ->
@@ -559,7 +559,7 @@ tokenize([$. | T], Line, Column, Scope, Tokens) ->
559559

560560
tokenize(String, Line, Column, Scope, Tokens) ->
561561
case tokenize_identifier(String, Line, Column, Scope, not previous_was_dot(Tokens)) of
562-
{Kind, Atom, Rest, Length, Ascii, Special} ->
562+
{Kind, Unencoded, Atom, Rest, Length, Ascii, Special} ->
563563
HasAt = lists:member($@, Special),
564564

565565
case Rest of
@@ -583,7 +583,7 @@ tokenize(String, Line, Column, Scope, Tokens) ->
583583
tokenize_alias(Rest, Line, Column, Atom, Length, Ascii, Special, Scope, Tokens);
584584

585585
_ when Kind == identifier ->
586-
NewScope = maybe_warn_for_ambiguous_bang_before_equals(identifier, Atom, Rest, Scope, Line),
586+
NewScope = maybe_warn_for_ambiguous_bang_before_equals(identifier, Unencoded, Rest, Scope, Line),
587587
Token = check_call_identifier(Line, Column, Atom, Rest),
588588
tokenize(Rest, Line, Column + Length, NewScope, [Token | Tokens]);
589589

@@ -1142,7 +1142,7 @@ tokenize_identifier(String, Line, Column, Scope, MaybeKeyword) ->
11421142
{keyword, Atom, Type} ->
11431143
{keyword, Atom, Type, Rest, Length};
11441144
{ok, Atom} ->
1145-
{Kind, Atom, Rest, Length, Ascii, Special};
1145+
{Kind, Acc, Atom, Rest, Length, Ascii, Special};
11461146
{error, _Reason} = Error ->
11471147
Error
11481148
end;
@@ -1462,11 +1462,11 @@ maybe_warn_too_many_of_same_char(_Token, _Rest, _Line, Scope) ->
14621462
Scope.
14631463

14641464
%% TODO: Turn into an error on v2.0
1465-
maybe_warn_for_ambiguous_bang_before_equals(Kind, Atom, [$= | _], Scope, Line) ->
1465+
maybe_warn_for_ambiguous_bang_before_equals(Kind, Unencoded, [$= | _], Scope, Line) ->
14661466
{What, Identifier} =
14671467
case Kind of
1468-
atom -> {"atom", [$: | atom_to_list(Atom)]};
1469-
identifier -> {"identifier", atom_to_list(Atom)}
1468+
atom -> {"atom", [$: | Unencoded]};
1469+
identifier -> {"identifier", Unencoded}
14701470
end,
14711471

14721472
case lists:last(Identifier) of

lib/elixir/test/elixir/code_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,14 @@ defmodule CodeTest do
224224
Code.string_to_quoted("there_is_no_such_var", static_atoms_encoder: encoder)
225225
end
226226

227+
test "static_atoms_encoder ambiguities" do
228+
encoder = fn string, _meta -> {:ok, {:atom, string}} end
229+
230+
# We check a=1 for precedence issues with a!=1, make sure it works
231+
assert Code.string_to_quoted!("a = 1", static_atoms_encoder: encoder)
232+
assert Code.string_to_quoted!("a=1", static_atoms_encoder: encoder)
233+
end
234+
227235
test "static_atoms_encoder does not encode keywords" do
228236
encoder = fn atom, _meta -> raise "shouldn't be invoked for #{atom}" end
229237

0 commit comments

Comments
 (0)