Skip to content

Commit 0e4aaf0

Browse files
authored
Correctly validate number of args for clauses with when in for and catch (#13785)
1 parent f2f4f3a commit 0e4aaf0

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

lib/elixir/src/elixir_clauses.erl

+3
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ expand_clauses_with_stacktrace(Meta, Fun, Clauses, S, E) ->
257257
{Ret, SE} = expand_clauses(Meta, 'try', Fun, Clauses, SS, E),
258258
{Ret, SE#elixir_ex{stacktrace=OldStacktrace}}.
259259

260+
expand_catch(Meta, [{'when', _, [_, _, _, _ | _]}], _, E) ->
261+
Error = {wrong_number_of_args_for_clause, "one or two args", origin(Meta, 'try'), 'catch'},
262+
file_error(Meta, E, ?MODULE, Error);
260263
expand_catch(_Meta, [_] = Args, S, E) ->
261264
head(Args, S, E);
262265
expand_catch(_Meta, [_, _] = Args, S, E) ->

lib/elixir/src/elixir_expand.erl

+3
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,9 @@ expand_for_do_block(_Meta, Expr, S, E, false) ->
812812
expand(Expr, S, E);
813813
expand_for_do_block(Meta, [{'->', _, _} | _] = Clauses, S, E, {reduce, _}) ->
814814
Transformer = fun
815+
({_, _, [[{'when', _, [_, _, _ | _]}], _]}, _) ->
816+
file_error(Meta, E, ?MODULE, for_with_reduce_bad_block);
817+
815818
({_, _, [[_], _]} = Clause, SA) ->
816819
SReset = elixir_env:reset_unused_vars(SA),
817820

lib/elixir/test/elixir/kernel/expansion_test.exs

+18
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,11 @@ defmodule Kernel.ExpansionTest do
948948
~r"when using :reduce with comprehensions, the do block must be written using acc -> expr clauses",
949949
fn -> expand(quote(do: for(x <- 1..3, reduce: %{}, do: (acc, x -> x)))) end
950950
)
951+
952+
assert_compile_error(
953+
~r"when using :reduce with comprehensions, the do block must be written using acc -> expr clauses",
954+
fn -> expand(quote(do: for(x <- 1..3, reduce: %{}, do: (acc, x when 1 == 1 -> x)))) end
955+
)
951956
end
952957

953958
test "raise error for unknown options" do
@@ -2130,6 +2135,19 @@ defmodule Kernel.ExpansionTest do
21302135

21312136
expand(code)
21322137
end)
2138+
2139+
assert_compile_error(message, fn ->
2140+
code =
2141+
quote do
2142+
try do
2143+
x
2144+
catch
2145+
_, _, _ when 1 == 1 -> :ok
2146+
end
2147+
end
2148+
2149+
expand(code)
2150+
end)
21332151
end
21342152

21352153
test "expects clauses for rescue, else, catch" do

0 commit comments

Comments
 (0)