Skip to content

Commit 1854397

Browse files
committed
WIP
1 parent ef14f2b commit 1854397

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

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

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -368,34 +368,44 @@ defmodule Module.Types.Descr do
368368
@doc """
369369
Optimized version of `not empty?(intersection(fun(), type))`.
370370
"""
371+
def fun_type?(:term), do: true
372+
def fun_type?(%{dynamic: :term}), do: true
371373
def fun_type?(%{dynamic: %{bitmap: bitmap}}) when (bitmap &&& @bit_fun) != 0, do: true
372374
def fun_type?(%{bitmap: bitmap}) when (bitmap &&& @bit_fun) != 0, do: true
373375
def fun_type?(_), do: false
374376

375377
@doc """
376378
Optimized version of `not empty?(intersection(binary(), type))`.
377379
"""
380+
def binary_type?(:term), do: true
381+
def binary_type?(%{dynamic: :term}), do: true
378382
def binary_type?(%{dynamic: %{bitmap: bitmap}}) when (bitmap &&& @bit_binary) != 0, do: true
379383
def binary_type?(%{bitmap: bitmap}) when (bitmap &&& @bit_binary) != 0, do: true
380384
def binary_type?(_), do: false
381385

382386
@doc """
383387
Optimized version of `not empty?(intersection(integer(), type))`.
384388
"""
389+
def integer_type?(:term), do: true
390+
def integer_type?(%{dynamic: :term}), do: true
385391
def integer_type?(%{dynamic: %{bitmap: bitmap}}) when (bitmap &&& @bit_integer) != 0, do: true
386392
def integer_type?(%{bitmap: bitmap}) when (bitmap &&& @bit_integer) != 0, do: true
387393
def integer_type?(_), do: false
388394

389395
@doc """
390396
Optimized version of `not empty?(intersection(float(), type))`.
391397
"""
398+
def float_type?(:term), do: true
399+
def float_type?(%{dynamic: :term}), do: true
392400
def float_type?(%{dynamic: %{bitmap: bitmap}}) when (bitmap &&& @bit_float) != 0, do: true
393401
def float_type?(%{bitmap: bitmap}) when (bitmap &&& @bit_float) != 0, do: true
394402
def float_type?(_), do: false
395403

396404
@doc """
397405
Optimized version of `not empty?(intersection(integer() or float(), type))`.
398406
"""
407+
def number_type?(:term), do: true
408+
def number_type?(%{dynamic: :term}), do: true
399409
def number_type?(%{dynamic: %{bitmap: bitmap}}) when (bitmap &&& @bit_number) != 0, do: true
400410
def number_type?(%{bitmap: bitmap}) when (bitmap &&& @bit_number) != 0, do: true
401411
def number_type?(_), do: false
@@ -442,28 +452,18 @@ defmodule Module.Types.Descr do
442452
# an empty list of atoms. It is simplified to `0` in set operations, and the key
443453
# is removed from the map.
444454

445-
@doc """
446-
Optimized version of `not empty?(intersection(atom(), type))`.
447-
"""
448-
def atom_type?(%{dynamic: %{atom: _}}), do: true
449-
def atom_type?(%{atom: _}), do: true
450-
def atom_type?(_), do: false
451-
452455
@doc """
453456
Optimized version of `not empty?(intersection(atom([atom]), type))`.
454457
"""
455-
def atom_type?(:term, _atom), do: false
458+
def atom_type?(:term, _atom), do: true
456459

457460
def atom_type?(%{} = descr, atom) do
458-
{static_or_dynamic, static} = Map.pop(descr, :dynamic, descr)
459-
460-
atom_only?(static) and
461-
case static_or_dynamic do
462-
:term -> true
463-
%{atom: {:union, set}} -> :sets.is_element(atom, set)
464-
%{atom: {:negation, set}} -> not :sets.is_element(atom, set)
465-
%{} -> false
466-
end
461+
case Map.get(descr, :dynamic, descr) do
462+
:term -> true
463+
%{atom: {:union, set}} -> :sets.is_element(atom, set)
464+
%{atom: {:negation, set}} -> not :sets.is_element(atom, set)
465+
%{} -> false
466+
end
467467
end
468468

469469
@doc """
@@ -704,7 +704,7 @@ defmodule Module.Types.Descr do
704704
case :maps.take(:dynamic, descr) do
705705
:error ->
706706
if is_map_key(descr, :map) and map_only?(descr) do
707-
{static_optional?, static_type} = map_fetch_static(descr, key) |> pop_optional_static()
707+
{static_optional?, static_type} = map_fetch_static(descr, key)
708708

709709
if static_optional? or empty?(static_type) do
710710
:badkey
@@ -720,10 +720,8 @@ defmodule Module.Types.Descr do
720720

721721
{dynamic, static} ->
722722
if is_map_key(dynamic, :map) and map_only?(static) do
723-
{dynamic_optional?, dynamic_type} =
724-
map_fetch_static(dynamic, key) |> pop_optional_static()
725-
726-
{static_optional?, static_type} = map_fetch_static(static, key) |> pop_optional_static()
723+
{dynamic_optional?, dynamic_type} = map_fetch_static(dynamic, key)
724+
{static_optional?, static_type} = map_fetch_static(static, key)
727725

728726
if static_optional? or empty?(dynamic_type) do
729727
:badkey
@@ -740,8 +738,16 @@ defmodule Module.Types.Descr do
740738

741739
defp map_fetch_static(descr, key) when is_atom(key) do
742740
case descr do
743-
%{map: map} -> Enum.reduce(map_split_on_key(map, key), none(), &union/2)
744-
%{} -> none()
741+
%{map: [{:open, fields, []}]} when fields == %{} ->
742+
{true, term()}
743+
744+
%{map: map} ->
745+
map_split_on_key(map, key)
746+
|> Enum.reduce(none(), &union/2)
747+
|> pop_optional_static()
748+
749+
%{} ->
750+
{false, none()}
745751
end
746752
end
747753

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ defmodule Module.Types.DescrTest do
4646
reference()
4747
]
4848

49-
assert Enum.reduce(all, &union/2) == term()
49+
assert Enum.reduce(all, &union/2) |> equal?(term())
5050
end
5151

5252
test "dynamic" do
@@ -258,15 +258,15 @@ defmodule Module.Types.DescrTest do
258258

259259
describe "queries" do
260260
test "atom_type?" do
261-
refute atom_type?(term(), :foo)
261+
assert atom_type?(term(), :foo)
262262
assert atom_type?(dynamic(), :foo)
263263

264264
assert atom_type?(atom([:foo, :bar]), :foo)
265265
refute atom_type?(atom([:foo, :bar]), :baz)
266266
assert atom_type?(negation(atom([:foo, :bar])), :baz)
267267

268268
refute atom_type?(union(atom([:foo, :bar]), integer()), :baz)
269-
assert atom_type?(dynamic(union(atom([:foo, :bar]), integer())), :baz)
269+
refute atom_type?(dynamic(union(atom([:foo, :bar]), integer())), :baz)
270270
end
271271
end
272272

0 commit comments

Comments
 (0)