Skip to content

Commit dc12d87

Browse files
committed
Bugfix: Macro.escape/1 properly escapes meta in :quote tuples
Close #14771
1 parent 2e7ee76 commit dc12d87

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

lib/elixir/src/elixir_quote.erl

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -340,23 +340,13 @@ quote(Expr, Q) ->
340340
do_quote({quote, Meta, [Arg]}, Q) when is_list(Meta) ->
341341
TArg = do_quote(Arg, Q#elixir_quote{unquote=false}),
342342

343-
NewMeta = case Q of
344-
#elixir_quote{op=add_context, context=Context} -> keystore(context, Meta, Context);
345-
_ -> Meta
346-
end,
347-
348-
{'{}', [], [quote, meta(NewMeta, Q), [TArg]]};
343+
{'{}', [], [quote, quote_meta(Meta, Q), [TArg]]};
349344

350345
do_quote({quote, Meta, [Opts, Arg]}, Q) when is_list(Meta) ->
351346
TOpts = do_quote(Opts, Q),
352347
TArg = do_quote(Arg, Q#elixir_quote{unquote=false}),
353348

354-
NewMeta = case Q of
355-
#elixir_quote{op=add_context, context=Context} -> keystore(context, Meta, Context);
356-
_ -> Meta
357-
end,
358-
359-
{'{}', [], [quote, meta(NewMeta, Q), [TOpts, TArg]]};
349+
{'{}', [], [quote, quote_meta(Meta, Q), [TOpts, TArg]]};
360350

361351
%
362352
do_quote({unquote, Meta, [Expr]}, #elixir_quote{unquote=true, shallow_validate=Validate}) when is_list(Meta) ->
@@ -598,6 +588,14 @@ argument_error(Message) ->
598588
meta(Meta, Q) ->
599589
generated(keep(keydelete(column, Meta), Q), Q).
600590

591+
quote_meta(Meta, Q) ->
592+
Meta1 = do_quote(Meta, Q),
593+
Meta2 = case Q of
594+
#elixir_quote{op=add_context, context=Context} -> keystore(context, Meta1, Context);
595+
_ -> Meta1
596+
end,
597+
meta(Meta2, Q).
598+
601599
generated(Meta, #elixir_quote{generated=true}) -> [{generated, true} | Meta];
602600
generated(Meta, #elixir_quote{generated=false}) -> Meta.
603601

lib/elixir/test/elixir/macro_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ defmodule MacroTest do
146146
assert Macro.escape({:quote, [], [[do: :foo]]}) == {:{}, [], [:quote, [], [[do: :foo]]]}
147147
end
148148

149+
test "escapes the content of :quote tuples" do
150+
assert Macro.escape({:quote, [%{}], [{}]}) ==
151+
{:{}, [], [:quote, [{:%{}, [], []}], [{:{}, [], []}]]}
152+
end
153+
149154
test "escape container when a reference cannot be escaped" do
150155
assert_raise ArgumentError, ~r"contains a reference", fn ->
151156
Macro.escape(%{re_pattern: {:re_pattern, 0, 0, 0, make_ref()}})

0 commit comments

Comments
 (0)