Skip to content

Keep position meta on & capture special variables in expanded AST #13607

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

Merged
merged 2 commits into from
May 27, 2024
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/src/elixir_fn.erl
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ validate(Meta, [{Pos, _} | _], Expected, E) ->
validate(_Meta, [], _Pos, _E) ->
[].

escape({'&', _, [Pos]}, _E, Dict) when is_integer(Pos), Pos > 0 ->
escape({'&', Meta, [Pos]}, _E, Dict) when is_integer(Pos), Pos > 0 ->
% Using a nil context here to emit warnings when variable is unused.
% This might pollute user space but is unlikely because variables
% named :"&1" are not valid syntax.
Var = {list_to_atom([$& | integer_to_list(Pos)]), [], nil},
Var = {list_to_atom([$& | integer_to_list(Pos)]), Meta, nil},
{Var, orddict:store(Pos, Var, Dict)};
escape({'&', Meta, [Pos]}, E, _Dict) when is_integer(Pos) ->
file_error(Meta, E, ?MODULE, {invalid_arity_for_capture, Pos});
Expand Down
6 changes: 6 additions & 0 deletions lib/elixir/test/elixir/kernel/expansion_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,12 @@ defmodule Kernel.ExpansionTest do
assert expand(quote(do: &unknown(&1, &2))) == {:&, [], [{:/, [], [{:unknown, [], nil}, 2]}]}
end

test "keeps position meta on & variables" do
assert expand(Code.string_to_quoted!("& &1")) ==
{:fn, [{:line, 1}],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure meta on fn and -> makes sense

[{:->, [{:line, 1}], [[{:"&1", [line: 1], nil}], {:"&1", [line: 1], nil}]}]}
end

test "expands remotes" do
assert expand(quote(do: &List.flatten/2)) ==
quote(do: &:"Elixir.List".flatten/2)
Expand Down