Skip to content

Disable migrate_unless within defmacro #13850

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 3 commits into from
Sep 22, 2024
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
10 changes: 10 additions & 0 deletions lib/elixir/lib/code/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,16 @@ defmodule Code.Formatter do
binary_op_to_algebra(:in, "not in", meta, left, right, context, state)
end

# disable migrate_unless within defmacro
defp quoted_to_algebra(
{atom, _, [{:unless, _, _}, _]} = ast,
context,
%{migrate_unless: true} = state
)
when atom in [:defmacro, :defmacrop] do
quoted_to_algebra(ast, context, %{state | migrate_unless: false})
end

# rewrite unless as if!
defp quoted_to_algebra(
{:unless, meta, [condition, block]},
Expand Down
20 changes: 8 additions & 12 deletions lib/elixir/lib/macro.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2652,17 +2652,16 @@ defmodule Macro do
end
end

defp dbg_ast_to_debuggable({op, meta, [condition_ast, clauses]} = ast, env)
when op in [:if, :unless] do
case Macro.Env.lookup_import(env, {op, 2}) do
defp dbg_ast_to_debuggable({:if, meta, [condition_ast, clauses]} = ast, env) do
case Macro.Env.lookup_import(env, {:if, 2}) do
[macro: Kernel] ->
condition_result_var = unique_var(:condition_result, __MODULE__)

quote do
unquote(condition_result_var) = unquote(condition_ast)
result = unquote({op, meta, [condition_result_var, clauses]})
result = unquote({:if, meta, [condition_result_var, clauses]})

{unquote(op), unquote(escape(ast)), unquote(escape(condition_ast)),
{:if, unquote(escape(ast)), unquote(escape(condition_ast)),
unquote(condition_result_var), result}
end

Expand Down Expand Up @@ -2809,18 +2808,15 @@ defmodule Macro do
end

defp dbg_format_ast_to_debug(
{op, ast, condition_ast, condition_result, result},
{:if, ast, condition_ast, condition_result, result},
options
)
when op in [:if, :unless] do
op_name = String.capitalize(Atom.to_string(op))

) do
formatted = [
dbg_maybe_underline("#{op_name} condition", options),
dbg_maybe_underline("If condition", options),
":\n",
dbg_format_ast_with_value(condition_ast, condition_result, options),
?\n,
dbg_maybe_underline("#{op_name} expression", options),
dbg_maybe_underline("If expression", options),
":\n",
dbg_format_ast_with_value(ast, result, options)
]
Expand Down
30 changes: 0 additions & 30 deletions lib/elixir/test/elixir/macro_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -652,36 +652,6 @@ defmodule MacroTest do
"""
end

test "unless expression" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd go ahead and remove the dbg feature altogether.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one specific to unless.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x = false
map = %{a: 5, b: 1}

{result, formatted} =
dbg_format(
unless true and x do
map[:a] * 2
else
map[:b]
end
)

assert result == 10

assert formatted =~ "macro_test.exs"

assert formatted =~ """
Unless condition:
true and x #=> false

Unless expression:
unless true and x do
map[:a] * 2
else
map[:b]
end #=> 10
"""
end

test "with \"syntax_colors: []\" it doesn't print any color sequences" do
{_result, formatted} = dbg_format("hello")
refute formatted =~ "\e["
Expand Down
Loading