Skip to content

Commit

Permalink
Do not add duplicates on maps and tuples intersections
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jan 17, 2025
1 parent 1637dc7 commit 67b6f89
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions lib/elixir/lib/module/types/descr.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,16 @@ defmodule Module.Types.Descr do
acc ->
try do
{tag, fields} = map_literal_intersection(tag1, pos1, tag2, pos2)
[{tag, fields, negs1 ++ negs2} | acc]
entry = {tag, fields, negs1 ++ negs2}

# 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]
end
catch
:empty -> acc
end
Expand Down Expand Up @@ -1966,8 +1975,16 @@ defmodule Module.Types.Descr do
reduce: [] do
acc ->
case tuple_literal_intersection(tag1, elements1, tag2, elements2) do
{tag, fields} -> [{tag, fields, negs1 ++ negs2} | acc]
:empty -> acc
{tag, elements} ->
entry = {tag, elements, negs1 ++ negs2}

case :lists.member(entry, acc) do
true -> acc
false -> [entry | acc]
end

:empty ->
acc
end
end
|> case do
Expand Down

0 comments on commit 67b6f89

Please sign in to comment.