-
Notifications
You must be signed in to change notification settings - Fork 3.4k
implement dbg for if expressions #13603
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
implement dbg for if expressions #13603
Conversation
lib/elixir/lib/macro.ex
Outdated
@@ -2642,6 +2642,15 @@ defmodule Macro do | |||
end | |||
end | |||
|
|||
defp dbg_ast_to_debuggable({:if, _meta, [condition_ast, _clauses]} = ast) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we might need to check the meta here - since if
is not a special form unlike case
& co, it can be redefined.
import Kernel, except: [if: 2]
import MyOtherIf
We'd need a test for this case too I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did that but maybe there is a better way to implement it?
452f341
to
5fc7161
Compare
lib/elixir/lib/macro.ex
Outdated
if_from_kernel? = | ||
for {Kernel, macros} <- env.macros, {:if, 2} <- macros do | ||
true | ||
end == [true] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@josevalim do we need to do this because if
is not a special form (like case
and cond
)?
If we need to do this, let's do it like this
if_from_kernel? = | |
for {Kernel, macros} <- env.macros, {:if, 2} <- macros do | |
true | |
end == [true] | |
if_from_kernel? = Enum.any?(env.macros, fn {mod, macros} -> mod == Kernel and {:if, 2} in macros end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is Macro.Env.lookup_import which we should use instead.
lib/elixir/lib/macro.ex
Outdated
dbg_format_ast_with_value(condition_ast, condition_result, options), | ||
?\n, | ||
dbg_maybe_underline("If expression", options), | ||
" (#{if result, do: "do", else: "else"} clause executed):\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we call the "do clause" the "truthy clause" instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, I would just print the value of the condition and the value we return. It is easy to infer which clause was executed from that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also think it's enough - I was trying to make it similar to other implementations.
I also wonder if the condition should be split on operators - but this may require a bit of refactoring to this module if condition:
true #=> true
true and x #=> true
If expression:
|
I think it would make sense, as |
@sabiwara yes, this pr is ready. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thank you for the great proposal and work! 💜
Great work @dkuku thank you! 💟 |
@dkuku we discussed and agreed to handle blocks (just plain :block tuples, not [do: block] ones). |
@sabiwara cool, I will do that |
Dbg currently is not showing any bindings when executed on if statement so it's very hard to guess what's going on:
This pr extends dbg to support if statements:
I proposed this on elixir forum with other tweaks to dbg, but did not get any answers.