Skip to content

Fixes #12299 #12324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions lib/elixir/lib/macro.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1801,16 +1801,27 @@ defmodule Macro do
do: {:filename.dirname(env.file), true}

defp do_expand_once({:__ENV__, _, atom}, env) when is_atom(atom) do
env = update_in(env.versioned_vars, &maybe_escape_map/1)
{maybe_escape_map(env), true}
env =
Map.new(env, fn
# TODO handle unescapable values which are deeper in the structure
{key, value} when is_reference(x) or is_pid(x) or is_port(x) -> {key, nil}
{key, value} -> {key, value}
end)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this is much more expensive. I think you can do:

env = put_in(env.lexical_tracker, nil)

and that should be it. We don't have any improper fields anywhere else. :)

If you want, you can add a test that traverses the result of Macro.expand recursively and verifies there is no improper data. :)


{escape(env), true}
end

defp do_expand_once({{:., _, [{:__ENV__, _, atom}, field]}, _, []} = original, env)
defp do_expand_once({{:., _, [{:__ENV__, _, atom} = the_env, field]}, _, []} = original, env)
when is_atom(atom) and is_atom(field) do
if Map.has_key?(env, field) do
{maybe_escape_map(Map.get(env, field)), true}
else
{original, false}
case env do
%{^field => value} when is_reference(x) or is_pid(x) or is_port(x) ->
{nil, true}

%{^field => value} ->
{escape(value), true}

_ ->
{original, false}
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/elixir/test/elixir/macro_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ defmodule MacroTest do
end

test "env" do
env = %{__ENV__ | line: 0}
env = %{__ENV__ | line: 0, lexical_tracker: self()}

expanded = Macro.expand_once(quote(do: __ENV__), env)
assert Macro.validate(expanded) == :ok
Expand Down