Skip to content

Commit

Permalink
Keep position meta on & capture special variables in expanded AST (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsamson authored and josevalim committed May 27, 2024
1 parent 79c19dc commit be6ed17
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
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}],
[{:->, [{: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

0 comments on commit be6ed17

Please sign in to comment.