Skip to content
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

Do not infer if type has been given #923

Closed
wants to merge 1 commit into from
Closed
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
41 changes: 1 addition & 40 deletions lib/explorer/shared.ex
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,8 @@ defmodule Explorer.Shared do

If no preferred type is given (nil), then the inferred type is returned.
"""
def dtype_from_list!(_list, :null), do: :null

def dtype_from_list!(list, nil), do: dtype_from_list!(list)

def dtype_from_list!(list, preferred_type) do
list
|> dtype_from_list!()
|> merge_preferred(preferred_type)
end
def dtype_from_list!(_list, preferred_type), do: preferred_type

@non_finite [:nan, :infinity, :neg_infinity]

Expand Down Expand Up @@ -351,38 +344,6 @@ defmodule Explorer.Shared do
{:struct, Enum.sort(types)}
end

defp merge_preferred(type, type), do: type
defp merge_preferred(:null, type), do: type
defp merge_preferred({:s, 64}, {:u, _} = type), do: type
defp merge_preferred({:s, 64}, {:s, _} = type), do: type
defp merge_preferred({:s, 64}, {:f, _} = type), do: type
defp merge_preferred({:f, 64}, {:f, _} = type), do: type
defp merge_preferred(:string, type) when type in [:binary, :string, :category], do: type

defp merge_preferred({:list, inferred}, {:list, preferred}) do
{:list, merge_preferred(inferred, preferred)}
end

defp merge_preferred({:struct, inferred}, {:struct, preferred}) do
{remaining, all_merged} =
Enum.reduce(preferred, {inferred, []}, fn {col, dtype}, {inferred_rest, merged} ->
case List.keytake(inferred_rest, col, 0) do
{{^col, inferred_dtype}, rest} ->
solved = merge_preferred(inferred_dtype, dtype)
{rest, List.keystore(merged, col, 0, {col, solved})}

nil ->
{inferred, List.keystore(merged, col, 0, {col, dtype})}
end
end)

{:struct, all_merged ++ remaining}
end

defp merge_preferred(inferred, _preferred) do
inferred
end

@doc """
Returns the leaf dtype from a {:list, _} dtype, or itself.
"""
Expand Down
Loading