Skip to content

Commit

Permalink
Fix bug when using pinned variable in with else block (#13667)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabiwara authored Jun 15, 2024
1 parent a39f469 commit 1c17f1e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/elixir/src/elixir_erl_pass.erl
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ translate_with_else(Meta, [{'else', Else}], S) ->
RaiseClause = {'->', Generated, [[RaiseVar], RaiseExpr]},

Clauses = elixir_erl_clauses:get_clauses('else', [{'else', Else ++ [RaiseClause]}], match),
{TranslatedClauses, SC} = elixir_erl_clauses:clauses(Clauses, SV),
with_else_closure(Meta, TranslatedClauses, SC).
{TranslatedClauses, SC} = elixir_erl_clauses:clauses(Clauses, SV#elixir_erl{extra=pin_guard}),
with_else_closure(Meta, TranslatedClauses, SC#elixir_erl{extra=SV#elixir_erl.extra}).

with_else_closure(Meta, TranslatedClauses, S) ->
Ann = ?ann(Meta),
Expand Down
24 changes: 24 additions & 0 deletions lib/elixir/test/elixir/kernel/with_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ defmodule Kernel.WithTest do
assert with({^key, res} <- ok(42), do: res) == 42
end

test "pin matching with multiple else" do
key = :error

first_else =
with nil <- error() do
:ok
else
^key -> :pinned
_other -> :other
end

assert first_else == :pinned

second_else =
with nil <- ok(42) do
:ok
else
^key -> :pinned
_other -> :other
end

assert second_else == :other
end

test "two levels with" do
result =
with {:ok, n1} <- ok(11),
Expand Down

0 comments on commit 1c17f1e

Please sign in to comment.