[erlang-questions] [Q] dialyzer message: The pattern X can never match the type Y

Kostis Sagonas <>
Wed May 28 17:53:56 CEST 2008


Ladislav Lenart wrote:
> Hello,
> 
> first I'd like to say that dialyzer is a wonderful and
> easy-to-use tool.

Thanks!

> It has already found several bugs for me :-)

Don't worry: dialyzer has even found several bugs in its own code!

> But I don't understand what dialyzer is trying to tell
> me with a warning like:
> 
>    The pattern X can never match the type Y.
> 
> The actual code seems fine to me. It is similar to:
> 
> 	... CODE REMOVED ...
> 
> However, dialyzer does not report a warning like:

It might have helped if instead of presenting code that does not show 
the warning, you presented code which does.  Anyway, here is a slight 
modification of the program you sent:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-module(mod).
-export([f/1]).

f(Arg) when Arg == foo; Arg == bar; Arg == baz ->
    case Arg of
       foo -> ok1;                 % line 6
       gazonk -> weird;            % line 7
       _ -> ok2
    end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

dialyzer will report that:

mod.erl:7: The pattern 'gazonk' can never match the type 'bar' | 'baz'

In the first line of the function, the type of the Arg variable is 
constrained to the type 'foo' | 'bar' | 'baz'.  In line 6, 'foo' is 
consumed and the remaining type is 'bar' | 'baz'.  Since 'gazonk' is not 
part of this type, you get the above warning.

Hope this explains what the warning means.

Kostis



More information about the erlang-questions mailing list