Skip to content

Commit b57be87

Browse files
committed
Remove domain_key wrapping
1 parent 02bf285 commit b57be87

6 files changed

Lines changed: 63 additions & 54 deletions

File tree

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

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,20 @@ defmodule Module.Types.Descr do
2727
@bit_number @bit_integer ||| @bit_float
2828

2929
defmacro bdd_leaf(arg1, arg2), do: {arg1, arg2}
30-
defmacro domain_key(key), do: {:domain_key, key}
3130

3231
@domain_key_types [
33-
{:domain_key, :binary},
34-
{:domain_key, :empty_list},
35-
{:domain_key, :integer},
36-
{:domain_key, :float},
37-
{:domain_key, :pid},
38-
{:domain_key, :port},
39-
{:domain_key, :reference},
40-
{:domain_key, :fun},
41-
{:domain_key, :atom},
42-
{:domain_key, :tuple},
43-
{:domain_key, :map},
44-
{:domain_key, :list}
32+
:binary,
33+
:empty_list,
34+
:integer,
35+
:float,
36+
:pid,
37+
:port,
38+
:reference,
39+
:fun,
40+
:atom,
41+
:tuple,
42+
:map,
43+
:list
4544
]
4645

4746
# Remark: those are explicit BDD constructors. The functional constructors are `bdd_new/1` and `bdd_new/3`.
@@ -2294,35 +2293,35 @@ defmodule Module.Types.Descr do
22942293
cond do
22952294
type_kind == :atom and match?({:union, _}, type) -> acc
22962295
type_kind == :bitmap -> bitmap_to_domain_keys(type, acc)
2297-
not empty?(%{type_kind => type}) -> [domain_key(type_kind) | acc]
2296+
not empty?(%{type_kind => type}) -> [type_kind | acc]
22982297
true -> acc
22992298
end
23002299
end
23012300
end
23022301

23032302
defp bitmap_to_domain_keys(bitmap, acc) do
2304-
acc = if (bitmap &&& @bit_binary) != 0, do: [domain_key(:binary) | acc], else: acc
2305-
acc = if (bitmap &&& @bit_empty_list) != 0, do: [domain_key(:empty_list) | acc], else: acc
2306-
acc = if (bitmap &&& @bit_integer) != 0, do: [domain_key(:integer) | acc], else: acc
2307-
acc = if (bitmap &&& @bit_float) != 0, do: [domain_key(:float) | acc], else: acc
2308-
acc = if (bitmap &&& @bit_pid) != 0, do: [domain_key(:pid) | acc], else: acc
2309-
acc = if (bitmap &&& @bit_port) != 0, do: [domain_key(:port) | acc], else: acc
2310-
acc = if (bitmap &&& @bit_reference) != 0, do: [domain_key(:reference) | acc], else: acc
2303+
acc = if (bitmap &&& @bit_binary) != 0, do: [:binary | acc], else: acc
2304+
acc = if (bitmap &&& @bit_empty_list) != 0, do: [:empty_list | acc], else: acc
2305+
acc = if (bitmap &&& @bit_integer) != 0, do: [:integer | acc], else: acc
2306+
acc = if (bitmap &&& @bit_float) != 0, do: [:float | acc], else: acc
2307+
acc = if (bitmap &&& @bit_pid) != 0, do: [:pid | acc], else: acc
2308+
acc = if (bitmap &&& @bit_port) != 0, do: [:port | acc], else: acc
2309+
acc = if (bitmap &&& @bit_reference) != 0, do: [:reference | acc], else: acc
23112310
acc
23122311
end
23132312

2314-
defp domain_key_to_descr(domain_key(:atom)), do: atom()
2315-
defp domain_key_to_descr(domain_key(:binary)), do: binary()
2316-
defp domain_key_to_descr(domain_key(:empty_list)), do: empty_list()
2317-
defp domain_key_to_descr(domain_key(:integer)), do: integer()
2318-
defp domain_key_to_descr(domain_key(:float)), do: float()
2319-
defp domain_key_to_descr(domain_key(:pid)), do: pid()
2320-
defp domain_key_to_descr(domain_key(:port)), do: port()
2321-
defp domain_key_to_descr(domain_key(:reference)), do: reference()
2322-
defp domain_key_to_descr(domain_key(:fun)), do: fun()
2323-
defp domain_key_to_descr(domain_key(:tuple)), do: tuple()
2324-
defp domain_key_to_descr(domain_key(:map)), do: open_map()
2325-
defp domain_key_to_descr(domain_key(:list)), do: non_empty_list(term(), term())
2313+
defp domain_key_to_descr(:atom), do: atom()
2314+
defp domain_key_to_descr(:binary), do: binary()
2315+
defp domain_key_to_descr(:empty_list), do: empty_list()
2316+
defp domain_key_to_descr(:integer), do: integer()
2317+
defp domain_key_to_descr(:float), do: float()
2318+
defp domain_key_to_descr(:pid), do: pid()
2319+
defp domain_key_to_descr(:port), do: port()
2320+
defp domain_key_to_descr(:reference), do: reference()
2321+
defp domain_key_to_descr(:fun), do: fun()
2322+
defp domain_key_to_descr(:tuple), do: tuple()
2323+
defp domain_key_to_descr(:map), do: open_map()
2324+
defp domain_key_to_descr(:list), do: non_empty_list(term(), term())
23262325

