From 1f9256a735a56a5e4d069d9a16934c68bcf59484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 30 Dec 2024 11:39:36 +0100 Subject: [PATCH] Properly handle optional keys in map intersection --- lib/elixir/lib/module/types/descr.ex | 9 ++++----- lib/elixir/test/elixir/module/types/descr_test.exs | 3 +++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/elixir/lib/module/types/descr.ex b/lib/elixir/lib/module/types/descr.ex index be4547c76b..e808be394a 100644 --- a/lib/elixir/lib/module/types/descr.ex +++ b/lib/elixir/lib/module/types/descr.ex @@ -1330,11 +1330,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 diff --git a/lib/elixir/test/elixir/module/types/descr_test.exs b/lib/elixir/test/elixir/module/types/descr_test.exs index 61c28c613c..2d814672a1 100644 --- a/lib/elixir/test/elixir/module/types/descr_test.exs +++ b/lib/elixir/test/elixir/module/types/descr_test.exs @@ -167,6 +167,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())