Skip to content

Commit 5dfa6ae

Browse files
author
José Valim
committed
Ensure macro expansion does not expand on variables
1 parent 1fbb687 commit 5dfa6ae

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

lib/elixir/lib/macro.ex

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -588,12 +588,20 @@ defmodule Macro do
588588
do: { env, true, cache }
589589

590590
# Expand possible macro import invocation
591-
defp expand_once({ atom, meta, args } = original, env, cache) when is_atom(atom) and is_list(meta) do
592-
args = case is_atom(args) do
593-
true -> []
594-
false -> args
591+
defp expand_once({ atom, meta, context } = original, env, cache)
592+
when is_atom(atom) and is_list(meta) and is_atom(context) do
593+
if :lists.member({ atom, Keyword.get(meta, :counter, context) }, env.vars) do
594+
{ original, false, cache }
595+
else
596+
case expand_once({ atom, meta, [] }, env, cache) do
597+
{ _, true, _ } = exp -> exp
598+
{ _, false, cache } -> { original, false, cache }
599+
end
595600
end
601+
end
596602

603+
defp expand_once({ atom, meta, args } = original, env, cache)
604+
when is_atom(atom) and is_list(args) and is_list(meta) do
597605
case not is_partial?(args) do
598606
false -> { original, false, cache }
599607
true ->

lib/elixir/test/elixir/macro_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ defmodule MacroTest do
156156
assert Macro.expand_once(quote(do: local_macro), __ENV__) == :local_macro
157157
end
158158

159+
test :expand_once_checks_vars do
160+
local_macro = 1
161+
assert local_macro == 1
162+
quote = quote(hygiene: [vars: false], do: local_macro)
163+
assert Macro.expand_once(quote, __ENV__) == quote
164+
end
165+
159166
test :expand_once_with_imported_macro do
160167
temp_var = { :x, [temp: true], Kernel }
161168
assert Macro.expand_once(quote(do: 1 || false), __ENV__) == (quote context: Kernel do

0 commit comments

Comments
 (0)