23272326
defp map_descr(tag, pairs, default, force_domains?) do
23282327
{fields, domains, dynamic?} = map_descr_pairs(pairs, [], %{}, false)
@@ -2347,17 +2346,12 @@ defmodule Module.Types.Descr do
23472346
end
23482347
end
23492348

2350-
# TODO: Unwrap domain keys when storing them, use a list wrapping instead
23512349
defp map_put_domain(domain, domain_keys, value) when is_list(domain_keys) do
2352-
Enum.reduce(domain_keys, domain, fn key, acc ->
2350+
Enum.reduce(domain_keys, domain, fn key, acc when is_atom(key) ->
23532351
Map.update(acc, key, if_set(value), &union(&1, value))
23542352
end)
23552353
end
23562354

2357-
defp map_put_domain(domain, domain_key(_) = domain_key, value) do
2358-
Map.update(domain, domain_key, if_set(value), &union(&1, value))
2359-
end
2360-
23612355
defp map_descr_pairs([{key, :term} | rest], fields, domain, dynamic?) do
23622356
case is_atom(key) do
23632357
true -> map_descr_pairs(rest, [{key, :term} | fields], domain, dynamic?)
@@ -2388,7 +2382,7 @@ defmodule Module.Types.Descr do
23882382
# Gets the default type associated to atom keys in a map.
23892383
defp map_key_tag_to_type(:open), do: term_or_optional()
23902384
defp map_key_tag_to_type(:closed), do: not_set()
2391-
defp map_key_tag_to_type(domains = %{}), do: Map.get(domains, domain_key(:atom), not_set())
2385+
defp map_key_tag_to_type(domains = %{}), do: Map.get(domains, :atom, not_set())
23922386

23932387
defguardp is_optional_static(map)
23942388
when is_map(map) and is_map_key(map, :optional)
@@ -2619,7 +2613,7 @@ defmodule Module.Types.Descr do
26192613

26202614
defp map_domain_intersection(domains1 = %{}, domains2 = %{}) do
26212615
new_domains =
2622-
for {domain_key(_) = domain_key, type1} <- domains1, reduce: %{} do
2616+
for {domain_key, type1} <- domains1, reduce: %{} do
26232617
acc_domains ->
26242618
case domains2 do
26252619
%{^domain_key => type2} ->
@@ -3077,7 +3071,7 @@ defmodule Module.Types.Descr do
30773071
not empty_or_optional?(value) ->
30783072
{true, [domain_key | valid], invalid, union(acc, value)}
30793073

3080-
domain_key == domain_key(:atom) and any_atom_key.() ->
3074+
domain_key == :atom and any_atom_key.() ->
30813075
{true, valid, [domain_key | invalid], acc}
30823076

30833077
true ->
@@ -3250,7 +3244,7 @@ defmodule Module.Types.Descr do
32503244
end
32513245

32523246
# Take a map bdd and return the union of types for the given key domain.
3253-
defp map_get_domain(dnf, domain_key(_) = domain_key, acc) do
3247+
defp map_get_domain(dnf, domain_key, acc) when is_atom(domain_key) do
32543248
Enum.reduce(dnf, acc, fn
32553249
{:open, _fields, []}, acc ->
32563250
union(term_or_optional(), acc)
@@ -3446,17 +3440,17 @@ defmodule Module.Types.Descr do
34463440
# An open map is a subtype iff the negative domains are all present as term_or_optional()
34473441
defp map_check_domain_keys(:open, neg_domains) do
34483442
map_size(neg_domains) == length(@domain_key_types) and
3449-
Enum.all?(neg_domains, fn {domain_key(_), type} -> subtype?(term_or_optional(), type) end)
3443+
Enum.all?(neg_domains, fn {_domain_key, type} -> subtype?(term_or_optional(), type) end)
34503444
end
34513445

34523446
# A positive domains is smaller than a closed map iff all its keys are empty or optional
34533447
defp map_check_domain_keys(pos_domains, :closed) do
3454-
Enum.all?(pos_domains, fn {domain_key(_), type} -> empty_or_optional?(type) end)
3448+
Enum.all?(pos_domains, fn {_domain_key, type} -> empty_or_optional?(type) end)
34553449
end
34563450

34573451
# Component-wise comparison of domains
34583452
defp map_check_domain_keys(pos_domains, neg_domains) do
3459-
Enum.all?(pos_domains, fn {domain_key(_) = domain_key, type} ->
3453+
Enum.all?(pos_domains, fn {domain_key, type} ->
34603454
subtype?(type, Map.get(neg_domains, domain_key, not_set()))
34613455
end)
34623456
end
@@ -3635,8 +3629,8 @@ defmodule Module.Types.Descr do
36353629

36363630
def map_literal_to_quoted({domains = %{}, fields}, opts) do
36373631
domain_fields =
3638-
for {domain_key(domain_type), value_type} <- domains do
3639-
{{domain_type, [], []}, map_value_to_quoted(value_type, opts)}
3632+
for {domain_key, value_type} <- domains do
3633+
{{domain_key, [], []}, map_value_to_quoted(value_type, opts)}
36403634
end
36413635

36423636
regular_fields_quoted = map_fields_to_quoted(:closed, Enum.sort(fields), opts)

lib/elixir/src/elixir_erl.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
-define(typespecs, 'Elixir.Kernel.Typespec').
1212

1313
checker_version() ->
14-
elixir_checker_v3.
14+
elixir_checker_v4.
1515

1616
%% debug_info callback
1717

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ defmodule Module.Types.DescrTest do
1515
use ExUnit.Case, async: true
1616

1717
import Module.Types.Descr, except: [fun: 1]
18+
defmacro domain_key(arg) when is_atom(arg), do: [arg]
1819

1920
defp number(), do: union(integer(), float())
2021
defp empty_tuple(), do: tuple([])

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Module.Types.ExprTest do
99

1010
import TypeHelper
1111
import Module.Types.Descr
12+
defmacro domain_key(arg) when is_atom(arg), do: [arg]
1213

1314
defmacro generated(x) do
1415
quote generated: true do
@@ -885,11 +886,24 @@ defmodule Module.Types.ExprTest do
885886
end
886887

887888
test "creating open maps" do
888-
assert typecheck!(%{123 => 456}) == dynamic(open_map())
889+
assert typecheck!(%{123 => 456}) == closed_map([{domain_key(:integer), integer()}])
890+
889891
# Since key cannot override :foo, we preserve it
890-
assert typecheck!([key], %{key => 456, foo: :bar}) == dynamic(open_map(foo: atom([:bar])))
892+
assert typecheck!([key], %{key => 456, foo: :bar}) ==
893+
dynamic(
894+
closed_map([
895+
{to_domain_keys(:term), integer()},
896+
{:foo, atom([:bar])}
897+
])
898+
)
899+
891900
# Since key can override :foo, we do not preserve it
892-
assert typecheck!([key], %{:foo => :bar, key => :baz}) == dynamic(open_map())
901+
assert typecheck!([key], %{:foo => :bar, key => :baz}) ==
902+
dynamic(
903+
closed_map([
904+
{to_domain_keys(:term), atom([:baz])}
905+
])
906+
)
893907
end
894908

895909
test "creating structs" do

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,7 @@ defmodule Module.Types.IntegrationTest do
15661566

15671567
defp read_chunk(binary) do
15681568
assert {:ok, {_module, [{~c"ExCk", chunk}]}} = :beam_lib.chunks(binary, [~c"ExCk"])
1569-
assert {:elixir_checker_v3, map} = :erlang.binary_to_term(chunk)
1569+
assert {:elixir_checker_v4, map} = :erlang.binary_to_term(chunk)
15701570
map
15711571
end
15721572

lib/elixir/test/elixir/protocol/consolidation_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ defmodule Protocol.ConsolidationTest do
165165

166166
defp exports(binary) do
167167
{:ok, {_, [{~c"ExCk", check_bin}]}} = :beam_lib.chunks(binary, [~c"ExCk"])
168-
assert {:elixir_checker_v3, contents} = :erlang.binary_to_term(check_bin)
168+
assert {:elixir_checker_v4, contents} = :erlang.binary_to_term(check_bin)
169169
Map.new(contents.exports)
170170
end
171171

0 commit comments

Comments
 (0)