Skip to content

Commit be6ed17

Browse files
lukaszsamsonjosevalim
authored andcommitted
Keep position meta on & capture special variables in expanded AST (#13607)
1 parent 79c19dc commit be6ed17

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/elixir/src/elixir_fn.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ validate(Meta, [{Pos, _} | _], Expected, E) ->
128128
validate(_Meta, [], _Pos, _E) ->
129129
[].
130130

131-
escape({'&', _, [Pos]}, _E, Dict) when is_integer(Pos), Pos > 0 ->
131+
escape({'&', Meta, [Pos]}, _E, Dict) when is_integer(Pos), Pos > 0 ->
132132
% Using a nil context here to emit warnings when variable is unused.
133133
% This might pollute user space but is unlikely because variables
134134
% named :"&1" are not valid syntax.
135-
Var = {list_to_atom([$& | integer_to_list(Pos)]), [], nil},
135+
Var = {list_to_atom([$& | integer_to_list(Pos)]), Meta, nil},
136136
{Var, orddict:store(Pos, Var, Dict)};
137137
escape({'&', Meta, [Pos]}, E, _Dict) when is_integer(Pos) ->
138138
file_error(Meta, E, ?MODULE, {invalid_arity_for_capture, Pos});

lib/elixir/test/elixir/kernel/expansion_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,12 @@ defmodule Kernel.ExpansionTest do
11331133
assert expand(quote(do: &unknown(&1, &2))) == {:&, [], [{:/, [], [{:unknown, [], nil}, 2]}]}
11341134
end
11351135

1136+
test "keeps position meta on & variables" do
1137+
assert expand(Code.string_to_quoted!("& &1")) ==
1138+
{:fn, [{:line, 1}],
1139+
[{:->, [{:line, 1}], [[{:"&1", [line: 1], nil}], {:"&1", [line: 1], nil}]}]}
1140+
end
1141+
11361142
test "expands remotes" do
11371143
assert expand(quote(do: &List.flatten/2)) ==
11381144
quote(do: &:"Elixir.List".flatten/2)

0 commit comments

Comments
 (0)