[erlang-questions] Bad warning when using short-circuit boolean expressions
Bjorn Gustavsson
bgustavsson@REDACTED
Sat Mar 14 10:01:57 CET 2009
On Fri, Mar 13, 2009 at 7:36 PM, Anthony Molinaro <nym-erlang-q@REDACTED> wrote:
> Hi,
>
> I have this code
>
> -module (is_dict).
>
> -include_lib ("eunit/include/eunit.hrl").
>
> -define (is_dict (D), is_tuple (D) andalso element (1, D) =:= dict).
>
> is_dict_test () ->
> ?assertEqual (false, ?is_dict (a)),
> ?assertEqual (false, ?is_dict ({a, b})),
> ?assertEqual (false, ?is_dict ([{a, b}])),
> ?assertEqual (true, ?is_dict (dict:new())),
> ?assertEqual (true, ?is_dict (dict:from_list ([{a, b}]))).
>
> which gives
>
> Erlang (BEAM) emulator version 5.6.5 [source] [smp:2] [async-threads:0] [kernel-poll:false]
>
> Eshell V5.6.5 (abort with ^G)
> 1> c (is_dict).
> ./is_dict.erl:8: Warning: this expression will fail with a 'badarg' exception
> ./is_dict.erl:10: Warning: this expression will fail with a 'badarg' exception
> {ok,is_dict}
> 2> is_dict:test ().
> Test successful.
> ok
> 3>
>
> The warnings seem wrong as the expression does not fail (or maybe I'm just
> doing something wrong with this test?).
I agree that the warnings should not occur.
The reasons for the warnings is that
is_tuple(a) andalso is_element(1, a) =:= dict
will be translated to something similar to
case is_tuple(a) of
true -> element(1, a) =:= dict);
false -> false
end
and that is the code that the optimizer will see and generate a warning for.
We might fix the problem in a future release. (Not R13A and probably not
R13B either.)
/Bjorn
--
Björn Gustavsson, Erlang/OTP, Ericsson AB
More information about the erlang-questions
mailing list