Skip to content

Commit c5b4521

Browse files
committed
opt mutators and compatible intersection
1 parent 93f32a8 commit c5b4521

1 file changed

Lines changed: 33 additions & 33 deletions

File tree

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

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,9 @@ defmodule Module.Types.Descr do
407407
defp pop_dynamic(:term), do: {:term, :term}
408408
defp pop_dynamic(descr), do: Map.pop(descr, :dynamic, descr)
409409

410-
@compile {:inline, maybe_union: 2}
411-
defp maybe_union(nil, _fun), do: nil
412-
defp maybe_union(descr, fun), do: bare_union(descr, fun.())
410+
@compile {:inline, maybe_opt_union: 2}
411+
defp maybe_opt_union(nil, _fun), do: nil
412+
defp maybe_opt_union(descr, fun), do: opt_union(descr, fun.())
413413

414414
@doc """
415415
Computes the union of two descrs.
@@ -949,9 +949,9 @@ defmodule Module.Types.Descr do
949949
Returns the intersection between two types
950950
only if they are compatible. Otherwise returns `:error`.
951951
952-
This finds the intersection between the arguments and the
953-
domain of a function. It is used to refine dynamic types
954-
as we traverse the program.
952+
This finds the optimized intersection between the arguments and the
953+
domain of a function. It is used to refine dynamic types as we traverse
954+
the program.
955955
"""
956956
def compatible_intersection(other, :term), do: {:ok, remove_optional(other)}
957957

@@ -966,12 +966,12 @@ defmodule Module.Types.Descr do
966966

967967
cond do
968968
empty?(left_static) ->
969-
dynamic = bare_intersection_static(unfold(left_dynamic), unfold(right_dynamic))
969+
dynamic = opt_intersection_static(unfold(left_dynamic), unfold(right_dynamic))
970970
if empty?(dynamic), do: {:error, left}, else: {:ok, dynamic(dynamic)}
971971

972972
subtype_static?(left_static, right_dynamic) ->
973-
dynamic = bare_intersection_static(unfold(left_dynamic), unfold(right_dynamic))
974-
{:ok, bare_union(dynamic(dynamic), left_static)}
973+
dynamic = opt_intersection_static(unfold(left_dynamic), unfold(right_dynamic))
974+
{:ok, opt_union(dynamic(dynamic), left_static)}
975975

976976
true ->
977977
{:error, left}
@@ -3471,8 +3471,8 @@ defmodule Module.Types.Descr do
34713471
# We can exceptionally check for none() here because
34723472
# we already check for empty downstream
34733473
if dynamic_found? do
3474-
{bare_union(static_value, dynamic(dynamic_value)),
3475-
bare_union(static_descr, dynamic(dynamic_descr)), static_errors ++ dynamic_errors}
3474+
{opt_union(static_value, dynamic(dynamic_value)),
3475+
opt_union(static_descr, dynamic(dynamic_descr)), static_errors ++ dynamic_errors}
34763476
else
34773477
{:error, static_errors ++ dynamic_errors}
34783478
end
@@ -3590,10 +3590,10 @@ defmodule Module.Types.Descr do
35903590
acc_errors = if required_key?, do: [{:badkey, key} | acc_errors], else: acc_errors
35913591
{acc_value, acc_descr, acc_errors, acc_found?}
35923592
else
3593-
acc_value = bare_union(value, acc_value)
3593+
acc_value = opt_union(value, acc_value)
35943594

35953595
acc_descr =
3596-
bare_union(map_put_key_static(descr, key, type_fun.(optional?, value)), acc_descr)
3596+
opt_union(map_put_key_static(descr, key, type_fun.(optional?, value)), acc_descr)
35973597

