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

OvermindDL1 <>
Fri May 6 04:28:08 CEST 2011


On Wed, Mar 9, 2011 at 3:56 PM, OvermindDL1 <> wrote:
> On Wed, Mar 9, 2011 at 3:48 PM, OvermindDL1 <> wrote:
>> On Tue, Mar 8, 2011 at 9:26 PM, Wes James <> wrote:
>>> On Tue, Mar 8, 2011 at 7:48 PM, Bernard Duggan <> 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.
>>>
>>> <snip>
>>>
>>> I just ran it in the debugger:
>>>
>>> c(casetest,[debug_info]).
>>> im().
>>> ii(casetest).
>>> iaa([init]).
>>>
>>> and it follows the _ -> ok branch and returns ok.
>>>
>>> Entering the function (the debugger stops at "case A of" ) the vars are:
>>>
>>> A is <<1>>
>>> B is 2
>>> _C is <<3,4>
>>> _Rest is <<5,6,7,8>>
>>
>> I confirm the above tests, and in fact if I change the module to this:
>> """
>> 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>>) ->
>>   io:format("~p~n", [{A,B,_C,_Rest}]),
>>   case A of
>>       B -> wrong;
>>       _ -> ok
>>   end.
>> """
>>
>> Then the erl shell does this:
>> Erlang R14B01 (erts-5.8.2) [source] [64-bit] [smp:6:6] [rq:6]
>> [async-threads:0] [hipe] [kernel-poll:false]
>>
>> Eshell V5.8.2  (abort with ^G)
>> 1> c(casetest).
>> ./casetest.erl:12: Warning: this clause cannot match because a
>> previous clause at line 11 always matches
>> {ok,casetest}
>> 2> casetest:test().
>> {<<1>>,2,<<3,4>>,<<5,6,7,8>>}
>> wrong
>> 3>
>>
>>
>> What is going on here?  I use binaries heavily in my apps and I would
>> like to be assured that this bug will not sneak in.
>
>
> I just confirmed it in R13B03 as well:
>
> Erlang R13B03 (erts-5.7.4) [source] [64-bit] [smp:6:6] [rq:6]
> [async-threads:0] [hipe] [kernel-poll:false]
>
> Eshell V5.7.4  (abort with ^G)
> 1> c(casetest).
> ./casetest.erl:12: Warning: this clause cannot match because a
> previous clause at line 11 always matches
> {ok,casetest}
> 2> casetest:test().
> {<<1>>,2,<<3,4>>,<<5,6,7,8>>}
> wrong
> 3>

Not to beat a dead horse, but this still worries me, any thoughts?
Even if not fixable in the old versions, I would at least like to know
the reason 'why' it breaks so as to help prevent it in the future...



More information about the erlang-questions mailing list