summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFernando Tapia Rico <fertapric@gmail.com>2019-10-13 16:50:05 +0200
committerGitHub <noreply@github.com>2019-10-13 16:50:05 +0200
commit66a289056e16c5854e56693b065890bf73d607eb (patch)
tree95efeb1d135b9d354d6bf28b4b10f1750f3a1441
parentfd871eab822a7cca118e21815e97470199eff69d (diff)
downloadelixir-66a289056e16c5854e56693b065890bf73d607eb.tar.gz
Raise when passing an empty list instead of clauses (#9402)
This fixes an edge case in which the compiler will process an empty list value (`[]`) as a block with no clauses, which is not possible. Here is an example: with {:ok, value} <- Map.fetch(%{a: 1}, :a) do value else [] end With this change, the code above will not compile.
-rw-r--r--lib/elixir/src/elixir_clauses.erl2
-rw-r--r--lib/elixir/test/elixir/kernel/expansion_test.exs45
2 files changed, 46 insertions, 1 deletions
diff --git a/lib/elixir/src/elixir_clauses.erl b/lib/elixir/src/elixir_clauses.erl
index a67defac6..54fdd0e4b 100644
--- a/lib/elixir/src/elixir_clauses.erl
+++ b/lib/elixir/src/elixir_clauses.erl
@@ -308,7 +308,7 @@ expand_clauses(Meta, Kind, Fun, Clauses, E) ->
NewKind = origin(Meta, Kind),
expand_clauses_origin(Meta, NewKind, Fun, Clauses, E).
-expand_clauses_origin(Meta, Kind, Fun, {Key, Clauses}, E) when is_list(Clauses) ->
+expand_clauses_origin(Meta, Kind, Fun, {Key, [_ | _] = Clauses}, E) when is_list(Clauses) ->
Transformer = fun(Clause, Acc) ->
{EClause, EAcc} = clause(Meta, {Kind, Key}, Fun, Clause, Acc),
{EClause, elixir_env:merge_and_check_unused_vars(Acc, EAcc)}
diff --git a/lib/elixir/test/elixir/kernel/expansion_test.exs b/lib/elixir/test/elixir/kernel/expansion_test.exs
index 9ebf8bf13..64cac6787 100644
--- a/lib/elixir/test/elixir/kernel/expansion_test.exs
+++ b/lib/elixir/test/elixir/kernel/expansion_test.exs
@@ -815,6 +815,10 @@ defmodule Kernel.ExpansionTest do
assert_raise CompileError, ~r"expected -> clauses for :else in \"with\"", fn ->
expand(quote(do: with(_ <- true, do: :ok, else: :error)))
end
+
+ assert_raise CompileError, ~r"expected -> clauses for :else in \"with\"", fn ->
+ expand(quote(do: with(_ <- true, do: :ok, else: [])))
+ end
end
test "fails for invalid options" do
@@ -1853,6 +1857,19 @@ defmodule Kernel.ExpansionTest do
expand(code)
end
+ assert_raise CompileError, ~r"expected -> clauses for :rescue in \"try\"", fn ->
+ code =
+ quote do
+ try do
+ e
+ rescue
+ []
+ end
+ end
+
+ expand(code)
+ end
+
assert_raise CompileError, ~r"expected -> clauses for :catch in \"try\"", fn ->
code =
quote do
@@ -1879,6 +1896,19 @@ defmodule Kernel.ExpansionTest do
expand(code)
end
+ assert_raise CompileError, ~r"expected -> clauses for :catch in \"try\"", fn ->
+ code =
+ quote do
+ try do
+ e
+ catch
+ []
+ end
+ end
+
+ expand(code)
+ end
+
assert_raise CompileError, ~r"expected -> clauses for :else in \"try\"", fn ->
code =
quote do
@@ -1908,6 +1938,21 @@ defmodule Kernel.ExpansionTest do
expand(code)
end
+
+ assert_raise CompileError, ~r"expected -> clauses for :else in \"try\"", fn ->
+ code =
+ quote do
+ try do
+ e
+ catch
+ _ -> :ok
+ else
+ []
+ end
+ end
+
+ expand(code)
+ end
end
end