[erlang-bugs] bug: missing warning statement

Kostis Sagonas kostis@REDACTED
Thu Apr 28 11:03:33 CEST 2011


Joe Armstrong wrote:
> %% How come no warnings for functions a .. c.
> %% I'd expected a warning as in d
> 
> -module(bug).
> -compile(export_all).
> 
> -define(IS_DIGIT(X),($0=<X andalso X=<$9)).
> 
> a([X|_]) when ?IS_DIGIT(X) -> a;
> a("0x" ++ _) -> b.
> 
> b([X|_]) when $0=<X andalso X=<$9 -> a;
> b("0x" ++ _) -> b.
> 
> c([X|_]) when X == $0 -> a;
> c("0x" ++ _) -> b.
>    
> d([$0|_]) -> a;
> d("0x" ++ _) -> b.
>   
>    
> %% erlc bug.erl
> %% ./bug.erl:16: Warning: this clause cannot match because a previous 
> clause at line 15 always matches

Somebody designed the ==, =<, etc. operators to have very weird 
semantics Joe :-), and not be equivalent to matching (i.e., exact 
arithmetic equality), as in the last case.

I suppose the compiler could be a bit more clever and have some simple 
logic e.g. that Var == Integer implies Var =:= Integer, but apparently 
this is not there yet.

(The logic needed for intervals is even more involved, I am afraid.)

<aside>
I would be a bit more conservative in what should be classified as a 
bug. I might be wrong, but AFAIK the compiler nowhere promised that it 
would generate all warnings. (In fact, not so long ago, there were 
people who were objecting to it generating warnings by default...)
</aside>

But I agree with you that it would be nice for the compiler to generate 
warnings for some/all of these cases.

Cheers,
Kostis



More information about the erlang-bugs mailing list