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

Merged
merged 2 commits into from
May 11, 2025
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
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
86 changes: 44 additions & 42 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 @@ -484,7 +487,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 @@ -508,7 +511,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 Down Expand Up @@ -548,7 +551,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 @@ -577,7 +580,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 @@ -648,6 +651,40 @@ defmodule Module.Types.IntegrationTest do
after
purge(A)
end

test "regressions" do
files = %{
# do not emit false positives from defguard
"a.ex" => """
defmodule A do
defguard is_non_nil_arity_function(fun, arity)
when arity != nil and is_function(fun, arity)

def check(fun, args) do
is_non_nil_arity_function(fun, length(args))
end
end
""",
# do not parse binary segments as variables
"b.ex" => """
defmodule B do
def decode(byte) do
case byte do
enc when enc in [<<0x00>>, <<0x01>>] -> :ok
end
end
end
""",
# String.Chars protocol dispatch on improper lists
"c.ex" => """
defmodule C do
def example, do: to_string([?a, ?b | "!"])
end
"""
}

assert_no_warnings(files, consolidate_protocols: true)
end
end

describe "undefined warnings" do
Expand Down Expand Up @@ -1163,41 +1200,6 @@ defmodule Module.Types.IntegrationTest do
end
end

describe "regressions" do
test "does not emit false positives from defguard" do
files = %{
"a.ex" => """
defmodule A do
defguard is_non_nil_arity_function(fun, arity)
when arity != nil and is_function(fun, arity)

def check(fun, args) do
is_non_nil_arity_function(fun, length(args))
end
end
"""
}

assert_no_warnings(files)
end

test "do not parse binary segments as variables" do
files = %{
"a.ex" => """
defmodule A do
def decode(byte) do
case byte do
enc when enc in [<<0x00>>, <<0x01>>] -> :ok
end
end
end
"""
}

assert_no_warnings(files)
end
end

describe "after_verify" do
test "reports functions" do
files = %{
Expand Down Expand Up @@ -1380,8 +1382,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