Skip to content

Commit

Permalink
Properly handle optional keys in map intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Dec 30, 2024
1 parent 7e14961 commit f3dfb72
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/elixir/lib/module/types/descr.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1329,11 +1329,10 @@ defmodule Module.Types.Descr do
:maps.next(iterator) |> map_literal_intersection_loop(acc)

_ ->
# If the key is marked as not_set in the open map, we can ignore it.
if type1 == @not_set do
:maps.next(iterator) |> map_literal_intersection_loop(acc)
else
throw(:empty)
# If the key is optional in the open map, we can ignore it
case type1 do
%{optional: 1} -> :maps.next(iterator) |> map_literal_intersection_loop(acc)
_ -> throw(:empty)
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/elixir/test/elixir/module/types/descr_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ defmodule Module.Types.DescrTest do
assert intersection(closed_map(a: integer()), open_map(b: not_set())) ==
closed_map(a: integer())

assert intersection(closed_map(a: integer()), open_map(b: if_set(integer()))) ==
closed_map(a: integer())

assert equal?(
intersection(closed_map(a: integer()), closed_map(a: if_set(integer()))),
closed_map(a: integer())
Expand Down

0 comments on commit f3dfb72

Please sign in to comment.