Skip to content

Commit 1230345

Browse files
committed
Generate unique vars in defguard
1 parent bdea27c commit 1230345

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

lib/elixir/lib/kernel/utils.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ defmodule Kernel.Utils do
375375

376376
%{} ->
377377
generated = String.to_atom("arg" <> Integer.to_string(map_size(acc) + 1))
378-
new_var = Macro.var(generated, Elixir)
378+
new_var = Macro.unique_var(generated, Elixir)
379379
{new_var, Map.put(acc, pair, {new_var, var})}
380380
end
381381

lib/elixir/src/elixir_erl_var.erl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,11 @@ dump_binding(Binding, ErlS, ExS, PruneBefore) ->
109109
#elixir_ex{vars={ExVars, _}, unused={Unused, _}} = ExS,
110110

111111
maps:fold(fun
112-
%% If the variable is part of the pruning (usually the input binding)
113-
%% and is unused, we removed it from vars.
114-
(Pair, Version, {B, V})
115-
when Version =< PruneBefore, not is_map_key({Pair, Version}, Unused) ->
116-
{B, maps:remove(Pair, V)};
117-
118-
({Var, Kind} = Pair, Version, {B, V}) when is_atom(Kind) ->
112+
({Var, Kind} = Pair, Version, {B, V})
113+
when is_atom(Kind),
114+
%% If the variable is part of the pruning (usually the input binding)
115+
%% and is unused, we removed it from vars.
116+
Version > PruneBefore orelse is_map_key({Pair, Version}, Unused) ->
119117
Key = case Kind of
120118
nil -> Var;
121119
_ -> Pair
@@ -125,6 +123,9 @@ dump_binding(Binding, ErlS, ExS, PruneBefore) ->
125123
Value = find_binding(ErlName, Binding),
126124
{[{Key, Value} | B], V};
127125

126+
(Pair, _, {B, V}) when PruneBefore >= 0 ->
127+
{B, maps:remove(Pair, V)};
128+
128129
(_, _, Acc) ->
129130
Acc
130131
end, {[], ExVars}, ExVars).

lib/elixir/test/elixir/code_test.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,15 @@ defmodule CodeTest do
234234
assert trace_env.versioned_vars == %{{:result, Kernel} => 5, {:x, nil} => 1, {:y, nil} => 4}
235235
end
236236

237+
test "eval_quoted_with_env/3 with defguard" do
238+
require Integer
239+
env = Code.env_for_eval(__ENV__)
240+
quoted = quote do: Integer.is_even(1)
241+
{false, binding, env} = Code.eval_quoted_with_env(quoted, [], env, prune_binding: true)
242+
assert binding == []
243+
assert Macro.Env.vars(env) == []
244+
end
245+
237246
test "compile_file/1" do
238247
assert Code.compile_file(fixture_path("code_sample.exs")) == []
239248
refute fixture_path("code_sample.exs") in Code.required_files()

0 commit comments

Comments
 (0)