[erlang-questions] Binding variables from binaries in function headers

Edward Wang yujiangw@REDACTED
Wed Mar 9 05:14:00 CET 2011


I can reproduce what you saw. And if I change the code as following:

---------------------------
case A =:= B of
    true -> wrong;
    _ -> ok
end.
---------------------------

it also works.

Very weird. For your original code erlc -S shows:

-------------------------------------------
    {move,{atom,wrong},{x,0}}.
    {deallocate,0}.
    return.
-------------------------------------------

It optimizes your case clause away altogether! This looks like a bug to me.

-Ed

On Wed, Mar 9, 2011 at 10:48 AM, Bernard Duggan <bernie@REDACTED> wrote:

> So I've just run into an interesting little bit of behaviour that doesn't
> seem quite right.  In the following code:
> -----------------------------------------
> -module(casetest).
>
> -export([test/0]).
>
> test() ->
>    match(<<1, 2, 3, 4, 5, 6, 7, 8>>).
>
> match(<<A:1/binary, B:8/integer, _C:B/binary, _Rest/binary>>) ->
>    case A of
>        B -> wrong;
>        _ -> ok
>    end.
> -----------------------------------------
> erlc gives me the warning
>
> ./casetest.erl:11: Warning: this clause cannot match because a previous
> clause at line 10 always matches
>
> (line 10 is the "B -> wrong;" line).
>
> And sure enough, if you run test/0 you get 'wrong' back.
>
> That, in itself, is curious to me since by my understanding B should be
> bound by the function header, and have no guarantee of being the same as A.
>  I can't see how it could be unbound.
>
> Doubly curious, is that if I stop using B as the size specifier of C, like
> this:
>
> match(<<A:1/binary, B:8/integer, _C:1/binary, _Rest/binary>>) ->
>
> The warning goes away.  And the result becomes 'ok' (in spite of nothing in
> the body having changed, and the only thing changing in the header being the
> size of an unused variable at the tail of the binary).  Similarly, if I
> change the body of match/1 to this:
>
>    Z = B,
>    case A of
>        Z -> wrong;
>        _ -> ok
>    end.
>
> It also works.
>
> Bug?  Feature?  Me making an obvious blunder that nobody I've shown this to
> can spot?
>
> Cheers,
>
> Bernard
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
>


More information about the erlang-questions mailing list