Skip to content

Commit dcf8df3

Browse files
author
José Valim
committed
Raise if arity not in 0..255, closes #3754
1 parent 38f716c commit dcf8df3

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/elixir/src/elixir_fn.erl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ expand(Meta, Clauses, E) when is_list(Clauses) ->
3838
%% Capture
3939

4040
capture(Meta, {'/', _, [{{'.', _, [_, F]} = Dot, RequireMeta, []}, A]}, E) when is_atom(F), is_integer(A) ->
41-
Args = [{'&', [], [X]} || X <- lists:seq(1, A)],
41+
Args = args_from_arity(Meta, A, E),
4242
capture_require(Meta, {Dot, RequireMeta, Args}, E, true);
4343

4444
capture(Meta, {'/', _, [{F, _, C}, A]}, E) when is_atom(F), is_integer(A), is_atom(C) ->
45+
Args = args_from_arity(Meta, A, E),
4546
ImportMeta =
4647
case lists:keyfind(import_fa, 1, Meta) of
4748
{import_fa, {Receiver, Context}} ->
@@ -51,7 +52,6 @@ capture(Meta, {'/', _, [{F, _, C}, A]}, E) when is_atom(F), is_integer(A), is_at
5152
);
5253
false -> Meta
5354
end,
54-
Args = [{'&', [], [X]} || X <- lists:seq(1, A)],
5555
capture_import(Meta, {F, ImportMeta, Args}, E, true);
5656

5757
capture(Meta, {{'.', _, [_, Fun]}, _, Args} = Expr, E) when is_atom(Fun), is_list(Args) ->
@@ -155,6 +155,12 @@ do_escape(List, Counter, E, Dict) when is_list(List) ->
155155
do_escape(Other, _Counter, _E, Dict) ->
156156
{Other, Dict}.
157157

158+
args_from_arity(_Meta, A, _E) when is_integer(A), A >= 0, A =< 255 ->
159+
[{'&', [], [X]} || X <- lists:seq(1, A)];
160+
args_from_arity(Meta, A, E) ->
161+
Message = "invalid arity for &, expected a number between 0 and 255, got: ~b",
162+
compile_error(Meta, ?m(E, file), Message, [A]).
163+
158164
is_sequential_and_not_empty([]) -> false;
159165
is_sequential_and_not_empty(List) -> is_sequential(List, 1).
160166

lib/elixir/test/elixir/kernel/fn_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ defmodule Kernel.FnTest do
136136
"&:foo"
137137
end
138138

139+
test "failure on invalid arity" do
140+
assert_compile_fail CompileError,
141+
"nofile:1: invalid arity for &, expected a number between 0 and 255, got: 256",
142+
"&Mod.fun/256"
143+
end
144+
139145
test "failure when no captures" do
140146
assert_compile_fail CompileError,
141147
"nofile:1: invalid args for &, expected an expression in the format of &Mod.fun/arity, " <>

0 commit comments

Comments
 (0)