Qqwy
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?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sasajuric
if apply(Kernel, :is_map, [super_result])might do the job, though it would also introduce some perf overhead.Something like
@dialyzer {:nowarn_function, generated_fun: arity}.al2o3cr
You can pass
generated: truetoquoteto get AST tagged with that option. Here’s an example fromKernel.is_struct:https://github.com/elixir-lang/elixir/blob/33a91237f68edf91afb45700ed149e0c7d1dd379/lib/elixir/lib/kernel.ex#L2431-L2442
If you’re producing AST nodes directly,
generated: truegoes in the metadata (second position in the tuple):https://github.com/elixir-lang/elixir/blob/79cd891eb86ecb7654a7acdb63769cfdd950a5c0/lib/ex_unit/lib/ex_unit/assertions.ex#L679-L681
Qqwy
All code which TypeCheck generates is annotated with
generated: truebut this does not seem to silence Dialyzer in the slightest. Maybe I am doing something wrong of course (I’m eager to learn how to improve the code further!)This way:
@spec, Dialyzer is still able to produce a warning as it should.Thank you very much for your help, @al2o3cr and @sasajuric !