Skip to content

Remove duplicates when computing map_difference #14625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 41 additions & 15 deletions lib/elixir/lib/module/types/descr.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2370,6 +2370,8 @@ defmodule Module.Types.Descr do
if subtype?(v2, v1), do: :right_subtype_of_left
end

defp map_intersection(dnf, dnf), do: dnf

# Given two unions of maps, intersects each pair of maps.
defp map_intersection(dnf1, dnf2) do
for {tag1, pos1, negs1} <- dnf1,
Expand All @@ -2378,15 +2380,17 @@ defmodule Module.Types.Descr do
acc ->
try do
{tag, fields} = map_literal_intersection(tag1, pos1, tag2, pos2)
entry = {tag, fields, negs1 ++ negs2}
negs = negs1 ++ (negs2 -- negs1)
entry = {tag, fields, negs}

# Imagine a, b, c, where a is closed and b and c are open with
# no keys in common. The result in both cases will be a and we
# want to avoid adding duplicates, especially as intersection
# is a cartesian product.
case :lists.member(entry, acc) do
true -> acc
false -> [entry | acc]
cond do
:lists.member({tag, fields}, negs) -> acc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove these checks? I think the will be much harder to do once we change the representation of maps. We should keep only the :lists.member(entry, acc) for now. Do you think that would be enough?

:lists.member(entry, acc) -> acc
true -> [entry | acc]
end
catch
:empty -> acc
Expand Down Expand Up @@ -2454,33 +2458,55 @@ defmodule Module.Types.Descr do
if empty?(type), do: throw(:empty), else: type
end

defp map_difference(_, dnf) when dnf == @map_top do
0
end
defp map_difference(_, dnf) when dnf == @map_top, do: 0
defp map_difference(dnf, dnf), do: 0

defp map_difference(dnf1, dnf2) do
Enum.reduce(dnf2, dnf1, fn
# Optimization: we are removing an open map with one field.
{:open, fields2, []}, dnf1 when map_size(fields2) == 1 ->
Enum.reduce(dnf1, [], fn {tag1, fields1, negs1}, acc ->
{:open, fields2, []}, current_dnf when map_size(fields2) == 1 ->
# Optimization: we are removing an open map with one field.
Enum.reduce(current_dnf, [], fn {tag1, fields1, negs1}, acc ->
{key, value, _rest} = :maps.next(:maps.iterator(fields2))
t_diff = difference(Map.get(fields1, key, tag_to_type(tag1)), value)

if empty?(t_diff) do
acc
else
[{tag1, Map.put(fields1, key, t_diff), negs1} | acc]
{tag, pos} = {tag1, Map.put(fields1, key, t_diff)}
entry = {tag, pos, negs1}

cond do
:lists.member({tag, pos}, negs1) -> acc
:lists.member(entry, acc) -> acc
true -> [entry | acc]
end
end
end)

{tag2, fields2, negs2}, dnf1 ->
Enum.reduce(dnf1, [], fn {tag1, fields1, negs1}, acc ->
acc = [{tag1, fields1, [{tag2, fields2} | negs1]} | acc]
{tag2, fields2, negs2}, current_dnf ->
Enum.reduce(current_dnf, [], fn {tag1, fields1, negs1}, acc ->
negs =
if :lists.member({tag2, fields2}, negs1), do: negs1, else: [{tag2, fields2} | negs1]

entry = {tag1, fields1, negs}

acc =
cond do
:lists.member({tag1, fields1}, negs) -> acc
:lists.member(entry, acc) -> acc
true -> [entry | acc]
end

Enum.reduce(negs2, acc, fn {neg_tag2, neg_fields2}, acc ->
try do
{tag, fields} = map_literal_intersection(tag1, fields1, neg_tag2, neg_fields2)
[{tag, fields, negs1} | acc]
entry = {tag, fields, negs1}

cond do
:lists.member({tag, fields}, negs1) -> acc
:lists.member(entry, acc) -> acc
true -> [entry | acc]
end
catch
:empty -> acc
end
Expand Down