The following problem came up while developing TypeCheck.
For who does not know about it, it is a library which adds runtime type-checking functionality to your existing Elixir code by using macros that take your @type
and @spec
annotations and turn them into snippets that run before/after your functions.
A problem however occurs when people are using it in combination with Dialyzer. (c.f. issue #85)
Here is a minimal example:
defmodule Example do
defstruct [:age]
@type t() :: %__MODULE__{age: integer()}
@spec new(integer()) :: Example.t()
def new(age) do
%__MODULE__{age: age}
end
# The following code is generated by a macro:
defoverridable new: 1
def new(param) do
super_result = super(param)
maybe_error =
if is_map(super_result) do
{:ok, super_result}
else
{:error, :not_a_map}
end
case maybe_error do
{:ok, _} -> super_result
{:error, problem} -> raise "Result did not pass type check: #{problem}."
end
end
# (macro-generated code ends here)
end
The āmacro-generated codeā here is a simplified version of the actual code that would be inserted. (In the actual code we would first check whether the result is a map (as we do right now), then check whether the result contains the proper struct key and other expected keys, and then check whether :age
is an integer. But nevermind that, since the problem already occurs with this simplified snippet.)
However, in this particular case the user-written implementation of new
is simple enough to be ātrivially correctā.
But this makes Dialyzer complain about the checks which have been inserted. Specifically:
lib/dialyzer_tests.ex:0:pattern_match
The pattern can never match the type.
Pattern:
{:error, _problem}
Type:
{:ok, %Example{:age => _}}
________________________________________________________________________________
lib/dialyzer_tests.ex:0:pattern_match
The pattern can never match the type.
Pattern:
false
Type:
true
So there are one small and one big problem:
- Small: The line numbers seem to be messed up. I have no clue why. Maybe Dialyzer/Dialyxir is partially confused by
defoverridable
? - Big: Dialyzer complains about the error parts of this code not being reachable. But this is somewhat of a āfalse positiveā. It would be a proper warning if this code were to be written by hand. But since this is machine-generated code, warnings for branches that can never happen are very unhelpful. (As an aside: the BEAM compiler is clever enough to optimize them away as well in many cases.)
So this brings me to my question:
- Is it possible to make Dialyzer happy with this kind of machine-generated code?
- If not, is there a way to fully disable warnings for machine-generated code?