Skip to content

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

Merged

Conversation

dkuku
Copy link
Contributor

@dkuku dkuku commented May 26, 2024

Dbg currently is not showing any bindings when executed on if statement so it's very hard to guess what's going on:

if a do
  b
else
  c
end #=> 2

This pr extends dbg to support if statements:

 If condition:
 true and x #=> true
 
 If expression (do clause executed):
 if true and x do
     map[:a] * 2
 else
    map[:b]
 end #=> 10

I proposed this on elixir forum with other tweaks to dbg, but did not get any answers.

@@ -2642,6 +2642,15 @@ defmodule Macro do
end
end

defp dbg_ast_to_debuggable({:if, _meta, [condition_ast, _clauses]} = ast) do
Copy link
Contributor

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

Copy link
Contributor Author

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?

@dkuku dkuku force-pushed the dk_implement_dbg_for_if_expressions branch from 452f341 to 5fc7161 Compare May 27, 2024 05:13
Comment on lines 2646 to 2649
if_from_kernel? =
for {Kernel, macros} <- env.macros, {:if, 2} <- macros do
true
end == [true]
Copy link
Member

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

Suggested change
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

Copy link
Member

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.

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",
Copy link
Member

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?

Copy link
Member

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.

Copy link
Contributor Author

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.

@dkuku
Copy link
Contributor Author

dkuku commented May 27, 2024

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:

@sabiwara
Copy link
Contributor

I also wonder if the condition should be split on operators - but this may require a bit of refactoring to this module

I think it would make sense, as && and if are very close in nature. But this can perhaps be done as a follow-up pull-request?

@dkuku
Copy link
Contributor Author

dkuku commented May 28, 2024

@sabiwara yes, this pr is ready.

@dkuku dkuku requested review from josevalim and whatyouhide May 28, 2024 05:26
Copy link
Contributor

@sabiwara sabiwara left a 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! 💜

@whatyouhide
Copy link
Member

Great work @dkuku thank you! 💟

@whatyouhide whatyouhide merged commit 7cf57ec into elixir-lang:main May 29, 2024
8 of 9 checks passed
@sabiwara
Copy link
Contributor

@dkuku we discussed and agreed to handle blocks (just plain :block tuples, not [do: block] ones).
If you are interested to work on this, feel free to open up a PR 🙂

@dkuku
Copy link
Contributor Author

dkuku commented May 29, 2024

@sabiwara cool, I will do that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants