Skip to content

Commit 9b34ec2

Browse files
authored
Print intermediate results of dbg for pipes (#14685)
1 parent 841f426 commit 9b34ec2

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
lines changed

lib/elixir/lib/macro.ex

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,34 +2639,29 @@ defmodule Macro do
26392639
# Pipelines.
26402640
defp dbg_ast_to_debuggable({:|>, _meta, _args} = pipe_ast, _env) do
26412641
value_var = unique_var(:value, __MODULE__)
2642-
values_acc_var = unique_var(:values, __MODULE__)
26432642

2644-
[start_ast | rest_asts] = asts = for {ast, 0} <- unpipe(pipe_ast), do: ast
2645-
rest_asts = Enum.map(rest_asts, &pipe(value_var, &1, 0))
2643+
[start_ast | rest_asts] = for {ast, 0} <- unpipe(pipe_ast), do: ast
2644+
piped_rest_asts = Enum.map(rest_asts, &{&1, pipe(value_var, &1, 0)})
26462645

2647-
initial_acc =
2646+
first_entry =
26482647
quote do
26492648
unquote(value_var) = unquote(start_ast)
2650-
unquote(values_acc_var) = [unquote(value_var)]
2649+
{:multi_value, unquote(escape(start_ast)), unquote(value_var)}
26512650
end
26522651

2653-
values_ast =
2654-
for step_ast <- rest_asts, reduce: initial_acc do
2655-
ast_acc ->
2656-
quote do
2657-
unquote(ast_acc)
2658-
unquote(value_var) = unquote(step_ast)
2659-
unquote(values_acc_var) = [unquote(value_var) | unquote(values_acc_var)]
2660-
end
2661-
end
2652+
len = length(piped_rest_asts)
26622653

2663-
[
2664-
quote do
2665-
unquote(values_ast)
2654+
pipe_entries =
2655+
Enum.with_index(piped_rest_asts, fn {original_ast, step_ast}, i ->
2656+
tag = if i + 1 == len, do: :pipe_end, else: :pipe
26662657

2667-
{:pipe, unquote(escape(asts)), Enum.reverse(unquote(values_acc_var))}
2668-
end
2669-
]
2658+
quote do
2659+
unquote(value_var) = unquote(step_ast)
2660+
{unquote(tag), unquote(escape(original_ast)), unquote(value_var)}
2661+
end
2662+
end)
2663+
2664+
[first_entry | pipe_entries]
26702665
end
26712666

26722667
dbg_decomposed_binary_operators = [:&&, :||, :and, :or]
@@ -2886,18 +2881,19 @@ defmodule Macro do
28862881
result
28872882
end
28882883

2889-
defp dbg_format_ast_to_debug({:pipe, code_asts, values}, options) do
2890-
result = List.last(values)
2891-
code_strings = Enum.map(code_asts, &to_string_with_colors(&1, options))
2892-
[{first_ast, first_value} | asts_with_values] = Enum.zip(code_strings, values)
2893-
first_formatted = [dbg_format_ast(first_ast), " ", inspect(first_value, options), ?\n]
2884+
defp dbg_format_ast_to_debug({:pipe, code_ast, value}, options) do
2885+
formatted = [
2886+
[:faint, "|> ", :reset],
2887+
dbg_format_ast_with_value_no_newline(code_ast, value, options)
2888+
]
2889+
2890+
{formatted, value}
2891+
end
28942892

2895-
rest_formatted =
2896-
Enum.map(asts_with_values, fn {code_ast, value} ->
2897-
[:faint, "|> ", :reset, dbg_format_ast(code_ast), " ", inspect(value, options), ?\n]
2898-
end)
2893+
defp dbg_format_ast_to_debug({:pipe_end, code_ast, value}, options) do
2894+
{formatted, value} = dbg_format_ast_to_debug({:pipe, code_ast, value}, options)
28992895

2900-
{[first_formatted | rest_formatted], result}
2896+
{[formatted, ?\n], value}
29012897
end
29022898

29032899
defp dbg_format_ast_to_debug({:case_argument, expr_ast, expr_value}, options) do

lib/elixir/test/elixir/macro_test.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,28 @@ defmodule MacroTest do
444444
assert formatted =~ ~r/[^\n]\n\n$/
445445
end
446446

447+
test "pipeline on multiple lines that raises" do
448+
{result, formatted} =
449+
dbg_format_no_newline(
450+
abc()
451+
|> tl()
452+
|> tl()
453+
|> tl()
454+
|> tl()
455+
)
456+
457+
assert %ArgumentError{} = result
458+
459+
assert formatted =~ "macro_test.exs"
460+
461+
assert formatted =~ """
462+
abc() #=> [:a, :b, :c]
463+
|> tl() #=> [:b, :c]
464+
|> tl() #=> [:c]
465+
|> tl() #=> []
466+
"""
467+
end
468+
447469
test "simple boolean expressions" do
448470
{result, formatted} = dbg_format(:rand.uniform() < 0.0 and length([]) == 0)
449471
assert result == false

0 commit comments

Comments
 (0)