dcrck
Confusing dialyzer warning: The pattern can never match the type
Hi, a user found an issue when attempting to use my Ultraviolet library: Unexpected Dialyzer issue · Issue #1 · dcrck/ultraviolet · GitHub
defmodule MyModule do
require Logger
def testing do
sport_color = "pink"
case Ultraviolet.new(sport_color) do
{:error, m} -> Logger.error("should_not_happen", reason: m)
{:ok, color} -> color
end
end
end
error :
lib/my_module.ex:9:pattern_match
The pattern can never match the type.
Pattern:
{:ok, _color}
Type:
{:error, _}
________________________________________________________________________________
done (warnings were emitted)
Halting VM with exit status 2
Here’s a repository where I can reproduce the issue:
The warning doesn’t really make much sense to me, as I’ve used Color.new() in a variety of tests without encountering error cases, so I don’t know why dialyzer would see no path forward but the error case for a case I’ve definitely tested. Regardless, I’ve been working at this for a few hours now and I can’t seem to resolve this issue. Here’s what I’ve tried so far:
- fixing all the dialyzer warnings / errors in Ultraviolet. While the published version still has warnings, the
mainbranch does not. I’ve editedmix.exson the example repository to point to my local copy with all the warnings resolved. - expanded the metaprogramming construct I use to generate function signatures for the W3CX11 named colors.
- overriding the function to force a return type of
{:ok, %Color{}}in an attempt to get something working.
None of these seem to work. Does anyone have any suggestions for how I might solve this problem? Thanks!
Marked As Solved
dcrck
Okay, this took an embarrassingly long time to figure out, but since I was debugging the issue with the local version of my library, dialyzer did not even check for code changes to update the PLT (see this dialixir issue)! You need to use the --force-check argument in this case, i.e. mix dialyzer --force-check That should update the PLT accordingly. (TIL)
Now the checks pass without any issues. ![]()
Also Liked
LostKobrakai
Dialyzer doesn‘t care for what happens when the code actually runs. It cannot run it. It just cares for inferred types and how manual specs improve them.
You‘d want to figure out where dialyzer goes of the rails. One of its problems is that it will report the violation at a higher place in a callstack than where the source of the problem is. E.g. some of the helper functions to construct a color might have type inferred or speced wrongly.
dcrck
I think that particular error is due to the presence of a check for the optional Jason dependency to generate the proper function signatures at compile time (See lib/ultraviolet/color_brewer.ex). Future versions of the library will require 1.18 and use the built-in JSON parser introduced in that release, so this warning should go away.







