Skip to content

Commit cebc5a7

Browse files
committed
Better error message for when all patterns are exhausted
1 parent c40c140 commit cebc5a7

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

lib/elixir/lib/module/types/pattern.ex

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1362,10 +1362,26 @@ defmodule Module.Types.Pattern do
13621362
#{to_quoted_string(type) |> indent(4)}
13631363
"""}
13641364

1365+
subtype?(type, previous_type) ->
1366+
{pattern,
1367+
"""
1368+
the following clause cannot match because the previous clauses already matched all possible values:
1369+
1370+
#{expr_to_string(pattern) |> indent(4)} ->
1371+
1372+
it attempts to match on the result of:
1373+
1374+
#{expr_to_string(expr) |> indent(4)}
1375+
1376+
and the following types have already been matched:
1377+
1378+
#{to_quoted_string(previous_type) |> indent(4)}
1379+
"""}
1380+
13651381
true ->
13661382
{pattern,
13671383
"""
1368-
the following clause cannot match because a previous clauses already matched this pattern:
1384+
the following clause cannot match because previous clauses already matched this pattern:
13691385
13701386
#{expr_to_string(pattern) |> indent(4)} ->
13711387

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,14 +1685,35 @@ defmodule Module.Types.ExprTest do
16851685
end
16861686

16871687
test "reports error from redundant clauses" do
1688+
assert typeerror!(
1689+
[x],
1690+
case System.get_env(x) do
1691+
nil -> 1
1692+
b when is_binary(b) -> 2
1693+
other -> other
1694+
end
1695+
) == ~l"""
1696+
the following clause cannot match because the previous clauses already matched all possible values:
1697+
1698+
other ->
1699+
1700+
it attempts to match on the result of:
1701+
1702+
System.get_env(x)
1703+
1704+
and the following types have already been matched:
1705+
1706+
nil or binary()
1707+
"""
1708+
16881709
assert typeerror!(
16891710
[x],
16901711
case String.to_atom(x) do
16911712
:ok -> 1
16921713
:ok -> 2
16931714
end
16941715
) == ~l"""
1695-
the following clause cannot match because a previous clauses already matched this pattern:
1716+
the following clause cannot match because previous clauses already matched this pattern:
16961717
16971718
:ok ->
16981719
@@ -1708,7 +1729,7 @@ defmodule Module.Types.ExprTest do
17081729
{x, y} when is_integer(x) and is_integer(y) -> 2
17091730
end
17101731
) =~ ~l"""
1711-
the following clause cannot match because a previous clauses already matched this pattern:
1732+
the following clause cannot match because previous clauses already matched this pattern:
17121733
17131734
{x, y} when is_integer(x) and is_integer(y) ->
17141735

0 commit comments

Comments
 (0)