35983598
# The field will be missing if we are not forcing,
35993599
# we are in static mode and the value is optional.
@@ -3639,7 +3639,7 @@ defmodule Module.Types.Descr do
36393639
# Optimization: if there are no negatives, we can directly remove the key.
36403640
{tag, fields, []}, {value, bdd} ->
36413641
{fst, snd} = map_pop_key_bdd(tag, fields, key)
3642-
{maybe_union(value, fn -> fst end), map_union(bdd, snd)}
3642+
{maybe_opt_union(value, fn -> fst end), opt_map_union(bdd, snd)}
36433643

36443644
{tag, fields, negs}, {value, bdd} ->
36453645
{fst, snd} = map_pop_key_bdd(tag, fields, key)
@@ -3659,17 +3659,17 @@ defmodule Module.Types.Descr do
36593659
do: [],
36603660
else: map_split_negative_key(negs, key, fst, snd)
36613661

3662-
{maybe_union(value, fn ->
3662+
{maybe_opt_union(value, fn ->
36633663
if keep_fst? do
36643664
fst
36653665
else
3666-
Enum.reduce(pairs, none(), &bare_union(elem(&1, 0), &2))
3666+
Enum.reduce(pairs, none(), &opt_union(elem(&1, 0), &2))
36673667
end
36683668
end),
36693669
if keep_snd? do
3670-
map_union(bdd, snd)
3670+
opt_map_union(bdd, snd)
36713671
else
3672-
Enum.reduce(pairs, bdd, &map_union(elem(&1, 1), &2))
3672+
Enum.reduce(pairs, bdd, &opt_map_union(elem(&1, 1), &2))
36733673
end}
36743674
end
36753675
end)
@@ -3689,7 +3689,7 @@ defmodule Module.Types.Descr do
36893689
{seen, acc}
36903690
else
36913691
{_, value} = map_dnf_fetch_static(dnf, key)
3692-
{Map.put(seen, key, []), bare_union(acc, value)}
3692+
{Map.put(seen, key, []), opt_union(acc, value)}
36933693
end
36943694
end)
36953695
end)
@@ -3730,12 +3730,12 @@ defmodule Module.Types.Descr do
37303730
cond do
37313731
# Domain has a direct match: valid, union both atom keys and domain value
37323732
not empty?(value) ->
3733-
acc = if require_type?, do: bare_union(bare_union(atom_acc, acc), value), else: acc
3733+
acc = if require_type?, do: opt_union(opt_union(atom_acc, acc), value), else: acc
37343734
{true, [:atom | valid], invalid, acc}
37353735

37363736
# No direct match, but individual atom keys exist: found but domain is invalid
37373737
not empty?(atom_acc) ->
3738-
acc = if require_type?, do: bare_union(atom_acc, acc), else: acc
3738+
acc = if require_type?, do: opt_union(atom_acc, acc), else: acc
37393739
{true, valid, [:atom | invalid], acc}
37403740

37413741
# No match at all
@@ -3745,7 +3745,7 @@ defmodule Module.Types.Descr do
37453745

37463746
# Non-atom domain key has a match: mark as valid
37473747
not empty?(value) ->
3748-
acc = if require_type?, do: bare_union(acc, value), else: acc
3748+
acc = if require_type?, do: opt_union(acc, value), else: acc
37493749
{true, [domain_key | valid], invalid, acc}
37503750

37513751
# Non-atom domain key not found: mark as invalid
@@ -3810,7 +3810,7 @@ defmodule Module.Types.Descr do
38103810
{:ok, value} ->
38113811
fields_store(
38123812
domain_key,
3813-
bare_union(value, type_fun.(true, remove_optional(value))),
3813+
opt_union(value, type_fun.(true, remove_optional(value))),
38143814
acc
38153815
)
38163816

