Skip to content

Commit b39205b

Browse files
committed
Optimize sized tuple check
1 parent 50547c1 commit b39205b

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,7 +1885,7 @@ defmodule Module.Types.Descr do
18851885
:error ->
18861886
# Note: the empty type is not a valid input
18871887
is_proper_tuple? = descr_key?(descr, :tuple) and tuple_only?(descr)
1888-
is_proper_size? = tuple_of_size_at_least?(descr, index + 1)
1888+
is_proper_size? = tuple_of_size_at_least_static?(descr, index + 1)
18891889

18901890
cond do
18911891
is_proper_tuple? and is_proper_size? -> tuple_delete_static(descr, index)
@@ -1895,7 +1895,7 @@ defmodule Module.Types.Descr do
18951895

18961896
{dynamic, static} ->
18971897
is_proper_tuple? = descr_key?(dynamic, :tuple) and tuple_only?(static)
1898-
is_proper_size? = tuple_of_size_at_least?(static, index + 1)
1898+
is_proper_size? = tuple_of_size_at_least_static?(static, index + 1)
18991899

19001900
cond do
19011901
is_proper_tuple? and is_proper_size? ->
@@ -1941,7 +1941,7 @@ defmodule Module.Types.Descr do
19411941
:error ->
19421942
# Note: the empty type is not a valid input
19431943
is_proper_tuple? = descr_key?(descr, :tuple) and tuple_only?(descr)
1944-
is_proper_size? = index == 0 or tuple_of_size_at_least?(descr, index)
1944+
is_proper_size? = index == 0 or tuple_of_size_at_least_static?(descr, index)
19451945

19461946
cond do
19471947
is_proper_tuple? and is_proper_size? -> tuple_insert_static(descr, index, type)
@@ -1951,7 +1951,7 @@ defmodule Module.Types.Descr do
19511951

19521952
{dynamic, static} ->
19531953
is_proper_tuple? = descr_key?(dynamic, :tuple) and tuple_only?(static)
1954-
is_proper_size? = index == 0 or tuple_of_size_at_least?(static, index)
1954+
is_proper_size? = index == 0 or tuple_of_size_at_least_static?(static, index)
19551955

19561956
cond do
19571957
is_proper_tuple? and is_proper_size? ->
@@ -2022,8 +2022,17 @@ defmodule Module.Types.Descr do
20222022
open_tuple(List.duplicate(term(), n))
20232023
end
20242024

2025-
defp tuple_of_size_at_least?(descr, index) do
2026-
subtype?(Map.take(descr, [:tuple]), tuple_of_size_at_least(index))
2025+
defp tuple_of_size_at_least_static?(descr, index) do
2026+
case descr do
2027+
%{tuple: dnf} ->
2028+
Enum.all?(dnf, fn
2029+
{_, elements, []} -> length(elements) >= index
2030+
entry -> subtype?(%{tuple: [entry]}, tuple_of_size_at_least(index))
2031+
end)
2032+
2033+
%{} ->
2034+
true
2035+
end
20272036
end
20282037

20292038
## Pairs

0 commit comments

Comments
 (0)