Skip to content

Commit

Permalink
Correctly validate number of args for clauses with when in for and ca…
Browse files Browse the repository at this point in the history
…tch (#13785)
  • Loading branch information
lukaszsamson authored Aug 19, 2024
1 parent f2f4f3a commit 0e4aaf0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/elixir/src/elixir_clauses.erl
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ expand_clauses_with_stacktrace(Meta, Fun, Clauses, S, E) ->
{Ret, SE} = expand_clauses(Meta, 'try', Fun, Clauses, SS, E),
{Ret, SE#elixir_ex{stacktrace=OldStacktrace}}.

expand_catch(Meta, [{'when', _, [_, _, _, _ | _]}], _, E) ->
Error = {wrong_number_of_args_for_clause, "one or two args", origin(Meta, 'try'), 'catch'},
file_error(Meta, E, ?MODULE, Error);
expand_catch(_Meta, [_] = Args, S, E) ->
head(Args, S, E);
expand_catch(_Meta, [_, _] = Args, S, E) ->
Expand Down
3 changes: 3 additions & 0 deletions lib/elixir/src/elixir_expand.erl
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,9 @@ expand_for_do_block(_Meta, Expr, S, E, false) ->
expand(Expr, S, E);
expand_for_do_block(Meta, [{'->', _, _} | _] = Clauses, S, E, {reduce, _}) ->
Transformer = fun
({_, _, [[{'when', _, [_, _, _ | _]}], _]}, _) ->
file_error(Meta, E, ?MODULE, for_with_reduce_bad_block);

({_, _, [[_], _]} = Clause, SA) ->
SReset = elixir_env:reset_unused_vars(SA),

Expand Down
18 changes: 18 additions & 0 deletions lib/elixir/test/elixir/kernel/expansion_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,11 @@ defmodule Kernel.ExpansionTest do
~r"when using :reduce with comprehensions, the do block must be written using acc -> expr clauses",
fn -> expand(quote(do: for(x <- 1..3, reduce: %{}, do: (acc, x -> x)))) end
)

assert_compile_error(
~r"when using :reduce with comprehensions, the do block must be written using acc -> expr clauses",
fn -> expand(quote(do: for(x <- 1..3, reduce: %{}, do: (acc, x when 1 == 1 -> x)))) end
)
end

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

expand(code)
end)

assert_compile_error(message, fn ->
code =
quote do
try do
x
catch
_, _, _ when 1 == 1 -> :ok
end
end

expand(code)
end)
end

test "expects clauses for rescue, else, catch" do
Expand Down

0 comments on commit 0e4aaf0

Please sign in to comment.