@@ -3875,7 +3875,7 @@ defmodule Module.Types.Descr do
38753875
if descr_key?(dynamic, :map) and map_only?(static) do
38763876
static_descr = map_put_static(static, split_keys, type)
38773877
dynamic_descr = map_put_static(dynamic, split_keys, type)
3878-
{:ok, bare_union(static_descr, dynamic(dynamic_descr))}
3878+
{:ok, opt_union(static_descr, dynamic(dynamic_descr))}
38793879
else
38803880
:badmap
38813881
end
@@ -3924,7 +3924,7 @@ defmodule Module.Types.Descr do
39243924
defp map_put_keys_static(dnf, keys, type, acc) do
39253925
Enum.reduce(keys, acc, fn key, acc ->
39263926
{nil, descr} = map_dnf_pop_key_static(dnf, key, nil)
3927-
bare_union(map_put_key_static(descr, key, type), acc)
3927+
opt_union(map_put_key_static(descr, key, type), acc)
39283928
end)
39293929
end
39303930

@@ -5172,13 +5172,13 @@ defmodule Module.Types.Descr do
51725172
static_result = tuple_delete_static(static, index)
51735173

51745174
# Prune for dynamic values that make the operation succeed.
5175-
dynamic_input = bare_intersection(dynamic, tuple_of_size_at_least(index + 1))
5175+
dynamic_input = opt_intersection(dynamic, tuple_of_size_at_least(index + 1))
51765176

51775177
if empty?(dynamic_input) and empty?(static) do
51785178
:badindex
51795179
else
51805180
dynamic_result = tuple_delete_static(dynamic_input, index)
5181-
bare_union(dynamic(dynamic_result), static_result)
5181+
opt_union(dynamic(dynamic_result), static_result)
51825182
end
51835183

51845184
# Highlight the case where the issue is an index out of range from the tuple
@@ -5201,7 +5201,7 @@ defmodule Module.Types.Descr do
52015201
|> Enum.reduce(:bdd_bot, fn
52025202
{tag, elements, []}, acc ->
52035203
{_, _, bdd} = tuple_take_element(elements, index, tag)
5204-
tuple_union(bdd, acc)
5204+
opt_tuple_union(bdd, acc)
52055205

52065206
{tag, elements, negs}, acc ->
52075207
{_, value, bdd} = tuple_take_element(elements, index, tag)
@@ -5212,11 +5212,11 @@ defmodule Module.Types.Descr do
52125212

52135213
negative ->
52145214
if tuple_pair_projection_keeps_full_snd?(negative, value) do
5215-
tuple_union(bdd, acc)
5215+
opt_tuple_union(bdd, acc)
52165216
else
52175217
negs
52185218
|> tuple_split_negative(index, value, bdd)
5219-
|> Enum.reduce(acc, fn {_, bdd}, acc -> tuple_union(bdd, acc) end)
5219+
|> Enum.reduce(acc, fn {_, bdd}, acc -> opt_tuple_union(bdd, acc) end)
52205220
end
52215221
end
52225222
end)
@@ -5249,7 +5249,7 @@ defmodule Module.Types.Descr do
52495249
else
52505250
case tuple_insert_at_checked(descr, index, static_type) do
52515251
static_result when is_descr(static_result) ->
5252-
bare_union(dynamic_result, static_result)
5252+
opt_union(dynamic_result, static_result)
52535253

52545254
error ->
52555255
error
@@ -5286,13 +5286,13 @@ defmodule Module.Types.Descr do
52865286
static_result = tuple_insert_static(static, index, type)
52875287

52885288
# Prune for dynamic values that make the intersection succeed
5289-
dynamic_input = bare_intersection(dynamic, tuple_of_size_at_least(index))
5289+
dynamic_input = opt_intersection(dynamic, tuple_of_size_at_least(index))
52905290

52915291
if empty?(dynamic_input) and empty?(static) do
52925292
:badindex
52935293
else
52945294
dynamic_result = tuple_insert_static(dynamic_input, index, type)
5295-
bare_union(dynamic(dynamic_result), static_result)
5295+
opt_union(dynamic(dynamic_result), static_result)
52965296
end
52975297

52985298
# Highlight the case where the issue is an index out of range from the tuple

0 commit comments

Comments
 (0)