Skip to content

Commit 5a624f7

Browse files
committed
Allow using protocol list implementation on improper lists
1 parent b096b50 commit 5a624f7

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

lib/elixir/lib/module/types/of.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ defmodule Module.Types.Of do
132132
{Float, float()},
133133
{Function, fun()},
134134
{Integer, integer()},
135-
{List, list(term())},
135+
{List, union(empty_list(), non_empty_list(term(), term()))},
136136
{Map, open_map(__struct__: if_set(negation(atom())))},
137137
{Port, port()},
138138
{PID, pid()},

lib/elixir/test/elixir/module/types/integration_test.exs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@ defmodule Module.Types.IntegrationTest do
206206
assert itself_arg.(Itself.Float) == dynamic(float())
207207
assert itself_arg.(Itself.Function) == dynamic(fun())
208208
assert itself_arg.(Itself.Integer) == dynamic(integer())
209-
assert itself_arg.(Itself.List) == dynamic(list(term()))
209+
210+
assert itself_arg.(Itself.List) ==
211+
dynamic(union(empty_list(), non_empty_list(term(), term())))
212+
210213
assert itself_arg.(Itself.Map) == dynamic(open_map(__struct__: if_set(negation(atom()))))
211214
assert itself_arg.(Itself.Port) == dynamic(port())
212215
assert itself_arg.(Itself.PID) == dynamic(pid())
@@ -483,7 +486,7 @@ defmodule Module.Types.IntegrationTest do
483486
dynamic(
484487
%Date{} or %DateTime{} or %NaiveDateTime{} or %Time{} or %URI{} or %Version{} or
485488
%Version.Requirement{}
486-
) or atom() or binary() or float() or integer() or list(term())
489+
) or atom() or binary() or empty_list() or float() or integer() or non_empty_list(term(), term())
487490
488491
where "data" was given the type:
489492
@@ -507,7 +510,7 @@ defmodule Module.Types.IntegrationTest do
507510
dynamic(
508511
%Date{} or %DateTime{} or %NaiveDateTime{} or %Time{} or %URI{} or %Version{} or
509512
%Version.Requirement{}
510-
) or atom() or binary() or float() or integer() or list(term())
513+
) or atom() or binary() or empty_list() or float() or integer() or non_empty_list(term(), term())
511514
512515
where "data" was given the type:
513516
@@ -520,6 +523,18 @@ defmodule Module.Types.IntegrationTest do
520523
assert_warnings(files, warnings, consolidate_protocols: true)
521524
end
522525

526+
test "String.Chars protocol dispatch on improper lists" do
527+
files = %{
528+
"a.ex" => """
529+
defmodule FooBar do
530+
def example1, do: to_string([?a, ?b | "!"])
531+
end
532+
"""
533+
}
534+
535+
assert_no_warnings(files, consolidate_protocols: true)
536+
end
537+
523538
test "Enumerable protocol dispatch" do
524539
files = %{
525540
"a.ex" => """
@@ -546,7 +561,7 @@ defmodule Module.Types.IntegrationTest do
546561
dynamic(
547562
%Date.Range{} or %File.Stream{} or %GenEvent.Stream{} or %HashDict{} or %HashSet{} or
548563
%IO.Stream{} or %MapSet{} or %Range{} or %Stream{}
549-
) or fun() or list(term()) or non_struct_map()
564+
) or empty_list() or fun() or non_empty_list(term(), term()) or non_struct_map()
550565
551566
where "date" was given the type:
552567
@@ -575,7 +590,7 @@ defmodule Module.Types.IntegrationTest do
575590
but expected a type that implements the Collectable protocol, it must be one of:
576591
577592
dynamic(%File.Stream{} or %HashDict{} or %HashSet{} or %IO.Stream{} or %MapSet{}) or binary() or
578-
list(term()) or non_struct_map()
593+
empty_list() or non_empty_list(term(), term()) or non_struct_map()
579594
580595
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
581596
"""
@@ -1378,8 +1393,8 @@ defmodule Module.Types.IntegrationTest do
13781393
end)
13791394
end
13801395

1381-
defp assert_no_warnings(files) do
1382-
assert capture_compile_warnings(files, []) == ""
1396+
defp assert_no_warnings(files, opts \\ []) do
1397+
assert capture_compile_warnings(files, opts) == ""
13831398
end
13841399

13851400
defp capture_compile_warnings(files, opts) do

0 commit comments

Comments
 (0)