Skip to content

Hoist dynamic up on map creation #13579

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

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 26 additions & 7 deletions lib/elixir/lib/module/types/descr.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule Module.Types.Descr do
def atom(as), do: %{atom: atom_new(as)}
def atom(), do: %{atom: @atom_top}
def binary(), do: %{bitmap: @bit_binary}
def closed_map(pairs), do: %{map: map_new(:closed, :maps.from_list(pairs))}
def closed_map(pairs), do: map_descr(:closed, pairs)
def empty_list(), do: %{bitmap: @bit_empty_list}
def empty_map(), do: %{map: @map_empty}
def integer(), do: %{bitmap: @bit_integer}
Expand All @@ -59,7 +59,7 @@ defmodule Module.Types.Descr do
def list(), do: %{bitmap: @bit_list}
def non_empty_list(), do: %{bitmap: @bit_non_empty_list}
def open_map(), do: %{map: @map_top}
def open_map(pairs), do: %{map: map_new(:open, :maps.from_list(pairs))}
def open_map(pairs), do: map_descr(:open, pairs)
def pid(), do: %{bitmap: @bit_pid}
def port(), do: %{bitmap: @bit_port}
def reference(), do: %{bitmap: @bit_reference}
Expand Down Expand Up @@ -597,15 +597,34 @@ defmodule Module.Types.Descr do
# `%{..., a: if_set(not atom()), b: integer()}`. For maps with more keys,
# each key in a negated literal may create a new union when eliminated.

defp map_descr(tag, fields) do
case map_descr_pairs(fields, [], false) do
{fields, true} ->
%{dynamic: %{map: map_new(tag, fields |> Enum.reverse() |> :maps.from_list())}}

{_, false} ->
%{map: map_new(tag, :maps.from_list(fields))}
end
end

defp map_descr_pairs([{key, value} | rest], acc, dynamic?) do
case :maps.take(:dynamic, value) do
:error -> map_descr_pairs(rest, [{key, value} | acc], dynamic?)
{dynamic, _static} -> map_descr_pairs(rest, [{key, dynamic} | acc], true)
end
end

defp map_descr_pairs([], acc, dynamic?) do
{acc, dynamic?}
end

defp optional?(%{bitmap: bitmap}) when (bitmap &&& @bit_optional) != 0, do: true
defp optional?(_), do: false

defp map_tag_to_type(:open), do: term_or_optional()
defp map_tag_to_type(:closed), do: not_set()

# TODO: We need to propagate any dynamic up
defp map_new(tag, fields), do: [{tag, fields, []}]
defp map_descr(tag, fields), do: %{map: map_new(tag, fields)}
defp map_new(tag, fields = %{}), do: [{tag, fields, []}]

@doc """
Fetches the type of the value returned by accessing `key` on `map`
Expand Down Expand Up @@ -834,8 +853,8 @@ defmodule Module.Types.Descr do

defp map_pop_key(tag, fields, key) do
case :maps.take(key, fields) do
{value, fields} -> {value, map_descr(tag, fields)}
:error -> {map_tag_to_type(tag), map_descr(tag, fields)}
{value, fields} -> {value, %{map: map_new(tag, fields)}}
:error -> {map_tag_to_type(tag), %{map: map_new(tag, fields)}}
end
end

Expand Down
11 changes: 10 additions & 1 deletion lib/elixir/test/elixir/module/types/descr_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ defmodule Module.Types.DescrTest do
end
end

describe "creation" do
test "map hoists dynamic" do
assert dynamic(open_map(a: integer())) == open_map(a: dynamic(integer()))

assert dynamic(open_map(a: union(integer(), binary()))) ==
open_map(a: dynamic(integer()) |> union(binary()))
end
end

describe "subtype" do
test "bitmap" do
assert subtype?(integer(), union(integer(), float()))
Expand Down Expand Up @@ -240,7 +249,7 @@ defmodule Module.Types.DescrTest do

describe "empty" do
test "map" do
assert intersection(closed_map(b: atom()), open_map(a: integer())) |> empty?
assert intersection(closed_map(b: atom()), open_map(a: integer())) |> empty?()
end
end

Expand Down
Loading