Skip to content

Commit 9721759

Browse files
committed
Improve error message for mismatched patterns
1 parent 6bace04 commit 9721759

2 files changed

Lines changed: 27 additions & 9 deletions

File tree

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ defmodule Module.Types.Pattern do
6363

6464
{:error, _old_type, error_context} ->
6565
if match_error?(var, new_type) do
66-
throw(badmatch_error(var, expr, stack, context))
66+
throw(badpattern_error(var, expr, stack, context))
6767
else
6868
throw(badvar_error(var, old_type, new_type, stack, error_context))
6969
end
7070
end
7171
end
7272

7373
:error ->
74-
throw(badmatch_error(var, expr, stack, context))
74+
throw(badpattern_error(var, expr, stack, context))
7575
end
7676
end)
7777
catch
@@ -260,9 +260,9 @@ defmodule Module.Types.Pattern do
260260
error(__MODULE__, error, error_meta(var, stack), stack, context)
261261
end
262262

263-
defp badmatch_error(var, expr, stack, context) do
263+
defp badpattern_error(var, expr, stack, context) do
264264
context = Of.error_var(var, context)
265-
error(__MODULE__, {:badmatch, expr, context}, error_meta(expr, stack), stack, context)
265+
error(__MODULE__, {:badpattern, expr, context}, error_meta(expr, stack), stack, context)
266266
end
267267

268268
defp badpattern_error(expr, index, tag, stack, context) do
@@ -272,7 +272,7 @@ defmodule Module.Types.Pattern do
272272
if index do
273273
{:badpattern, meta, expr, index, tag, context}
274274
else
275-
{:badmatch, expr, context}
275+
{:badpattern, expr, context}
276276
end
277277

278278
error(__MODULE__, error, meta, stack, context)
@@ -1012,15 +1012,15 @@ defmodule Module.Types.Pattern do
10121012
}
10131013
end
10141014

1015-
def format_diagnostic({:badmatch, expr, context}) do
1015+
def format_diagnostic({:badpattern, expr, context}) do
10161016
traces = collect_traces(expr, context)
10171017

10181018
%{
10191019
details: %{typing_traces: traces},
10201020
message:
10211021
IO.iodata_to_binary([
10221022
"""
1023-
this match will never succeed due to incompatible types:
1023+
the following pattern will never match:
10241024
10251025
#{expr_to_string(expr) |> indent(4)}
10261026
""",
@@ -1146,7 +1146,7 @@ defmodule Module.Types.Pattern do
11461146
"""}
11471147
end
11481148

1149-
defp badpattern({:infer, types}, pattern_or_expr, index) do
1149+
defp badpattern({:infer, types}, pattern_or_expr, index) when is_integer(index) do
11501150
type = Enum.fetch!(types, index)
11511151

11521152
if type == dynamic() do

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ defmodule Module.Types.PatternTest do
145145
"""
146146

147147
assert typeerror!([{x, _} = {y, _}, x = :foo, y = :bar], {x, y}) == ~l"""
148-
this match will never succeed due to incompatible types:
148+
the following pattern will never match:
149149
150150
{x, _} = {y, _}
151151
@@ -161,6 +161,24 @@ defmodule Module.Types.PatternTest do
161161
# from: types_test.ex:LINE
162162
y = :bar
163163
"""
164+
165+
assert typeerror!([{:ok, x} = {:ok, y}], is_integer(x) and is_atom(y), {x, y}) == ~l"""
166+
the following pattern will never match:
167+
168+
{:ok, x} = {:ok, y}
169+
170+
where "x" was given the type:
171+
172+
# type: integer()
173+
# from: types_test.ex:LINE
174+
is_integer(x)
175+
176+
where "y" was given the type:
177+
178+
# type: dynamic(atom())
179+
# from: types_test.ex:LINE
180+
is_atom(y)
181+
"""
164182
end
165183
end
166184

0 commit comments

Comments
 (0)