Skip to content

Commit aef3dce

Browse files
authored
Warn on deprecated ~~~ unary op in tokenizer (elixir-lang#14870)
1 parent 25771dd commit aef3dce

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

lib/elixir/src/elixir_tokenizer.erl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -883,8 +883,18 @@ handle_unary_op(Rest, Line, Column, Kind, Length, Op, Scope, Tokens) ->
883883
Token = {identifier, {Line, Column, nil}, Op},
884884
tokenize(Remaining, NewLine, NewColumn, Scope, [Token | Tokens]);
885885
{Remaining, NewLine, NewColumn} ->
886+
NewScope =
887+
%% TODO: Remove these deprecations on Elixir v2.0
888+
case Op of
889+
'~~~' ->
890+
Msg = "~~~ is deprecated. Use Bitwise.bnot/1 instead for clarity",
891+
prepend_warning(Line, Column, Msg, Scope);
892+
_ ->
893+
Scope
894+
end,
895+
886896
Token = {Kind, {Line, Column, nil}, Op},
887-
tokenize(Remaining, NewLine, NewColumn, Scope, [Token | Tokens])
897+
tokenize(Remaining, NewLine, NewColumn, NewScope, [Token | Tokens])
888898
end.
889899

890900
handle_op([$: | Rest], Line, Column, _Kind, Length, Op, Scope, Tokens) when ?is_space(hd(Rest)) ->
@@ -904,10 +914,6 @@ handle_op(Rest, Line, Column, Kind, Length, Op, Scope, Tokens) ->
904914
Msg = "^^^ is deprecated. It is typically used as xor but it has the wrong precedence, use Bitwise.bxor/2 instead",
905915
prepend_warning(Line, Column, Msg, Scope);
906916

907-
'~~~' ->
908-
Msg = "~~~ is deprecated. Use Bitwise.bnot/1 instead for clarity",
909-
prepend_warning(Line, Column, Msg, Scope);
910-
911917
'<|>' ->
912918
Msg = "<|> is deprecated. Use another pipe-like operator",
913919
prepend_warning(Line, Column, Msg, Scope);

lib/elixir/test/erlang/tokenizer_test.erl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ tokenize_error(String) ->
1616
{error, Error, _, _, _} = elixir_tokenizer:tokenize(String, 1, []),
1717
Error.
1818

19+
tokenize_warnings(String) ->
20+
{ok, _Line, _Column, Warnings, Result, []} = elixir_tokenizer:tokenize(String, 1, []),
21+
{lists:reverse(Result), Warnings}.
22+
1923
type_test() ->
2024
[{int, {1, 1, 1}, "1"},
2125
{type_op, {1, 3, nil}, '::'},
@@ -279,3 +283,13 @@ sigil_heredoc_test() ->
279283
invalid_sigil_delimiter_test() ->
280284
{[{line, 1}, {column, 1}], "invalid sigil delimiter: ", Message} = tokenize_error("~s\\"),
281285
true = lists:prefix("\"\\\" (column 3, code point U+005C)", lists:flatten(Message)).
286+
287+
deprecated_operators_test() ->
288+
{
289+
[{xor_op, {1, 1, nil}, '^^^'}, {int, {1, 4, 1}, "1"}],
290+
[{{1, 1}, "^^^ is deprecated. It is typically used as xor but it has the wrong precedence, use Bitwise.bxor/2 instead"}]
291+
} = tokenize_warnings("^^^1"),
292+
{
293+
[{unary_op, {1, 1, nil}, '~~~'}, {int, {1, 4, 1}, "1"}],
294+
[{{1, 1}, "~~~ is deprecated. Use Bitwise.bnot/1 instead for clarity"}]
295+
} = tokenize_warnings("~~~1").

0 commit comments

Comments
 (0)