Skip to content
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

Translate with/1 as a closure #13299

Merged
merged 2 commits into from
Jan 30, 2024
Merged

Conversation

sabiwara
Copy link
Contributor

As discussed in #6738.

A nice side-effect is that we are able to remove a bunch of generated except from the raise clause.
The compiler and dialyzer should be able to track actual issues without false positives.

@sabiwara sabiwara marked this pull request as draft January 30, 2024 00:06
@@ -5,7 +5,6 @@ defmodule Kernel.WithTest do

test "basic with" do
assert with({:ok, res} <- ok(41), do: res) == 41
assert with(res <- four(), do: res + 10) == 14
Copy link
Contributor Author

@sabiwara sabiwara Jan 30, 2024

Choose a reason for hiding this comment

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

Was triggering a warning which I think is legit since this clause is always matching indeed:
Screenshot 2024-01-30 at 8 59 44

Maybe what could be improved could be to emit a different warning (e.g. "var <- clause will always match in with statement, use var = instead) during expansion and replace <- by a = so that we don't actually emit the else clause, WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

@sabiwara 👍 to omit the else clause in this case and generate leaner code. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in e408364

@sabiwara sabiwara marked this pull request as ready for review January 30, 2024 00:30
{{'<-', Meta, [ELeft, ERight]}, {SL, EL, true}};
case ELeft of
{Var, _, Ctx} when is_atom(Var), is_atom(Ctx) ->
{{'=', Meta, [ELeft, ERight]}, {SL, EL, HasMatch}};
Copy link
Contributor Author

@sabiwara sabiwara Jan 30, 2024

Choose a reason for hiding this comment

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

This is nice, it is even able to emit a warning in this case

Screenshot 2024-01-30 at 19 45 02

which is indeed on point.

Copy link
Member

Choose a reason for hiding this comment

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

fantastic

Copy link
Member

@josevalim josevalim Jan 30, 2024

Choose a reason for hiding this comment

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

However, we should probably do the conversion when translating to Erlang code, to avoid doing many changes to the original AST. The original AST is used for type checking and in error reports, so we want to preserve semantics when we can.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense! Addressed in 4e11ff2

Ann = ?ann(Meta),
{_, FunErlVar, SC} = elixir_erl_var:assign(Meta, S),
{_, ArgErlVar, SA} = elixir_erl_var:assign(Meta, SC),
FunAssign = {match, Ann, FunErlVar, {'fun', Ann, {clauses, TranslatedClauses}}},
Copy link
Member

Choose a reason for hiding this comment

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

Just to confirm, we generate one function for all else clauses, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is correct 👍
Including the :else_clause exception one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Basically here is how it gets translated:

Screenshot 2024-01-30 at 21 12 37

No closure if no else:

Screenshot 2024-01-30 at 21 12 53

Copy link
Member

@josevalim josevalim left a comment

Choose a reason for hiding this comment

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

Beautiful!

@sabiwara sabiwara merged commit 0b95ca4 into elixir-lang:main Jan 30, 2024
7 of 8 checks passed
@sabiwara sabiwara deleted the with-closure branch January 30, 2024 12:26
@lud-wj
Copy link
Contributor

lud-wj commented Oct 31, 2024

Hello @sabiwara

I would like to know if this change is supposed to fix a dialyzer problem when the else block of the with construct is repeated under all nested cases, but some pattern in the else block can only match one of the case result, leading dialyzer to warn about "the pattern can never match the type".

To me, this shows me that it would indeed fix the problem, but we still have it.

We use v1.17.2-erlang-26.2.5.2, and I see the "Ubuntu 20.04, Erlang/OTP 26.0" CI task failed. Is that fix available in OTP 26?

Thank you.

@sabiwara
Copy link
Contributor Author

sabiwara commented Oct 31, 2024

Hi @lud-wj, the main idea was to avoid emitting the same block of code repeatedly and be closer to what erlang's new maybe is doing.

I had hopes that this change would make dialyzer more useful and that we could now mark less code as generated, but unfortunately the kind of issues you mentioned didn't disappear so we had to mark most of it as generated again (making Dialyzer unable to track with types again).

#13659
#13869

The second one has not been released yet, it should be fixed with 1.17.4. Could you try the v1.17 or main branch, and if you still face the problem, open an issue? Thank you!

@lud-wj
Copy link
Contributor

lud-wj commented Nov 4, 2024

Hey @sabiwara :)

Thank you!

Hmmm strange that it did not work, it was a nice solution.

Tried on 1.17.3 with a demo app and it seems to be ok. I'll try to keep you posted when the elixir version of the app is upgraded.

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.

3 participants