Skip to content

Commit

Permalink
Avoid double tracing on captures
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed May 25, 2024
1 parent ebe3474 commit 73d7762
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 51 deletions.
2 changes: 0 additions & 2 deletions lib/elixir/src/elixir_expand.erl
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,6 @@ resolve_super(Meta, Arity, E) ->
expand_fn_capture(Meta, Arg, S, E) ->
case elixir_fn:capture(Meta, Arg, S, E) of
{{remote, Remote, Fun, Arity}, RequireMeta, DotMeta, SE, EE} ->
is_atom(Remote) andalso
elixir_env:trace({remote_function, RequireMeta, Remote, Fun, Arity}, E),
AttachedMeta = attach_runtime_module(Remote, RequireMeta, S, E),
{{'&', Meta, [{'/', [], [{{'.', DotMeta, [Remote, Fun]}, AttachedMeta, []}, Arity]}]}, SE, EE};
{{local, Fun, Arity}, _, _, _SE, #{function := nil}} ->
Expand Down
17 changes: 0 additions & 17 deletions lib/elixir/test/elixir/kernel/expansion_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -621,23 +621,6 @@ defmodule Kernel.ExpansionTest do
assert expand(quote(do: quote(do: hello)), []) == {:{}, [], [:hello, [], __MODULE__]}
end

test "expand bind_quoted once" do
expand_env(
quote do
var = 123
quote(bind_quoted: [var: var], do: var)
end,
__ENV__,
[]
)
|> elem(0)
|> Macro.prewalk(fn
{:var, [version: 0], Kernel.ExpansionTest} -> :ok
{:var, _, Kernel.ExpansionTest} = invalid -> flunk("unexpected node #{inspect(invalid)}")
node -> node
end)
end

test "raises if the :bind_quoted option is invalid" do
assert_compile_error(~r"invalid :bind_quoted for quote", fn ->
expand(quote(do: quote(bind_quoted: self(), do: :ok)))
Expand Down
88 changes: 56 additions & 32 deletions lib/elixir/test/elixir/kernel/tracers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ defmodule Kernel.TracersTest do
Foo
""")

assert_receive {:start, %{lexical_tracker: pid}} when is_pid(pid)
assert_receive {:stop, %{lexical_tracker: pid}} when is_pid(pid)
assert_received {:start, %{lexical_tracker: pid}} when is_pid(pid)
assert_received {:stop, %{lexical_tracker: pid}} when is_pid(pid)
end

test "traces alias references" do
compile_string("""
Foo
""")

assert_receive {{:alias_reference, meta, Foo}, _}
assert_received {{:alias_reference, meta, Foo}, _}
assert meta[:line] == 1
assert meta[:column] == 1
end
Expand All @@ -50,17 +50,17 @@ defmodule Kernel.TracersTest do
Bar
""")

assert_receive {{:alias, meta, Hello.World, World, []}, _}
assert_received {{:alias, meta, Hello.World, World, []}, _}
assert meta[:line] == 1
assert meta[:column] == 1
assert_receive {{:alias_expansion, meta, World, Hello.World}, _}
assert_received {{:alias_expansion, meta, World, Hello.World}, _}
assert meta[:line] == 2
assert meta[:column] == 1

assert_receive {{:alias, meta, Foo, Bar, [as: Bar, warn: true]}, _}
assert_received {{:alias, meta, Foo, Bar, [as: Bar, warn: true]}, _}
assert meta[:line] == 4
assert meta[:column] == 1
assert_receive {{:alias_expansion, meta, Bar, Foo}, _}
assert_received {{:alias_expansion, meta, Bar, Foo}, _}
assert meta[:line] == 5
assert meta[:column] == 1
end
Expand All @@ -72,15 +72,19 @@ defmodule Kernel.TracersTest do
{1, ""} = parse("1")
""")

assert_receive {{:import, meta, Integer, only: [is_odd: 1, parse: 1]}, _}
assert_received {{:import, meta, Integer, only: [is_odd: 1, parse: 1]}, _}
assert meta[:line] == 1
assert meta[:column] == 1

assert_receive {{:imported_macro, meta, Integer, :is_odd, 1}, _}
assert_received {{:imported_macro, meta, Integer, :is_odd, 1}, _}
assert meta[:line] == 2
assert meta[:column] == 8

assert_receive {{:imported_function, meta, Integer, :parse, 1}, _}
assert_received {{:imported_function, meta, Integer, :parse, 1}, _}
assert meta[:line] == 3
assert meta[:column] == 11

assert_received {{:remote_function, meta, Integer, :parse, 1}, _}
assert meta[:line] == 3
assert meta[:column] == 11
end
Expand All @@ -92,15 +96,15 @@ defmodule Kernel.TracersTest do
&parse/1
""")

assert_receive {{:import, meta, Integer, only: [is_odd: 1, parse: 1]}, _}
assert_received {{:import, meta, Integer, only: [is_odd: 1, parse: 1]}, _}
assert meta[:line] == 1
assert meta[:column] == 1

assert_receive {{:imported_macro, meta, Integer, :is_odd, 1}, _}
assert_received {{:imported_macro, meta, Integer, :is_odd, 1}, _}
assert meta[:line] == 2
assert meta[:column] == 2

assert_receive {{:imported_function, meta, Integer, :parse, 1}, _}
assert_received {{:imported_function, meta, Integer, :parse, 1}, _}
assert meta[:line] == 3
assert meta[:column] == 2
end
Expand All @@ -110,7 +114,7 @@ defmodule Kernel.TracersTest do
%URI{path: "/"}
""")

assert_receive {{:struct_expansion, meta, URI, [:path]}, _}
assert_received {{:struct_expansion, meta, URI, [:path]}, _}
assert meta[:line] == 1
assert meta[:column] == 1
end
Expand All @@ -123,15 +127,15 @@ defmodule Kernel.TracersTest do
"foo" = Atom.to_string(:foo)
""")

assert_receive {{:remote_macro, meta, Integer, :is_odd, 1}, _}
assert_received {{:remote_macro, meta, Integer, :is_odd, 1}, _}
assert meta[:line] == 2
assert meta[:column] == 16

assert_receive {{:remote_function, meta, Integer, :parse, 1}, _}
assert_received {{:remote_function, meta, Integer, :parse, 1}, _}
assert meta[:line] == 3
assert meta[:column] == 19

assert_receive {{:remote_function, meta, Atom, :to_string, 1}, _}
assert_received {{:remote_function, meta, Atom, :to_string, 1}, _}
assert meta[:line] == 4
assert meta[:column] == 14
end
Expand All @@ -143,11 +147,11 @@ defmodule Kernel.TracersTest do
&Integer.parse/1
""")

assert_receive {{:remote_macro, meta, Integer, :is_odd, 1}, _}
assert_received {{:remote_macro, meta, Integer, :is_odd, 1}, _}
assert meta[:line] == 2
assert meta[:column] == 10

assert_receive {{:remote_function, meta, Integer, :parse, 1}, _}
assert_received {{:remote_function, meta, Integer, :parse, 1}, _}
assert meta[:line] == 3
assert meta[:column] == 10
end
Expand All @@ -161,11 +165,11 @@ defmodule Kernel.TracersTest do
end
""")

assert_receive {{:local_macro, meta, :foo, 1}, _}
assert_received {{:local_macro, meta, :foo, 1}, _}
assert meta[:line] == 4
assert meta[:column] == 21

assert_receive {{:local_function, meta, :bar, 1}, _}
assert_received {{:local_function, meta, :bar, 1}, _}
assert meta[:line] == 4
assert meta[:column] == 32
after
Expand All @@ -182,11 +186,11 @@ defmodule Kernel.TracersTest do
end
""")

assert_receive {{:local_macro, meta, :foo, 1}, _}
assert_received {{:local_macro, meta, :foo, 1}, _}
assert meta[:line] == 4
assert meta[:column] == 21

assert_receive {{:local_function, meta, :bar, 1}, _}
assert_received {{:local_function, meta, :bar, 1}, _}
assert meta[:line] == 4
assert meta[:column] == 29
after
Expand All @@ -201,8 +205,8 @@ defmodule Kernel.TracersTest do
end
""")

assert_receive {:defmodule, %{module: Sample, function: nil}}
assert_receive {{:on_module, <<_::binary>>, :none}, %{module: Sample, function: nil}}
assert_received {:defmodule, %{module: Sample, function: nil}}
assert_received {{:on_module, <<_::binary>>, :none}, %{module: Sample, function: nil}}
after
:code.purge(Sample)
:code.delete(Sample)
Expand All @@ -213,8 +217,8 @@ defmodule Kernel.TracersTest do
Module.create(Sample, :ok, __ENV__)
""")

assert_receive {:defmodule, %{module: Sample, function: nil}}
assert_receive {{:on_module, <<_::binary>>, :none}, %{module: Sample, function: nil}}
assert_received {:defmodule, %{module: Sample, function: nil}}
assert_received {{:on_module, <<_::binary>>, :none}, %{module: Sample, function: nil}}
after
:code.purge(Sample)
:code.delete(Sample)
Expand All @@ -226,7 +230,7 @@ defmodule Kernel.TracersTest do
"foo\#{arg}"
""")

assert_receive {{:remote_macro, meta, Kernel, :to_string, 1}, _env}
assert_received {{:remote_macro, meta, Kernel, :to_string, 1}, _env}
assert meta[:from_interpolation]
end

Expand All @@ -236,7 +240,7 @@ defmodule Kernel.TracersTest do
foo[:bar]
""")

assert_receive {{:remote_function, meta, Access, :get, 2}, _env}
assert_received {{:remote_function, meta, Access, :get, 2}, _env}
assert meta[:from_brackets]

compile_string("""
Expand All @@ -248,17 +252,37 @@ defmodule Kernel.TracersTest do
end
""")

assert_receive {{:remote_function, meta, Access, :get, 2}, _env}
assert_received {{:remote_function, meta, Access, :get, 2}, _env}
assert meta[:from_brackets]

compile_string("""
%{bar: 3}[:bar]
""")

assert_receive {{:remote_function, meta, Access, :get, 2}, _env}
assert_received {{:remote_function, meta, Access, :get, 2}, _env}
assert meta[:from_brackets]
end

test "does not trace bind quoted twice" do
compile_string("""
quote bind_quoted: [foo: List.flatten([])] do
foo
end
""")

assert_received {{:remote_function, _, List, :flatten, 1}, _}
refute_received {{:remote_function, _, List, :flatten, 1}, _}
end

test "does not trace captures twice" do
compile_string("""
&List.flatten/1
""")

assert_received {{:remote_function, _, List, :flatten, 1}, _}
refute_received {{:remote_function, _, List, :flatten, 1}, _}
end

"""
# Make sure this module is compiled with column information
defmodule MacroWithColumn do
Expand All @@ -278,7 +302,7 @@ defmodule Kernel.TracersTest do
MacroWithColumn.some_macro(["hello", "world", "!"])
""")

assert_receive {{:alias_reference, meta, Enum}, _env}
assert_received {{:alias_reference, meta, Enum}, _env}
refute meta[:column]
end
end

0 comments on commit 73d7762

Please sign in to comment.