Quantcast

bug: missing warning statement

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

bug: missing warning statement

Joe Armstrong-2
%% 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

/Joe

_______________________________________________
erlang-bugs mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-bugs
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: bug: missing warning statement

Kostis Sagonas-2
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
_______________________________________________
erlang-bugs mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-bugs
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: bug: missing warning statement

bucko909
This post has NOT been accepted by the mailing list yet.
The following (shorter) test case warned only on f in erlang 1:14.b.3-dfsg-2
(that's 'R14B03 (erts-5.8.4)' according to erl)

-module(test).
-compile([export_all]).

f(X, 1) -> {ok, X};
f(a, 1) -> not_ok.

g(1, X) -> {ok, X};
g(1, a) -> not_ok.
Loading...