Skip to content

Commit

Permalink
syntax tools: Fix location info of reverted lists
Browse files Browse the repository at this point in the history
The hidden `nil` node that is generated takes it's location from the
last element of the list, instead of the list  node itself, because that
generally represents the start of the list.

Similarly, the `cons` nodes are given the location of each element.

However, care must be taken to preserve the other annotations in `pos`,
which is really an `erl_anno` annotation.
  • Loading branch information
maxno-kivra committed Aug 31, 2023
1 parent 5bbee0e commit 4f2b413
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions lib/syntax_tools/src/erl_syntax.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2456,17 +2456,23 @@ list(Elements, Tail) when Elements =/= [] ->

revert_list(Node) ->
Pos = get_pos(Node),
P = list_prefix(Node),
S = case list_suffix(Node) of
Prefix = list_prefix(Node),
Suffix = case list_suffix(Node) of
none ->
revert_nil(set_pos(nil(), Pos));
S1 ->
S1
LastPos = get_pos(lists:last(Prefix)),
LastLocation = case erl_anno:end_location(LastPos) of
undefined -> erl_anno:location(LastPos);
Location -> Location
end,
revert_nil(set_pos(nil(), erl_anno:set_location(LastLocation, Pos)));
Suffix1 ->
Suffix1
end,
lists:foldr(fun (X, A) ->
{cons, Pos, X, A}
end,
S, P).
lists:foldr(fun (Head, Tail) ->
HeadPos = get_pos(Head),
HeadLocation = erl_anno:location(HeadPos),
{cons, erl_anno:set_location(HeadLocation, Pos), Head, Tail}
end, Suffix, Prefix).

%% =====================================================================
%% @doc Creates an abstract empty list. The result represents
Expand Down

0 comments on commit 4f2b413

Please sign in to comment.