Skip to content

Commit f300c7e

Browse files
committed
Annotate the root level type that does not match a given expression
1 parent 8302dbf commit f300c7e

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

lib/elixir/lib/module/types/apply.ex

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ defmodule Module.Types.Apply do
5757
list(closed_map(default: if_set(term()), field: atom()))
5858
|> union(atom([nil]))
5959
] ++ shared_info,
60+
# TODO: Move this to a type signature in the long term
6061
__protocol__: [
6162
module: atom(),
6263
functions: fas,
@@ -1069,22 +1070,39 @@ defmodule Module.Types.Apply do
10691070
end
10701071

10711072
defp args_to_quoted_string(args_types, domain, converter) do
1072-
ansi? = IO.ANSI.enabled?()
1073-
10741073
docs =
10751074
Enum.zip_with(args_types, domain, fn actual, expected ->
1076-
doc = actual |> to_quoted() |> Code.Formatter.to_algebra()
1077-
1078-
cond do
1079-
compatible?(actual, expected) -> doc
1080-
ansi? -> IA.concat(IA.color(doc, IO.ANSI.red()), IA.color(IA.empty(), IO.ANSI.reset()))
1081-
true -> IA.concat(["-", doc, "-"])
1075+
if compatible?(actual, expected) do
1076+
actual |> to_quoted() |> Code.Formatter.to_algebra()
1077+
else
1078+
common = intersection(actual, expected)
1079+
1080+
uncommon_doc =
1081+
difference(actual, common)
1082+
|> to_quoted()
1083+
|> Code.Formatter.to_algebra()
1084+
|> ansi_red()
1085+
1086+
if empty?(common) do
1087+
uncommon_doc
1088+
else
1089+
common_doc = common |> to_quoted() |> Code.Formatter.to_algebra()
1090+
IA.glue(IA.concat(uncommon_doc, " or"), IA.nest(common_doc, 2))
1091+
end
10821092
end
10831093
end)
10841094

10851095
args_docs_to_quoted_string(converter.(docs))
10861096
end
10871097

1098+
defp ansi_red(doc) do
1099+
if IO.ANSI.enabled?() do
1100+
IA.concat(IA.color(doc, IO.ANSI.red()), IA.color(IA.empty(), IO.ANSI.reset()))
1101+
else
1102+
IA.concat(["-", doc, "-"])
1103+
end
1104+
end
1105+
10881106
defp args_docs_to_quoted_string(docs) do
10891107
doc = IA.fold(docs, fn doc, acc -> IA.glue(IA.concat(doc, ","), acc) end)
10901108

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ defmodule Module.Types.PatternTest do
3434
3535
given types:
3636
37-
list(integer())
37+
empty_list() or non_empty_list(integer())
3838
3939
but expected one of:
4040

0 commit comments

Comments
 (0)