Skip to content

Commit e2556fd

Browse files
committed
Clarify behaviour of Macro.to_string on invalid ASTs
Closes #13875.
1 parent 8dcc969 commit e2556fd

File tree

4 files changed

+27
-50
lines changed

4 files changed

+27
-50
lines changed

lib/elixir/lib/code/formatter.ex

+4-1
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,11 @@ defmodule Code.Formatter do
614614
end
615615

616616
# #PID's and #Ref's may appear on regular AST
617+
# Other foreign structures, such as maps and structs,
618+
# may appear from Macro.to_string, so we stick a limit,
619+
# although they won't be formatted accordingly.
617620
defp quoted_to_algebra(unknown, _context, state) do
618-
{inspect(unknown), state}
621+
{inspect(unknown, printable_limit: :infinity), state}
619622
end
620623

621624
## Blocks

lib/elixir/lib/macro.ex

+4
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,10 @@ defmodule Macro do
11251125
`Code.quoted_to_algebra/2` as a lower level function
11261126
with more control around formatting.
11271127
1128+
If the AST contains invalid nodes, this function will
1129+
attempt to inspect them, to aid debugging, although
1130+
the elements won't be formatted accordingly.
1131+
11281132
## Examples
11291133
11301134
iex> Macro.to_string(quote(do: foo.bar(1, 2, 3)))

lib/elixir/test/elixir/code_normalizer/quoted_ast_test.exs

+17-11
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,6 @@ defmodule Code.Normalizer.QuotedASTTest do
226226
assert quoted_to_string(quoted) <> "\n" == expected
227227
end
228228

229-
test "invalid block" do
230-
assert quoted_to_string({:__block__, [], {:bar, [], []}}) ==
231-
"{:__block__, [], {:bar, [], []}}"
232-
233-
assert quoted_to_string({:foo, [], [{:do, :ok}, :not_keyword]}) ==
234-
"foo({:do, :ok}, :not_keyword)"
235-
236-
assert quoted_to_string({:foo, [], [[{:do, :ok}, :not_keyword]]}) ==
237-
"foo([{:do, :ok}, :not_keyword])"
238-
end
239-
240229
test "not in" do
241230
assert quoted_to_string(quote(do: false not in [])) == "false not in []"
242231
end
@@ -738,6 +727,23 @@ defmodule Code.Normalizer.QuotedASTTest do
738727
end
739728
end
740729

730+
describe "quoted_to_algebra/2 with invalid" do
731+
test "block" do
732+
assert quoted_to_string({:__block__, [], {:bar, [], []}}) ==
733+
"{:__block__, [], {:bar, [], []}}"
734+
735+
assert quoted_to_string({:foo, [], [{:do, :ok}, :not_keyword]}) ==
736+
"foo({:do, :ok}, :not_keyword)"
737+
738+
assert quoted_to_string({:foo, [], [[{:do, :ok}, :not_keyword]]}) ==
739+
"foo([{:do, :ok}, :not_keyword])"
740+
end
741+
742+
test "ode" do
743+
assert quoted_to_string(1..3) == "1..3"
744+
end
745+
end
746+
741747
describe "quoted_to_algebra/2 does not escape" do
742748
test "sigils" do
743749
assert quoted_to_string(quote(do: ~s/a\nb\tc/), escape: false) == ~S"~s/a\nb\tc/"

lib/elixir/test/elixir/macro_test.exs

+2-38
Original file line numberDiff line numberDiff line change
@@ -680,44 +680,8 @@ defmodule MacroTest do
680680
assert Macro.to_string(quote do: hello(world)) == "hello(world)"
681681
end
682682

683-
test "large number literals" do
684-
# with quote
685-
assert Macro.to_string(quote do: 576_460_752_303_423_455) == "576_460_752_303_423_455"
686-
assert Macro.to_string(quote do: -576_460_752_303_423_455) == "-576_460_752_303_423_455"
687-
688-
# without quote
689-
assert Macro.to_string(576_460_752_303_423_455) == "576_460_752_303_423_455"
690-
assert Macro.to_string(-576_460_752_303_423_455) == "-576_460_752_303_423_455"
691-
end
692-
693-
test "charlists" do
694-
assert Macro.to_string(~c"foo") == ~s(~c"foo")
695-
end
696-
697-
defmodule HTML do
698-
defstruct [:string]
699-
700-
defimpl Inspect do
701-
def inspect(%{string: string}, _) do
702-
"~HTML[#{string}]"
703-
end
704-
end
705-
end
706-
707-
defmacro sigil_HTML({:<<>>, _, [string]}, []) do
708-
Macro.escape(%HTML{string: string})
709-
end
710-
711-
test "sigils" do
712-
assert Macro.to_string(quote(do: ~HTML[hi])) == ~S/~HTML[hi]/
713-
714-
assert Macro.to_string(
715-
quote do
716-
~HTML"""
717-
hi
718-
"""
719-
end
720-
) == ~s[~HTML"""\nhi\n"""]
683+
test "converts invalid AST with inspect" do
684+
assert Macro.to_string(quote do: unquote(1..3)) == "1..3"
721685
end
722686
end
723687

0 commit comments

Comments
 (0)