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
6 changes: 2 additions & 4 deletions lib/elixir/lib/kernel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4703,10 +4703,8 @@ defmodule Kernel do
false

[] ->
quote do
_ = unquote(left)
false
end
# inlined as false in erlang pass
quote(do: :lists.member(unquote(left), []))

[head | tail] = list ->
case in_body? do
Expand Down
6 changes: 5 additions & 1 deletion lib/elixir/lib/module/types/apply.ex
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,11 @@ defmodule Module.Types.Apply do
{Kernel, :put_elem, [{[open_tuple([]), integer(), term()], dynamic(open_tuple([]))}]},

## Lists
{:lists, :member, [{[term(), list(term())], boolean()}]},
{:lists, :member,
[
{[term(), empty_list()], atom([false])},
{[term(), non_empty_list(term())], boolean()}
]},

## Map
{Map, :delete, [{[open_map(), term()], open_map()}]},
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir/lib/module/types/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ defmodule Module.Types.Helpers do
end
end

{{:., _, [:lists, :member]}, meta, [expr, [_ | _] = args]} = call ->
{{:., _, [:lists, :member]}, meta, [expr, args]} = call when is_list(args) ->
if Enum.any?(args, &match?({:|, _, [_, _]}, &1)) do
call
else
Expand Down
4 changes: 4 additions & 0 deletions lib/elixir/src/elixir_erl_pass.erl
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,10 @@ translate_remote('Elixir.String.Chars', to_string, Meta, [Arg], S) ->
{clause, Generated, [Var], [[Guard]], [Fast]},
{clause, Generated, [Var], [], [Slow]}
]}, VS};
translate_remote(lists, member, Meta, [Expr, []], S) ->
Ann = ?ann(Meta),
{TExpr, S1} = translate(Expr, Ann, S),
{{block, Ann, [{match, Ann, {var, Ann, '_'}, TExpr}, {atom, Ann, false}]}, S1};
translate_remote(lists, member, Meta, [Expr, [Head | Tail] = List], S) ->
Ann = ?ann(Meta),

Expand Down
7 changes: 6 additions & 1 deletion lib/elixir/test/elixir/kernel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,11 @@ defmodule KernelTest do
refute 2 in []
refute false in []
refute true in []

# make sure optimization still evaluates the left-hand side
# (do not use assert/refute which handle in/2 differently)
send(self(), :foo) in []
assert_received :foo
end

test "with expressions on right side" do
Expand Down Expand Up @@ -697,7 +702,7 @@ defmodule KernelTest do
"""

# Empty list
assert expand_to_string(quote(do: :x in [])) =~ "_ = :x\nfalse"
assert expand_to_string(quote(do: :x in [])) =~ ":lists.member(:x, [])"
assert expand_to_string(quote(do: :x in []), :guard) == "false"

# Lists
Expand Down
2 changes: 1 addition & 1 deletion lib/elixir/test/elixir/macro/env_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ defmodule Macro.EnvTest do
test "to_match/1" do
quote = quote(do: x in [])

assert {:__block__, [], [{:=, [], [{:_, [], Kernel}, {:x, [], Macro.EnvTest}]}, false]} =
assert {{:., [], [:lists, :member]}, [], [{:x, [], Macro.EnvTest}, []]} =
Macro.expand_once(quote, __ENV__)

assert Macro.expand_once(quote, Macro.Env.to_match(__ENV__)) == false
Expand Down
5 changes: 5 additions & 0 deletions lib/elixir/test/elixir/module/types/expr_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,11 @@ defmodule Module.Types.ExprTest do
end

test "Kernel.in/2" do
assert typecheck!(
[x],
x in []
) == atom([false])

assert typecheck!(
[x],
(
Expand Down
2 changes: 2 additions & 0 deletions lib/elixir/test/elixir/module/types/helpers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ defmodule Module.Types.HelpersTest do
assert expr_to_string(quote(do: :erlang.list_to_atom(a))) == "List.to_atom(a)"
assert expr_to_string(quote(do: :erlang.element(1, a))) == "elem(a, 0)"
assert expr_to_string(quote(do: :erlang.element(:erlang.+(a, 1), b))) == "elem(b, a)"
assert expr_to_string(quote(do: :lists.member(a, []))) == "a in []"
assert expr_to_string(quote(do: :lists.member(a, [:foo, :bar]))) == "a in [:foo, :bar]"
end

test "Kernel macros" do
Expand Down
17 changes: 17 additions & 0 deletions lib/elixir/test/erlang/control_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ optimized_or_test() ->
{clause, _, [{atom, _, true}], [], [{atom, _, true}]}]
} = to_erl("is_list([]) or :done").

optimized_in_test() ->
{'block', _,
[{match,_,
{var, _, '_'},
{call, _, {remote, _, {atom, _, 'Elixir.IO'}, {atom, _, puts}}, [{atom, _, hi}]}},
{atom, _, false}]
} = to_erl("IO.puts(:hi) in []"),
{'block', _,
[{match,_,
{var, _, '_1'},
{call, _, {remote, _, {atom, _, 'Elixir.IO'}, {atom, _, puts}}, [{atom, _, hi}]}},
{op, _, 'orelse',
{op, _, '=:=', {var, _ , '_1'}, {integer, _, 1}},
{op, _, '=:=', {var, _ , '_1'}, {integer, _, 2}}
}]
} = to_erl("IO.puts(:hi) in [1, 2]").

no_after_in_try_test() ->
{'try', _, [_], [], [_], []} = to_erl("try do :foo.bar() catch _ -> :ok end").

Expand Down
Loading