Skip to content

Commit a7164bd

Browse files
committed
More progress
1 parent 39494ca commit a7164bd

2 files changed

Lines changed: 32 additions & 18 deletions

File tree

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ defmodule Module.Types.Pattern do
188188
try do
189189
{types, of_pattern_var_deps(changed, vars_paths, vars_deps, tag, stack, context)}
190190
catch
191-
{:error, context} -> {types, context}
191+
{:error, context} -> {types, error_vars(pattern_info, context)}
192192
end
193193
catch
194194
{types, context} -> {types, error_vars(pattern_info, context)}
@@ -250,15 +250,16 @@ defmodule Module.Types.Pattern do
250250
end
251251

252252
defp error_vars({args_paths, vars_paths, _vars_deps}, context) do
253-
callback = fn {_, [%{var: var} | _paths]}, context ->
253+
callback = fn [%{var: var} | _paths], context ->
254254
Of.error_var(var, context)
255255
end
256256

257-
context = Enum.reduce(args_paths, context, callback)
258-
context = Enum.reduce(vars_paths, context, callback)
257+
context = Enum.reduce(Map.values(args_paths), context, callback)
258+
context = Enum.reduce(Map.values(vars_paths), context, callback)
259259
context
260260
end
261261

262+
# TODO: May pass the expression as well for more context
262263
defp refine_error({_, meta, _} = var, old_type, type, stack, context) do
263264
error(__MODULE__, {:badvar, old_type, type, var, context}, meta, stack, context)
264265
end
@@ -799,6 +800,7 @@ defmodule Module.Types.Pattern do
799800
{pattern_info, %{context | pattern_info: nil}}
800801
end
801802

803+
# TODO: Deal when new_type is none()
802804
def format_diagnostic({:badvar, old_type, new_type, var, context}) do
803805
traces = collect_traces(var, context)
804806

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,19 @@ defmodule Module.Types.PatternTest do
5454
test "errors on conflicting refinements" do
5555
assert typeerror!([a = b, a = :foo, b = :bar], {a, b}) ==
5656
~l"""
57-
the following pattern will never match:
57+
incompatible types assigned to "a":
5858
59-
a = b
59+
dynamic(:foo) !~ dynamic(:bar)
6060
61-
where "a" was given the type:
61+
where "a" was given the types:
6262
6363
# type: dynamic(:foo)
64-
# from: types_test.ex:LINE-1
64+
# from: types_test.ex:55
6565
a = :foo
6666
67-
where "b" was given the type:
68-
6967
# type: dynamic(:bar)
70-
# from: types_test.ex:LINE-1
71-
b = :bar
68+
# from: types_test.ex:55
69+
a = b
7270
"""
7371
end
7472

@@ -120,17 +118,31 @@ defmodule Module.Types.PatternTest do
120118
assert typeerror!([x = {:ok, _} = {:error, _, _}], x) == ~l"""
121119
the following pattern will never match:
122120
123-
[_ | _] = x
121+
x = {:ok, _} = {:error, _, _}
122+
"""
124123

125-
because the right-hand side has type:
124+
assert typeerror!([x = {:ok, y} = {:error, z, w}], {x, y, z, w}) == ~l"""
125+
incompatible types assigned to "x":
126126
127-
dynamic({:ok, term()})
127+
dynamic() !~ none()
128128
129129
where "x" was given the type:
130130
131-
# type: dynamic({:ok, term()})
132-
# from: types_test.ex:LINE
133-
x = {:ok, _}
131+
# type: none()
132+
# from: types_test.ex:124
133+
x = {:ok, y} = {:error, z, w}
134+
"""
135+
136+
assert typeerror!([{:ok, y} = {:error, z, w}], {y, z, w}) == ~l"""
137+
incompatible types assigned to "match" (context Module.Types.Pattern):
138+
139+
dynamic() !~ none()
140+
141+
where "match" (context Module.Types.Pattern) was given the type:
142+
143+
# type: none()
144+
# from: types_test.ex:136
145+
{:ok, y} = {:error, z, w}
134146
"""
135147

136148
assert typeerror!([x = {:ok, _}], [_ | _] = x) == ~l"""

0 commit comments

Comments
 (0)