Skip to content

Commit 0c59548

Browse files
committed
Improve protocol type error to list possible root causes
1 parent cdbfa09 commit 0c59548

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
@@ -487,7 +487,9 @@ defmodule Module.Types.IntegrationTest do
487487
488488
-dynamic(%Range{})-
489489
490-
but expected a type that implements the String.Chars protocol, it must be one of:
490+
but expected a type that implements the String.Chars protocol. You either passed the wrong value or you forgot to implement the protocol.
491+
492+
The String.Chars protocol is implemented for the following types:
491493
492494
dynamic(
493495
%Date{} or %DateTime{} or %NaiveDateTime{} or %Time{} or %URI{} or %Version{} or
@@ -511,7 +513,9 @@ defmodule Module.Types.IntegrationTest do
511513
512514
-dynamic(%Range{})-
513515
514-
but expected a type that implements the String.Chars protocol, it must be one of:
516+
but expected a type that implements the String.Chars protocol. You either passed the wrong value or you forgot to implement the protocol.
517+
518+
The String.Chars protocol is implemented for the following types:
515519
516520
dynamic(
517521
%Date{} or %DateTime{} or %NaiveDateTime{} or %Time{} or %URI{} or %Version{} or
@@ -551,7 +555,9 @@ defmodule Module.Types.IntegrationTest do
551555
552556
-dynamic(%Date{})-
553557
554-
but expected a type that implements the Enumerable protocol, it must be one of:
558+
but expected a type that implements the Enumerable protocol. You either passed the wrong value or you forgot to implement the protocol.
559+
560+
The Enumerable protocol is implemented for the following types:
555561
556562
dynamic(
557563
%Date.Range{} or %File.Stream{} or %GenEvent.Stream{} or %HashDict{} or %HashSet{} or
@@ -582,7 +588,9 @@ defmodule Module.Types.IntegrationTest do
582588
583589
-integer()-
584590
585-
but expected a type that implements the Collectable protocol, it must be one of:
591+
but expected a type that implements the Collectable protocol. You either passed the wrong value or you forgot to implement the protocol.
592+
593+
The Collectable protocol is implemented for the following types:
586594
587595
dynamic(%File.Stream{} or %HashDict{} or %HashSet{} or %IO.Stream{} or %MapSet{}) or binary() or
588596
empty_list() or non_empty_list(term(), term()) or non_struct_map()

0 commit comments

Comments
 (0)