-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
contract_with_opaque.ex
53 lines (43 loc) · 1.32 KB
/
contract_with_opaque.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
defmodule Dialyxir.Warnings.ContractWithOpaque do
@moduledoc """
The @spec says the function is returning an opaque type, but it is
returning a different type.
## Example
defmodule Types do
@opaque type :: :ok
end
defmodule Example do
@spec ok() :: Types.type()
def ok() do
:ok
end
end
"""
@behaviour Dialyxir.Warning
@impl Dialyxir.Warning
@spec warning() :: :contract_with_opaque
def warning(), do: :contract_with_opaque
@impl Dialyxir.Warning
@spec format_short([String.t()]) :: String.t()
def format_short([_module, function | _]) do
"The @spec for #{function} has an opaque subtype which is violated by the success typing."
end
@impl Dialyxir.Warning
@spec format_long([String.t()]) :: String.t()
def format_long([module, function, arity, type, signature_type]) do
pretty_module = Erlex.pretty_print(module)
pretty_type = Erlex.pretty_print_type(type)
pretty_success_type = Erlex.pretty_print_contract(signature_type)
"""
The @spec for #{pretty_module}.#{function}/#{arity} has an opaque
subtype #{pretty_type} which is violated by the success typing.
Success typing:
#{pretty_success_type}
"""
end
@impl Dialyxir.Warning
@spec explain() :: String.t()
def explain() do
@moduledoc
end
end