Skip to content

Conversation

@sabiwara
Copy link
Contributor

@sabiwara sabiwara commented Sep 23, 2023

Simplify emitted erlang AST for signed numbers following discussions in #12949.

Before, -1 would be:

  • {:integer, _, -1} in matches
  • {:op, _, :-, {:integer, _, 1}} otherwise

With this PR:

  • {:integer, _, -1}, always

%% care about Erlang ones.
match_rewrite(erlang, _, '+', _, [Arg]) when is_number(Arg) -> {ok, Arg};
match_rewrite(erlang, _, '-', _, [Arg]) when is_number(Arg) -> {ok, -Arg};
match_rewrite(erlang, _, '++', Meta, [Left, Right]) ->
Copy link
Member

Choose a reason for hiding this comment

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

Can you please do a check later that we still need this one? I think recent Erlang versions started inlining ++ as well. :) It may be we can get rid of match_rewrite altogether!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, will definitely check!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I tried for a while but I don't think we can reasonably remove it without introducing regressions.

Issue 1: Elixir's current inlining is more powerful

iex(5)> [a] ++ bc = 'abc'
~c"abc"
1> [A] ++ BC = "abc".
* 1:5: illegal pattern

2> "a" ++ BC = "abc".
"abc"

Issue 2: Rewrite currently serves as validation, will need to translate more erlang errors

Currently:

iex(1)> "a" ++ "b" = "ab"
error: invalid argument for ++ operator inside a match, expected a literal proper list, got: "a"

Relying on erlang's inlining:

iex(1)> "a" ++ "b" = "ab"
** (ErlangError) Erlang error: {:illegal_pattern, {:op, 1, :++, {:bin, 1, [{:bin_element, 1, {:string, 1, ~c"a"}, :default, :default}]}, {:bin, 1, [{:bin_element, 1, {:string, 1, ~c"b"}, :default, :default}]}}}

Copy link
Member

Choose a reason for hiding this comment

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

Thank you for checking!

@sabiwara sabiwara merged commit 55fbc52 into elixir-lang:main Sep 23, 2023
@sabiwara sabiwara deleted the rewrite-numbers branch September 23, 2023 09:09
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.

2 participants