Skip to content

Commit ca6edfd

Browse files
committed
Include types in comparison warning
1 parent ed83c40 commit ca6edfd

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

Diff for: lib/elixir/lib/module/types/apply.ex

+22-5
Original file line numberDiff line numberDiff line change
@@ -363,14 +363,14 @@ defmodule Module.Types.Apply do
363363

364364
match?({false, _}, map_fetch(left, :__struct__)) or
365365
match?({false, _}, map_fetch(right, :__struct__)) ->
366-
warning = {:struct_comparison, expr, context}
366+
warning = {:struct_comparison, expr, name, left, right, context}
367367
warn(__MODULE__, warning, elem(expr, 1), stack, context)
368368

369369
number_type?(left) and number_type?(right) ->
370370
context
371371

372372
disjoint?(left, right) ->
373-
warning = {:mismatched_comparison, expr, context}
373+
warning = {:mismatched_comparison, expr, name, left, right, context}
374374
warn(__MODULE__, warning, elem(expr, 1), stack, context)
375375

376376
true ->
@@ -395,7 +395,7 @@ defmodule Module.Types.Apply do
395395
context
396396

397397
disjoint?(left, right) ->
398-
warning = {:mismatched_comparison, expr, context}
398+
warning = {:mismatched_comparison, expr, name, left, right, context}
399399
warn(__MODULE__, warning, elem(expr, 1), stack, context)
400400

401401
true ->
@@ -810,7 +810,7 @@ defmodule Module.Types.Apply do
810810
}
811811
end
812812

813-
def format_diagnostic({:mismatched_comparison, expr, context}) do
813+
def format_diagnostic({:mismatched_comparison, expr, name, left, right, context}) do
814814
traces = collect_traces(expr, context)
815815

816816
%{
@@ -821,6 +821,10 @@ defmodule Module.Types.Apply do
821821
comparison between distinct types found:
822822
823823
#{expr_to_string(expr) |> indent(4)}
824+
825+
given types:
826+
827+
#{type_comparison_to_string(name, left, right) |> indent(4)}
824828
""",
825829
format_traces(traces),
826830
"""
@@ -833,7 +837,7 @@ defmodule Module.Types.Apply do
833837
}
834838
end
835839

836-
def format_diagnostic({:struct_comparison, expr, context}) do
840+
def format_diagnostic({:struct_comparison, expr, name, left, right, context}) do
837841
traces = collect_traces(expr, context)
838842

839843
%{
@@ -844,6 +848,10 @@ defmodule Module.Types.Apply do
844848
comparison with structs found:
845849
846850
#{expr_to_string(expr) |> indent(4)}
851+
852+
given types:
853+
854+
#{type_comparison_to_string(name, left, right) |> indent(4)}
847855
""",
848856
format_traces(traces),
849857
"""
@@ -933,6 +941,15 @@ defmodule Module.Types.Apply do
933941

934942
alias Inspect.Algebra, as: IA
935943

944+
defp type_comparison_to_string(fun, left, right) do
945+
{Kernel, fun, [left, right], _} = :elixir_rewrite.erl_to_ex(:erlang, fun, [left, right])
946+
947+
{fun, [], [to_quoted(left), to_quoted(right)]}
948+
|> Code.Formatter.to_algebra()
949+
|> Inspect.Algebra.format(98)
950+
|> IO.iodata_to_binary()
951+
end
952+
936953
defp clauses_args_to_quoted_string([{args, _return}], converter) do
937954
"\n " <> (clause_args_to_quoted_string(args, converter) |> indent(4))
938955
end

Diff for: lib/elixir/test/elixir/module/types/expr_test.exs

+12
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,10 @@ defmodule Module.Types.ExprTest do
845845
846846
min(x, y)
847847
848+
given types:
849+
850+
min(dynamic(:foo), integer())
851+
848852
where "x" was given the type:
849853
850854
# type: dynamic(:foo)
@@ -868,6 +872,10 @@ defmodule Module.Types.ExprTest do
868872
869873
x === y
870874
875+
given types:
876+
877+
integer() === float()
878+
871879
where "x" was given the type:
872880
873881
# type: integer()
@@ -893,6 +901,10 @@ defmodule Module.Types.ExprTest do
893901
894902
mod.<=(x, y)
895903
904+
given types:
905+
906+
dynamic(:foo) <= dynamic(%Point{x: term(), y: term(), z: term()})
907+
896908
where "mod" was given the type:
897909
898910
# type: dynamic(Kernel)

0 commit comments

Comments
 (0)