Skip to content

Commit 730fd0f

Browse files
committed
Warn on deprecated ~~~ unary op in tokenizer (#14870)
1 parent 29d3b0c commit 730fd0f

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
@@ -876,8 +876,18 @@ handle_unary_op(Rest, Line, Column, Kind, Length, Op, Scope, Tokens) ->
876876
Token = {identifier, {Line, Column, nil}, Op},
877877
tokenize(Remaining, Line, Column + Length + Extra, Scope, [Token | Tokens]);
878878
{Remaining, Extra} ->
879+
NewScope =
880+
%% TODO: Remove these deprecations on Elixir v2.0
881+
case Op of
882+
'~~~' ->
883+
Msg = "~~~ is deprecated. Use Bitwise.bnot/1 instead for clarity",
884+
prepend_warning(Line, Column, Msg, Scope);
885+
_ ->
886+
Scope
887+
end,
888+
879889
Token = {Kind, {Line, Column, nil}, Op},
880-
tokenize(Remaining, Line, Column + Length + Extra, Scope, [Token | Tokens])
890+
tokenize(Remaining, Line, Column + Length + Extra, NewScope, [Token | Tokens])
881891
end.
882892

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

900-
'~~~' ->
901-
Msg = "~~~ is deprecated. Use Bitwise.bnot/1 instead for clarity",
902-
prepend_warning(Line, Column, Msg, Scope);
903-
904910
'<|>' ->
905911
Msg = "<|> is deprecated. Use another pipe-like operator",
906912
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)