Skip to content
Merged
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
4 changes: 2 additions & 2 deletions lib/elixir/lib/macro.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1832,13 +1832,13 @@ defmodule Macro do
defp do_expand_once({:__DIR__, _, atom}, env) when is_atom(atom),
do: {:filename.dirname(env.file), true}

defp do_expand_once({:__ENV__, _, atom}, env) when is_atom(atom) do
defp do_expand_once({:__ENV__, _, atom}, env) when is_atom(atom) and env.context != :match do
env = update_in(env.versioned_vars, &maybe_escape_map/1)
{maybe_escape_map(env), true}
end

defp do_expand_once({{:., _, [{:__ENV__, _, atom}, field]}, _, []} = original, env)
when is_atom(atom) and is_atom(field) do
when is_atom(atom) and is_atom(field) and env.context != :match do
if Map.has_key?(env, field) do
{maybe_escape_map(Map.get(env, field)), true}
else
Expand Down
10 changes: 10 additions & 0 deletions lib/elixir/test/elixir/macro_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ defmodule MacroTest do
assert Code.eval_quoted(expanded) == {env.versioned_vars, []}
end

test "env in :match context does not expand" do
env = %{__ENV__ | line: 0, lexical_tracker: self(), context: :match}

expanded = Macro.expand_once(quote(do: __ENV__), env)
assert expanded == quote(do: __ENV__)

expanded = Macro.expand_once(quote(do: __ENV__.file), env)
assert expanded == quote(do: __ENV__.file)
end

defmacro local_macro(), do: raise("ignored")

test "vars" do
Expand Down
22 changes: 22 additions & 0 deletions lib/ex_unit/test/ex_unit/assertions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,28 @@ defmodule ExUnit.AssertionsTest do
end
end

test "assert match with __ENV__ in the pattern" do
message =
ExUnit.CaptureIO.capture_io(:stderr, fn ->
assert_raise CompileError, fn ->
Code.eval_string("""
defmodule EnvMatch do
import ExUnit.Assertions

def run do
assert __ENV__ = %{}
end
end
""")
end
end)

assert message =~ "invalid pattern in match, __ENV__ is not allowed in matches"
after
:code.purge(EnvMatch)
:code.delete(EnvMatch)
end

test "assert match?" do
true = assert match?({2, 1}, Value.tuple())

Expand Down