[erlang-questions] Warning not emitted on string matching clauses

Anthony Ramine <>
Wed Apr 8 09:52:29 CEST 2015


Le 8 avr. 2015 à 08:46, Erik Søe Sørensen <> a écrit :

> It would of course make sense to have the compiler warn in this case.
> However - if my understanding of the compiler implementation is correct - matching of binaries is handled specially, and apparently in such a manner that it doesn't realise that the two binary patterns are identical.
> /Erik

That would be a reasonable explanation, but unfortunately it's not the case. If you compile that module to BEAM assembly, you can see very that the two patterns are indeed merged together, there is only one occurrence of "user_auth" in it.

{function, authenticate_nouns, 1, 2}.
  {label,1}.
    {line,[{location,"wat_clauses.erl",3}]}.
    {func_info,{atom,wat_clauses},{atom,authenticate_nouns},1}.
  {label,2}.
    {test,is_nonempty_list,{f,4},[{x,0}]}.
    {get_list,{x,0},{x,1},{x,2}}.
    {test,is_tuple,{f,4},[{x,1}]}.
    {test,test_arity,{f,4},[{x,1},2]}.
    {get_tuple_element,{x,1},0,{x,3}}.
    {get_tuple_element,{x,1},1,{x,4}}.
    {test,bs_start_match2,{f,4},5,[{x,3},0],{x,5}}.
    {test,bs_match_string,{f,4},[{x,5},72,{string,"user_auth"}]}.
    {test,bs_test_tail2,{f,4},[{x,5},0]}.
    {test,is_nil,{f,3},[{x,2}]}.
    {move,{atom,true},{x,0}}.
    return.
  {label,3}.
    {test,is_nonempty_list,{f,4},[{x,4}]}.
    {get_list,{x,4},{x,6},{x,7}}.
    {test,bs_start_match2,{f,4},8,[{x,6},0],{x,8}}.
    {test,bs_match_string,{f,4},[{x,8},64,{string,"recovery"}]}.
    {test,bs_test_tail2,{f,4},[{x,8},0]}.
    {test,is_nil,{f,4},[{x,7}]}.
    {test,is_nil,{f,4},[{x,2}]}.
    {move,{atom,hi},{x,0}}.
    return.
  {label,4}.
    {move,{atom,false},{x,0}}.
    return.



More information about the erlang-questions mailing list