Skip to content

Print intermediate results of dbg for blocks #14218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 8 additions & 33 deletions lib/elixir/lib/macro.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2645,17 +2645,18 @@ defmodule Macro do
dbg_boolean_tree(ast, result_var, [])
end

defp dbg_ast_to_debuggable({:__block__, _meta, exprs} = ast, _env) when exprs != [] do
acc_var = unique_var(:acc, __MODULE__)
defp dbg_ast_to_debuggable({:__block__, _meta, exprs}, _env) when exprs != [] do
result_var = unique_var(:result, __MODULE__)
count = length(exprs)

Enum.with_index(exprs, fn expr, i ->
tag = if i + 1 == count, do: :value, else: :multi_value

[
quote do
unquote(acc_var) = []
unquote(dbg_block(ast, acc_var, result_var))
{:block, Enum.reverse(unquote(acc_var)), unquote(result_var)}
unquote(result_var) = unquote(expr)
{unquote(tag), unquote(Macro.escape(expr)), unquote(result_var)}
end
]
end)
end

defp dbg_ast_to_debuggable({:case, _meta, [expr, [do: clauses]]} = ast, _env) do
Expand Down Expand Up @@ -2833,18 +2834,6 @@ defmodule Macro do
[node | nodes]
end

defp dbg_block({:__block__, meta, exprs}, acc_var, result_var) do
modified_exprs =
Enum.map(exprs, fn expr ->
quote do
unquote(result_var) = unquote(expr)
unquote(acc_var) = [{unquote(escape(expr)), unquote(result_var)} | unquote(acc_var)]
end
end)

{:__block__, meta, modified_exprs}
end

# Made public to be called from Macro.dbg/3, so that we generate as little code
# as possible and call out into a function as soon as we can.
@doc false
Expand Down Expand Up @@ -2884,20 +2873,6 @@ defmodule Macro do
{[first_formatted | rest_formatted], result}
end

defp dbg_format_ast_to_debug({:block, components, value}, options) do
formatted =
[
dbg_maybe_underline("Code block", options),
":\n(\n",
Enum.map(components, fn {ast, value} ->
[" ", dbg_format_ast_with_value(ast, value, options)]
end),
")\n"
]

{formatted, value}
end

defp dbg_format_ast_to_debug({:case_argument, expr_ast, expr_value}, options) do
formatted = [
dbg_maybe_underline("Case argument", options),
Expand Down
29 changes: 23 additions & 6 deletions lib/elixir/test/elixir/macro_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -506,12 +506,29 @@ defmodule MacroTest do
assert formatted =~ "macro_test.exs"

assert formatted =~ """
Code block:
(
a = 1 #=> 1
b = a + 2 #=> 3
a + b #=> 4
)
a = 1 #=> 1
b = a + 2 #=> 3
a + b #=> 4
"""
end

test "block that raises" do
{result, formatted} =
dbg_format_no_newline(
(
a = 1
b = a - 1
a / b
)
)

assert result == %ArithmeticError{}

assert formatted =~ "macro_test.exs"

assert formatted =~ """
a = 1 #=> 1
b = a - 1 #=> 0
"""
end

Expand Down
Loading