Skip to content

Commit 66f13b7

Browse files
authoredFeb 1, 2020
Deprecate remote nil arity calls without parens (#9769)
1 parent 534d8ea commit 66f13b7

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed
 

‎lib/elixir/src/elixir_expand.erl

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -401,14 +401,13 @@ expand({{'.', DotMeta, [Left, Right]}, Meta, Args}, E)
401401
when (is_tuple(Left) orelse is_atom(Left)), is_atom(Right), is_list(Meta), is_list(Args) ->
402402
{ELeft, EL} = expand(Left, elixir_env:prepare_write(E)),
403403

404-
%% TODO: Emit this warning on v1.11
405-
%% case is_atom(ELeft) andalso (Args == []) andalso
406-
%% (lists:keyfind(no_parens, 1, Meta) == {no_parens, true}) of
407-
%% true ->
408-
%% elixir_errors:form_warn(DotMeta, E, ?MODULE, {no_parens_nullary_remote, ELeft, Right});
409-
%% false ->
410-
%% ok
411-
%% end,
404+
case is_atom(ELeft) andalso (Args == []) andalso
405+
(lists:keyfind(no_parens, 1, Meta) == {no_parens, true}) of
406+
true ->
407+
elixir_errors:form_warn(DotMeta, E, ?MODULE, {no_parens_nullary_remote, ELeft, Right});
408+
false ->
409+
ok
410+
end,
412411

413412
elixir_dispatch:dispatch_require(Meta, ELeft, Right, Args, EL, fun(AR, AF, AA) ->
414413
expand_remote(AR, DotMeta, AF, Meta, AA, E, EL)
@@ -520,6 +519,7 @@ expand_fn_capture(Meta, Arg, E) ->
520519
is_atom(Remote) andalso
521520
elixir_env:trace({remote_function, Meta, Remote, Fun, Arity}, E),
522521
AttachedMeta = attach_context_module(Remote, Meta, E),
522+
handle_capture_possible_warning(Meta, Arg, E, Arity, Remote, Fun),
523523
{{'&', AttachedMeta, [{'/', [], [{{'.', [], [Remote, Fun]}, [], []}, Arity]}]}, EE};
524524
{{local, Fun, Arity}, #{function := nil}} ->
525525
form_error(Meta, E, ?MODULE, {undefined_local_capture, Fun, Arity});
@@ -529,6 +529,16 @@ expand_fn_capture(Meta, Arg, E) ->
529529
expand(Expr, EE)
530530
end.
531531

532+
handle_capture_possible_warning(Meta, {_, _, [{_, KeyList, _}, _]}, E, 0, Remote, Fun) ->
533+
case (lists:keyfind(no_parens, 1, KeyList) /= {no_parens, true}) of
534+
true ->
535+
elixir_errors:form_warn(Meta, E, ?MODULE, {parens_remote_capture, Remote, Fun});
536+
537+
false -> ok
538+
end;
539+
540+
handle_capture_possible_warning(_, _, _, _, _, _) -> ok.
541+
532542
expand_list([{'|', Meta, [_, _] = Args}], Fun, Acc, List) ->
533543
{EArgs, EAcc} = lists:mapfoldl(Fun, Acc, Args),
534544
expand_list([], Fun, EAcc, [{'|', Meta, EArgs} | List]);
@@ -1270,9 +1280,13 @@ format_error({unknown_variable, Name}) ->
12701280
io_lib:format("variable \"~ts\" does not exist and is being expanded to \"~ts()\","
12711281
" please use parentheses to remove the ambiguity or change the variable name", [Name, Name]);
12721282
format_error({no_parens_nullary_remote, Remote, Fun}) ->
1273-
io_lib:format("missing parenthesis on call to ~ts.~ts/0. "
1274-
"parenthesis are always required on function calls without arguments",
1283+
io_lib:format("missing parentheses on call to ~ts.~ts/0. "
1284+
"Parentheses are always required on function calls without arguments",
12751285
[elixir_aliases:inspect(Remote), Fun]);
1286+
format_error({parens_remote_capture, Remote, Fun}) ->
1287+
io_lib:format("extra parentheses on a remote function capture &~ts.~ts()/0 have been "
1288+
"deprecated. Please remove the parentheses: &~ts.~ts/0",
1289+
[elixir_aliases:inspect(Remote), Fun, elixir_aliases:inspect(Remote), Fun]);
12761290
format_error({parens_map_lookup_guard, Map, Field}) ->
12771291
io_lib:format("cannot invoke remote function in guard. "
12781292
"If you want to do a map lookup instead, please remove parens from ~ts.~ts()",

‎lib/elixir/test/elixir/kernel/warning_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,26 @@ defmodule Kernel.WarningTest do
17681768
purge(TestMod)
17691769
end
17701770

1771+
test "deprecate remote nullary zero-arity calls without parens" do
1772+
assert capture_err(fn ->
1773+
Code.eval_string("""
1774+
System.pid
1775+
""")
1776+
end) =~
1777+
"missing parentheses on call to System.pid/0. Parentheses are always required on function calls without arguments"
1778+
after
1779+
purge(TestMod)
1780+
end
1781+
1782+
test "deprecate nullary remote zero-arity capture with parens" do
1783+
assert capture_err(fn ->
1784+
Code.eval_string("""
1785+
&System.pid()/0
1786+
""")
1787+
end) =~
1788+
"extra parentheses on a remote function capture &System.pid()/0 have been deprecated. Please remove the parentheses: &System.pid/0"
1789+
end
1790+
17711791
defp purge(list) when is_list(list) do
17721792
Enum.each(list, &purge/1)
17731793
end

0 commit comments

Comments
 (0)