Skip to content

Commit 2fdf96e

Browse files
ericentinJosé Valim
authored andcommitted
The else clause in with supports guards (#4960)
Signed-off-by: José Valim <[email protected]>
1 parent 494bded commit 2fdf96e

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/elixir/src/elixir_with.erl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,14 @@ build_else(Meta, ElseClauses) ->
107107
each_clause_to_error_match(ElseClauses)] ++ [build_raise(Meta)].
108108

109109
each_clause_to_error_match(Clauses) ->
110-
[{match, Meta, [{error, Match}], Expr} ||
110+
[{match, Meta, [error_match_for_match(Match)], Expr} ||
111111
{'->', Meta, [[Match], Expr]} <- Clauses].
112112

113+
error_match_for_match({'when', Meta, [Left, Right]}) ->
114+
{'when', Meta, [{error, Left}, Right]};
115+
error_match_for_match(Match) ->
116+
{error, Match}.
117+
113118
build_raise(Meta) ->
114119
Other = {other, Meta, ?MODULE},
115120
{match, ?generated, [{error, Other}], {{'.', Meta, [erlang, error]}, Meta, [{with_clause, Other}]}}.

lib/elixir/test/elixir/kernel/with_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ defmodule Kernel.WithTest do
6666

6767
test "else conditions" do
6868
assert with({:ok, res} <- 41, do: res, else: ({:error, error} -> error; res -> res + 1)) == 42
69+
assert with({:ok, res} <- 41, do: res, else: (res when res == 41 -> res + 1; res -> res)) == 42
6970
assert with({:ok, res} <- 41, do: res, else: (_ -> :error)) == :error
7071
end
7172

0 commit comments

Comments
 (0)