Skip to content

Commit 69a95a8

Browse files
committed
Merge empty_list and non_empty_list together on pretty printing
1 parent 4dd0d5d commit 69a95a8

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

lib/elixir/lib/module/types/descr.ex

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,22 @@ defmodule Module.Types.Descr do
356356
if term_type?(descr) do
357357
{:term, [], []}
358358
else
359-
case Enum.flat_map(descr, fn {key, value} -> to_quoted(key, value) end) do
359+
{extra, descr} =
360+
case descr do
361+
# Merge empty list and list together if they both exist
362+
%{list: list, bitmap: bitmap} when (bitmap &&& @bit_empty_list) != 0 ->
363+
descr = descr |> Map.delete(:list) |> Map.update!(:bitmap, &(&1 - @bit_empty_list))
364+
365+
case list_to_quoted(list, :list) do
366+
[] -> {[{:empty_list, [], []}], descr}
367+
unions -> {unions, descr}
368+
end
369+
370+
%{} ->
371+
{[], descr}
372+
end
373+
374+
case extra ++ Enum.flat_map(descr, fn {key, value} -> to_quoted(key, value) end) do
360375
[] -> {:none, [], []}
361376
unions -> unions |> Enum.sort() |> Enum.reduce(&{:or, [], [&2, &1]})
362377
end
@@ -368,7 +383,7 @@ defmodule Module.Types.Descr do
368383
defp to_quoted(:bitmap, val), do: bitmap_to_quoted(val)
369384
defp to_quoted(:dynamic, descr), do: dynamic_to_quoted(descr)
370385
defp to_quoted(:map, dnf), do: map_to_quoted(dnf)
371-
defp to_quoted(:list, dnf), do: list_to_quoted(dnf)
386+
defp to_quoted(:list, dnf), do: list_to_quoted(dnf, :non_empty_list)
372387
defp to_quoted(:tuple, dnf), do: tuple_to_quoted(dnf)
373388

374389
@doc """
@@ -963,7 +978,7 @@ defmodule Module.Types.Descr do
963978
end
964979
end
965980

966-
defp list_to_quoted(dnf) do
981+
defp list_to_quoted(dnf, name) do
967982
dnf = list_normalize(dnf)
968983

969984
for {list_type, last_type, negs} <- dnf, reduce: [] do
@@ -976,7 +991,7 @@ defmodule Module.Types.Descr do
976991
end
977992

978993
if negs == [] do
979-
[{:non_empty_list, [], arguments} | acc]
994+
[{name, [], arguments} | acc]
980995
else
981996
negs
982997
|> Enum.map(fn {ty, lst} ->
@@ -987,14 +1002,14 @@ defmodule Module.Types.Descr do
9871002
[to_quoted(ty), to_quoted(lst)]
9881003
end
9891004

990-
{:non_empty_list, [], args}
1005+
{name, [], args}
9911006
end)
9921007
|> Enum.reduce(&{:or, [], [&2, &1]})
9931008
|> Kernel.then(
9941009
&[
9951010
{:and, [],
9961011
[
997-
{:non_empty_list, [], arguments},
1012+
{name, [], arguments},
9981013
{:not, [], [&1]}
9991014
]}
10001015
| acc

lib/elixir/test/elixir/module/types/descr_test.exs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,9 +1043,7 @@ defmodule Module.Types.DescrTest do
10431043
test "negation" do
10441044
assert negation(negation(integer())) |> to_quoted_string() == "integer()"
10451045
assert negation(negation(atom([:foo, :bar]))) |> to_quoted_string() == ":bar or :foo"
1046-
1047-
assert negation(negation(list(term()))) |> to_quoted_string() ==
1048-
"empty_list() or non_empty_list(term())"
1046+
assert negation(negation(list(term()))) |> to_quoted_string() == "list(term())"
10491047
end
10501048

10511049
test "atom" do
@@ -1082,8 +1080,8 @@ defmodule Module.Types.DescrTest do
10821080
end
10831081

10841082
test "lists" do
1085-
assert list(term()) |> to_quoted_string() == "empty_list() or non_empty_list(term())"
1086-
assert list(integer()) |> to_quoted_string() == "empty_list() or non_empty_list(integer())"
1083+
assert list(term()) |> to_quoted_string() == "list(term())"
1084+
assert list(integer()) |> to_quoted_string() == "list(integer())"
10871085

10881086
assert list(term()) |> difference(empty_list()) |> to_quoted_string() ==
10891087
"non_empty_list(term())"
@@ -1098,13 +1096,13 @@ defmodule Module.Types.DescrTest do
10981096
"non_empty_list(term()) and not (non_empty_list(atom()) or non_empty_list(integer()))"
10991097

11001098
assert list(term(), integer()) |> to_quoted_string() ==
1101-
"empty_list() or non_empty_list(term(), integer())"
1099+
"list(term(), integer())"
11021100

11031101
assert difference(list(term(), atom()), list(term(), boolean())) |> to_quoted_string() ==
11041102
"non_empty_list(term(), atom() and not boolean())"
11051103

11061104
assert list(term(), term()) |> to_quoted_string() ==
1107-
"empty_list() or non_empty_list(term(), term())"
1105+
"list(term(), term())"
11081106
end
11091107

11101108
test "tuples" do

lib/elixir/test/elixir/module/types/expr_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ defmodule Module.Types.ExprTest do
868868
869869
where "args_or_arity" was given the type:
870870
871-
# type: empty_list() or integer() or non_empty_list(term())
871+
# type: integer() or list(term())
872872
# from: types_test.ex:LINE-3
873873
[{_, _, args_or_arity, _} | _] = __STACKTRACE__
874874
"""

lib/elixir/test/elixir/module/types/pattern_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ defmodule Module.Types.PatternTest do
3434
3535
given types:
3636
37-
empty_list() or non_empty_list(integer())
37+
list(integer())
3838
3939
but expected one of:
4040

0 commit comments

Comments
 (0)