From c40182ebd79456df1f40f6d0adf22c5c5a17c8dc Mon Sep 17 00:00:00 2001 From: Daniel Kukula Date: Thu, 31 Jul 2025 00:33:28 +0200 Subject: [PATCH 1/2] Print intermediate results of dbg for pipes --- lib/elixir/lib/macro.ex | 56 +++++++++++++-------------- lib/elixir/test/elixir/macro_test.exs | 22 +++++++++++ 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/lib/elixir/lib/macro.ex b/lib/elixir/lib/macro.ex index b09a418f7e1..2cadcf63148 100644 --- a/lib/elixir/lib/macro.ex +++ b/lib/elixir/lib/macro.ex @@ -2639,34 +2639,29 @@ defmodule Macro do # Pipelines. defp dbg_ast_to_debuggable({:|>, _meta, _args} = pipe_ast, _env) do value_var = unique_var(:value, __MODULE__) - values_acc_var = unique_var(:values, __MODULE__) - [start_ast | rest_asts] = asts = for {ast, 0} <- unpipe(pipe_ast), do: ast - rest_asts = Enum.map(rest_asts, &pipe(value_var, &1, 0)) + [start_ast | rest_asts] = for {ast, 0} <- unpipe(pipe_ast), do: ast + piped_rest_asts = Enum.map(rest_asts, &{&1, pipe(value_var, &1, 0)}) - initial_acc = + first_entry = quote do unquote(value_var) = unquote(start_ast) - unquote(values_acc_var) = [unquote(value_var)] + {:multi_value, unquote(escape(start_ast)), unquote(value_var)} end - values_ast = - for step_ast <- rest_asts, reduce: initial_acc do - ast_acc -> - quote do - unquote(ast_acc) - unquote(value_var) = unquote(step_ast) - unquote(values_acc_var) = [unquote(value_var) | unquote(values_acc_var)] - end - end + len = length(piped_rest_asts) - [ - quote do - unquote(values_ast) + pipe_entries = + Enum.with_index(piped_rest_asts, fn {original_ast, step_ast}, i -> + tag = if i + 1 == len, do: :pipe_end, else: :pipe - {:pipe, unquote(escape(asts)), Enum.reverse(unquote(values_acc_var))} - end - ] + quote do + unquote(value_var) = unquote(step_ast) + {unquote(tag), unquote(escape(original_ast)), unquote(value_var)} + end + end) + + [first_entry | pipe_entries] end dbg_decomposed_binary_operators = [:&&, :||, :and, :or] @@ -2886,18 +2881,19 @@ defmodule Macro do result end - defp dbg_format_ast_to_debug({:pipe, code_asts, values}, options) do - result = List.last(values) - code_strings = Enum.map(code_asts, &to_string_with_colors(&1, options)) - [{first_ast, first_value} | asts_with_values] = Enum.zip(code_strings, values) - first_formatted = [dbg_format_ast(first_ast), " ", inspect(first_value, options), ?\n] + defp dbg_format_ast_to_debug({:pipe, code_ast, value}, options) do + formatted = [ + [:faint, "|> ", :reset], + dbg_format_ast_with_value_no_newline(code_ast, value, options) + ] + + {formatted, value} + end - rest_formatted = - Enum.map(asts_with_values, fn {code_ast, value} -> - [:faint, "|> ", :reset, dbg_format_ast(code_ast), " ", inspect(value, options), ?\n] - end) + defp dbg_format_ast_to_debug({:pipe_end, code_ast, value}, options) do + {formatted, value} = dbg_format_ast_to_debug({:pipe, code_ast, value}, options) - {[first_formatted | rest_formatted], result} + {[formatted | ?\n], value} end defp dbg_format_ast_to_debug({:case_argument, expr_ast, expr_value}, options) do diff --git a/lib/elixir/test/elixir/macro_test.exs b/lib/elixir/test/elixir/macro_test.exs index c3f752eea40..78c5a12610c 100644 --- a/lib/elixir/test/elixir/macro_test.exs +++ b/lib/elixir/test/elixir/macro_test.exs @@ -444,6 +444,28 @@ defmodule MacroTest do assert formatted =~ ~r/[^\n]\n\n$/ end + test "pipeline on multiple lines that raises" do + {result, formatted} = + dbg_format_no_newline( + abc() + |> tl() + |> tl() + |> tl() + |> tl() + ) + + assert %ArgumentError{} = result + + assert formatted =~ "macro_test.exs" + + assert formatted =~ """ + abc() #=> [:a, :b, :c] + |> tl() #=> [:b, :c] + |> tl() #=> [:c] + |> tl() #=> [] + """ + end + test "simple boolean expressions" do {result, formatted} = dbg_format(:rand.uniform() < 0.0 and length([]) == 0) assert result == false From c1aec54fb20925391ed8335d15fda2c1ba9d2bf7 Mon Sep 17 00:00:00 2001 From: Daniel Kukula Date: Thu, 31 Jul 2025 06:06:16 +0200 Subject: [PATCH 2/2] use proper list --- lib/elixir/lib/macro.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/elixir/lib/macro.ex b/lib/elixir/lib/macro.ex index 2cadcf63148..cbc58958e2c 100644 --- a/lib/elixir/lib/macro.ex +++ b/lib/elixir/lib/macro.ex @@ -2893,7 +2893,7 @@ defmodule Macro do defp dbg_format_ast_to_debug({:pipe_end, code_ast, value}, options) do {formatted, value} = dbg_format_ast_to_debug({:pipe, code_ast, value}, options) - {[formatted | ?\n], value} + {[formatted, ?\n], value} end defp dbg_format_ast_to_debug({:case_argument, expr_ast, expr_value}, options) do