Skip to content

Commit 021dc54

Browse files
author
José Valim
committed
Remove unsupported matching on rescue clause
1 parent af0970f commit 021dc54

File tree

3 files changed

+9
-56
lines changed

3 files changed

+9
-56
lines changed

lib/elixir/src/elixir_try.erl

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ each_clause(Meta, { rescue, [Condition|T], Expr }, S) ->
4949
end
5050
end;
5151
_ ->
52-
validate_rescue_access(Meta, Condition, S),
5352
each_clause(Meta, { 'catch', [error, Condition|T], Expr }, S)
5453
end;
5554

@@ -65,8 +64,12 @@ each_clause(Meta, {Key,_,_}, S) ->
6564
normalize_rescue(Meta, List, S) when is_list(List) ->
6665
normalize_rescue(Meta, { in, Meta, [{ '_', Meta, nil }, List] }, S);
6766

67+
%% rescue _
68+
normalize_rescue(_, { '_', _, Atom }, _S) when is_atom(Atom) ->
69+
false;
70+
6871
%% rescue var -> var in _
69-
normalize_rescue(_, { Name, Meta, Atom } = Rescue, S) when is_atom(Name), is_atom(Atom), Name /= '_' ->
72+
normalize_rescue(_, { Name, Meta, Atom } = Rescue, S) when is_atom(Name), is_atom(Atom) ->
7073
normalize_rescue(Meta, { in, Meta, [Rescue, { '_', Meta, nil }] }, S);
7174

7275
%% rescue var in [Exprs]
@@ -85,22 +88,13 @@ normalize_rescue(_, { in, Meta, [Left, Right] }, S) ->
8588
end
8689
end;
8790

88-
normalize_rescue(_, { '=', Meta, [{ '__aliases__', _, _ } = Alias, { Name, _, Atom } = Var] }, S)
89-
when is_atom(Name) and is_atom(Atom) ->
90-
elixir_errors:handle_file_warning(S#elixir_scope.file, { Meta, ?MODULE, { rescue_no_match, Var, Alias } }),
91-
false;
92-
93-
normalize_rescue(_, { '=', Meta, [{ Name, _, Atom } = Var, { '__aliases__', _, _ } = Alias] }, S)
94-
when is_atom(Name) and is_atom(Atom) ->
95-
elixir_errors:handle_file_warning(S#elixir_scope.file, { Meta, ?MODULE, { rescue_no_match, Var, Alias } }),
96-
false;
97-
9891
normalize_rescue(Meta, Condition, S) ->
9992
case elixir_translator:translate_each(Condition, S#elixir_scope{context=match}) of
10093
{ { atom, _, Atom }, _ } ->
10194
normalize_rescue(Meta, { in, Meta, [{ '_', Meta, nil }, [Atom]] }, S);
10295
_ ->
103-
false
96+
elixir_errors:syntax_error(Meta, S#elixir_scope.file, "invalid rescue clause. The clause should match on an alias, "
97+
"a variable or be in the `var in [alias]` format")
10498
end.
10599

106100
%% Convert rescue clauses into guards.
@@ -242,24 +236,6 @@ erlang_rescue_guard_for(Meta, Var, 'Elixir.ErlangError') ->
242236
] },
243237
{ 'or', Meta, [IsNotTuple, IsException] }.
244238

245-
%% Validate rescue access
246-
247-
validate_rescue_access(Meta, { '=', _, [Left, Right] }, S) ->
248-
validate_rescue_access(Meta, Left, S),
249-
validate_rescue_access(Meta, Right, S);
250-
251-
validate_rescue_access(Meta, { { '.', _, ['Elixir.Kernel', 'access'] }, _, [Element, _] }, S) ->
252-
case elixir_translator:translate_each(Element, S) of
253-
{ { atom, _, Atom }, _ } ->
254-
case lists:member(Atom, erlang_rescues()) of
255-
false -> [];
256-
true -> elixir_errors:syntax_error(Meta, S#elixir_scope.file, "cannot (yet) pattern match against erlang exceptions")
257-
end;
258-
_ -> []
259-
end;
260-
261-
validate_rescue_access(_, _, _) -> [].
262-
263239
%% Helpers
264240

265241
format_error({ rescue_no_match, Var, Alias }) ->

lib/elixir/test/elixir/kernel/errors_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ defmodule Kernel.ErrorsTest do
229229
format_rescue 'Kernel.ErrorsTest.Config[foo: :bar]'
230230
end
231231

232-
test :invalid_access_protocol_on_rescue do
233-
assert "nofile:1: cannot (yet) pattern match against erlang exceptions" ==
232+
test :invalid_rescue_clause do
233+
assert "nofile:1: invalid rescue clause. The clause should match on an alias, a variable or be in the `var in [alias]` format" ==
234234
format_rescue 'try do\n1\nrescue\nUndefinedFunctionError[arity: 1] -> false\nend'
235235
end
236236

lib/elixir/test/elixir/kernel/rescue_test.exs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,6 @@ defmodule Kernel.RescueTest do
9393
assert result == "an exception"
9494
end
9595

96-
test :rescue_defined_variable do
97-
var = Protocol.UndefinedError[protocol: Foo]
98-
99-
result = try do
100-
raise Protocol.UndefinedError, protocol: Foo
101-
rescue
102-
^var -> true
103-
end
104-
105-
assert result, "Expected to rescue with success"
106-
end
107-
10896
test :rescue_named_defined_variable do
10997
expected = RuntimeError
11098

@@ -262,16 +250,5 @@ defmodule Kernel.RescueTest do
262250
assert result == "undefined function: DoNotExist.for_sure/0"
263251
end
264252

265-
test :pattern_matching do
266-
result = try do
267-
raise Protocol.UndefinedError, protocol: Foo
268-
rescue
269-
Protocol.UndefinedError[protocol: Bar] -> false
270-
Protocol.UndefinedError[protocol: Foo] = x -> x.message
271-
end
272-
273-
assert result == "protocol Foo not implemented for nil"
274-
end
275-
276253
defp zero(0), do: 0
277254
end

0 commit comments

Comments
 (0)