Skip to content

Commit 640275b

Browse files
committed
Improve protocol type error to list possible root causes
1 parent 53fb96c commit 640275b

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

lib/elixir/lib/module/types/apply.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,10 @@ defmodule Module.Types.Apply do
10421042
else
10431043
# Protocol errors can be very verbose, so we collapse structs
10441044
"""
1045-
but expected a type that implements the #{inspect(mod)} protocol, it must be one of:
1045+
but expected a type that implements the #{inspect(mod)} protocol. You either passed the wrong \
1046+
value or you forgot to implement the protocol.
1047+
1048+
The #{inspect(mod)} protocol is implemented for the following types:
10461049
#{clauses_args_to_quoted_string(clauses, converter, collapse_structs: true)}
10471050
"""
10481051
end

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,9 @@ defmodule Module.Types.IntegrationTest do
558558
559559
-dynamic(%Range{})-
560560
561-
but expected a type that implements the String.Chars protocol, it must be one of:
561+
but expected a type that implements the String.Chars protocol. You either passed the wrong value or you forgot to implement the protocol.
562+
563+
The String.Chars protocol is implemented for the following types:
562564
563565
dynamic(
564566
%Date{} or %DateTime{} or %NaiveDateTime{} or %Time{} or %URI{} or %Version{} or
@@ -582,7 +584,9 @@ defmodule Module.Types.IntegrationTest do
582584
583585
-dynamic(%Range{})-
584586
585-
but expected a type that implements the String.Chars protocol, it must be one of:
587+
but expected a type that implements the String.Chars protocol. You either passed the wrong value or you forgot to implement the protocol.
588+
589+
The String.Chars protocol is implemented for the following types:
586590
587591
dynamic(
588592
%Date{} or %DateTime{} or %NaiveDateTime{} or %Time{} or %URI{} or %Version{} or
@@ -622,7 +626,9 @@ defmodule Module.Types.IntegrationTest do
622626
623627
-dynamic(%Date{})-
624628
625-
but expected a type that implements the Enumerable protocol, it must be one of:
629+
but expected a type that implements the Enumerable protocol. You either passed the wrong value or you forgot to implement the protocol.
630+
631+
The Enumerable protocol is implemented for the following types:
626632
627633
dynamic(
628634
%Date.Range{} or %File.Stream{} or %GenEvent.Stream{} or %HashDict{} or %HashSet{} or
@@ -653,7 +659,9 @@ defmodule Module.Types.IntegrationTest do
653659
654660
-integer()-
655661
656-
but expected a type that implements the Collectable protocol, it must be one of:
662+
but expected a type that implements the Collectable protocol. You either passed the wrong value or you forgot to implement the protocol.
663+
664+
The Collectable protocol is implemented for the following types:
657665
658666
dynamic(%File.Stream{} or %HashDict{} or %HashSet{} or %IO.Stream{} or %MapSet{}) or binary() or
659667
empty_list() or non_empty_list(term(), term()) or non_struct_map()

0 commit comments

Comments
 (0)