Skip to content

Allow using protocol list implementation on improper lists #14366

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/elixir/lib/module/types/of.ex
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ defmodule Module.Types.Of do
{Float, float()},
{Function, fun()},
{Integer, integer()},
{List, list(term())},
{List, union(empty_list(), non_empty_list(term(), term()))},
{Map, open_map(__struct__: if_set(negation(atom())))},
{Port, port()},
{PID, pid()},
Expand Down
29 changes: 22 additions & 7 deletions lib/elixir/test/elixir/module/types/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ defmodule Module.Types.IntegrationTest do
assert itself_arg.(Itself.Float) == dynamic(float())
assert itself_arg.(Itself.Function) == dynamic(fun())
assert itself_arg.(Itself.Integer) == dynamic(integer())
assert itself_arg.(Itself.List) == dynamic(list(term()))

assert itself_arg.(Itself.List) ==
dynamic(union(empty_list(), non_empty_list(term(), term())))

assert itself_arg.(Itself.Map) == dynamic(open_map(__struct__: if_set(negation(atom()))))
assert itself_arg.(Itself.Port) == dynamic(port())
assert itself_arg.(Itself.PID) == dynamic(pid())
Expand Down Expand Up @@ -483,7 +486,7 @@ defmodule Module.Types.IntegrationTest do
dynamic(
%Date{} or %DateTime{} or %NaiveDateTime{} or %Time{} or %URI{} or %Version{} or
%Version.Requirement{}
) or atom() or binary() or float() or integer() or list(term())
) or atom() or binary() or empty_list() or float() or integer() or non_empty_list(term(), term())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should format this as maybe_improper_list(term(), term()) instead (and make a public helper for it in descr)


where "data" was given the type:

Expand All @@ -507,7 +510,7 @@ defmodule Module.Types.IntegrationTest do
dynamic(
%Date{} or %DateTime{} or %NaiveDateTime{} or %Time{} or %URI{} or %Version{} or
%Version.Requirement{}
) or atom() or binary() or float() or integer() or list(term())
) or atom() or binary() or empty_list() or float() or integer() or non_empty_list(term(), term())

where "data" was given the type:

Expand All @@ -520,6 +523,18 @@ defmodule Module.Types.IntegrationTest do
assert_warnings(files, warnings, consolidate_protocols: true)
end

test "String.Chars protocol dispatch on improper lists" do
files = %{
"a.ex" => """
defmodule FooBar do
def example1, do: to_string([?a, ?b | "!"])
end
"""
}

assert_no_warnings(files, consolidate_protocols: true)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, if we can add this to one of the existing tests, it would be better, as they can be expensive!


test "Enumerable protocol dispatch" do
files = %{
"a.ex" => """
Expand All @@ -546,7 +561,7 @@ defmodule Module.Types.IntegrationTest do
dynamic(
%Date.Range{} or %File.Stream{} or %GenEvent.Stream{} or %HashDict{} or %HashSet{} or
%IO.Stream{} or %MapSet{} or %Range{} or %Stream{}
) or fun() or list(term()) or non_struct_map()
) or empty_list() or fun() or non_empty_list(term(), term()) or non_struct_map()

where "date" was given the type:

Expand Down Expand Up @@ -575,7 +590,7 @@ defmodule Module.Types.IntegrationTest do
but expected a type that implements the Collectable protocol, it must be one of:

dynamic(%File.Stream{} or %HashDict{} or %HashSet{} or %IO.Stream{} or %MapSet{}) or binary() or
list(term()) or non_struct_map()
empty_list() or non_empty_list(term(), term()) or non_struct_map()

hint: the :into option in for-comprehensions use the Collectable protocol to build its result. Either pass a valid data type or implement the protocol accordingly
"""
Expand Down Expand Up @@ -1378,8 +1393,8 @@ defmodule Module.Types.IntegrationTest do
end)
end

defp assert_no_warnings(files) do
assert capture_compile_warnings(files, []) == ""
defp assert_no_warnings(files, opts \\ []) do
assert capture_compile_warnings(files, opts) == ""
end

defp capture_compile_warnings(files, opts) do
Expand Down
Loading