Skip to content

Commit dbf35d5

Browse files
committed
Attempts to solve #13593
Attempts to solve an issue that causes `Macro.escape/2` to crash when given certain tuples. Note that, `Macro.escape({:quote, [column: 10], [:foo]})` still produces : `{:{}, [], [:quote, [], [:foo]]}` which is arguably wrong.
1 parent f0e97d0 commit dbf35d5

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

lib/elixir/src/elixir_quote.erl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,10 @@ update_last([H | T], F) -> [H | update_last(T, F)].
538538

539539
keyfind(Key, Meta) ->
540540
lists:keyfind(Key, 1, Meta).
541-
keydelete(Key, Meta) ->
542-
lists:keydelete(Key, 1, Meta).
541+
keydelete(Key, Meta) when is_list(Meta) ->
542+
lists:keydelete(Key, 1, Meta);
543+
keydelete(_Key, Meta) ->
544+
Meta.
543545
keystore(_Key, Meta, nil) ->
544546
Meta;
545547
keystore(Key, Meta, Value) ->
@@ -571,4 +573,4 @@ annotate_def({'when', Meta, [Left, Right]}, Context) ->
571573
annotate_def({Fun, Meta, Args}, Context) ->
572574
{Fun, keystore(context, Meta, Context), Args};
573575
annotate_def(Other, _Context) ->
574-
Other.
576+
Other.

lib/elixir/test/elixir/macro_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ defmodule MacroTest do
3838
assert Macro.escape({:a, {1, 2, 3}, :c}) == {:{}, [], [:a, {:{}, [], [1, 2, 3]}, :c]}
3939
end
4040

41+
test "correctly escapes tuples where the first element is :quote" do
42+
assert Macro.escape({:quote, :foo, [:bar]}) == {:{}, [], [:quote, :foo, [:bar]]}
43+
assert Macro.escape({:quote, :foo, [:bar, :baz]}) == {:{}, [], [:quote, :foo, [:bar, :baz]]}
44+
end
45+
4146
test "escapes maps" do
4247
assert Macro.escape(%{a: 1}) == {:%{}, [], [a: 1]}
4348
end

0 commit comments

Comments
 (0)