Skip to content
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
3 changes: 3 additions & 0 deletions lib/elixir/src/elixir_expand.erl
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,9 @@ attach_context_module(Receiver, Meta, #{context_modules := ContextModules}) ->
false -> Meta
end.

% Signed numbers can be rewritten no matter the context
rewrite(_, erlang, _, '+', _, [Arg], _S) when is_number(Arg) -> {ok, Arg};
rewrite(_, erlang, _, '-', _, [Arg], _S) when is_number(Arg) -> {ok, -Arg};
rewrite(match, Receiver, DotMeta, Right, Meta, EArgs, _S) ->
elixir_rewrite:match_rewrite(Receiver, DotMeta, Right, Meta, EArgs);
rewrite(guard, Receiver, DotMeta, Right, Meta, EArgs, S) ->
Expand Down
2 changes: 0 additions & 2 deletions lib/elixir/src/elixir_rewrite.erl
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,6 @@ increment(Meta, Other) ->
%% The allowed operations are very limited.
%% The Kernel operators are already inlined by now, we only need to
%% 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!

try {ok, static_append(Left, Right, Meta)}
catch impossible -> {error, {invalid_match_append, Left}}
Expand Down
4 changes: 2 additions & 2 deletions lib/elixir/test/elixir/kernel/expansion_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -725,10 +725,10 @@ defmodule Kernel.ExpansionTest do
expand(quote(do: [1] ++ 2 ++ [3] = [1, 2, 3]))
end)

assert {:=, _, [-1, {{:., _, [:erlang, :-]}, _, [1]}]} =
assert {:=, _, [-1, -1]} =
expand(quote(do: -1 = -1))

assert {:=, _, [1, {{:., _, [:erlang, :+]}, _, [1]}]} =
assert {:=, _, [1, 1]} =
expand(quote(do: +1 = +1))

assert {:=, _, [[{:|, _, [1, [{:|, _, [2, 3]}]]}], [1, 2, 3]]} =
Expand Down