Skip to content

Commit b4c75f9

Browse files
author
José Valim
committed
Fail to compile try without else/catch/after/rescue, closes #2797
Signed-off-by: José Valim <[email protected]>
1 parent 70692e6 commit b4c75f9

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

lib/elixir/src/elixir_exp_clauses.erl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ do_receive(Meta, {Key, _}, _Acc, E) ->
101101
%% Try
102102

103103
'try'(Meta, [], E) ->
104-
compile_error(Meta, ?m(E, file), "missing do keywords in try");
104+
compile_error(Meta, ?m(E, file), "missing do keyword in try");
105+
'try'(Meta, [{do, _}], E) ->
106+
compile_error(Meta, ?m(E, file), "missing catch/rescue/after/else keyword in try");
105107
'try'(Meta, KV, E) when not is_list(KV) ->
106108
elixir_errors:compile_error(Meta, ?m(E, file), "invalid arguments for try");
107109
'try'(Meta, KV, E) ->

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,6 @@ defmodule Kernel.ExpansionTest do
392392

393393
## Try
394394

395-
test "try: expands do" do
396-
assert expand(quote do: (try do x = y end; x)) ==
397-
quote do: (try do x = y() end; x())
398-
end
399-
400395
test "try: expands catch" do
401396
assert expand(quote do: (try do x catch x, y -> z = :erlang.+(x, y) end; z)) ==
402397
quote do: (try do x() catch x, y -> z = :erlang.+(x, y) end; z())
@@ -417,6 +412,12 @@ defmodule Kernel.ExpansionTest do
417412
quote do: (try do x() rescue unquote(:in)(x, _) -> x; unquote(:in)(_, [:"Elixir.Error"]) -> x() end; x())
418413
end
419414

415+
test "try: expects more than do" do
416+
assert_raise CompileError, ~r"missing catch/rescue/after/else keyword in try", fn ->
417+
expand(quote do: (try do x = y end; x))
418+
end
419+
end
420+
420421
## Binaries
421422

422423
test "bitstrings: size * unit" do

0 commit comments

Comments
 (0)