Support equality on bytes(N)

This commit is contained in:
Ulf Norell 2019-04-25 16:06:50 +02:00
parent 386419f112
commit 8a381e5ef1
3 changed files with 38 additions and 5 deletions

View File

@ -579,16 +579,30 @@ ast_binop(Op, Ann, {typed, _, A, Type}, B, Icode)
_ when not Monomorphic -> _ when not Monomorphic ->
gen_error({cant_compare_polymorphic_type, Ann, Op, Type}); gen_error({cant_compare_polymorphic_type, Ann, Op, Type});
word -> #binop{op = Op, left = ast_body(A, Icode), right = ast_body(B, Icode)}; word -> #binop{op = Op, left = ast_body(A, Icode), right = ast_body(B, Icode)};
string -> OtherType ->
Neg = case Op of Neg = case Op of
'==' -> fun(X) -> X end; '==' -> fun(X) -> X end;
'!=' -> fun(X) -> #unop{ op = '!', rand = X } end; '!=' -> fun(X) -> #unop{ op = '!', rand = X } end;
_ -> gen_error({cant_compare, Ann, Op, Type}) _ -> gen_error({cant_compare, Ann, Op, Type})
end, end,
Neg(#funcall{ function = #var_ref{name = {builtin, str_equal}}, Args = [ast_body(A, Icode), ast_body(B, Icode)],
args = [ast_body(A, Icode), ast_body(B, Icode)] }); Builtin =
case OtherType of
string ->
#funcall{ function = #var_ref{name = {builtin, str_equal}},
args = Args };
{tuple, Types} ->
case lists:usort(Types) of
[word] ->
#funcall{ function = #var_ref{name = {builtin, str_equal_p}},
args = [ #integer{value = 32 * length(Types)} | Args] };
_ -> gen_error({cant_compare, Ann, Op, Type}) _ -> gen_error({cant_compare, Ann, Op, Type})
end; end;
_ ->
gen_error({cant_compare, Ann, Op, Type})
end,
Neg(Builtin)
end;
ast_binop('++', _, A, B, Icode) -> ast_binop('++', _, A, B, Icode) ->
#funcall{ function = #var_ref{ name = {builtin, list_concat} }, #funcall{ function = #var_ref{ name = {builtin, list_concat} },
args = [ast_body(A, Icode), ast_body(B, Icode)] }; args = [ast_body(A, Icode), ast_body(B, Icode)] };

View File

@ -106,7 +106,8 @@ compilable_contracts() ->
"include", "include",
"basic_auth", "basic_auth",
"bitcoin_auth", "bitcoin_auth",
"address_literals" "address_literals",
"bytes_equality"
]. ].
%% Contracts that should produce type errors %% Contracts that should produce type errors

View File

@ -0,0 +1,18 @@
contract BytesEquality =
function eq16(a : bytes(16), b) = a == b
function ne16(a : bytes(16), b) = a != b
function eq32(a : bytes(32), b) = a == b
function ne32(a : bytes(32), b) = a != b
function eq47(a : bytes(47), b) = a == b
function ne47(a : bytes(47), b) = a != b
function eq64(a : bytes(64), b) = a == b
function ne64(a : bytes(64), b) = a != b
function eq65(a : bytes(65), b) = a == b
function ne65(a : bytes(65), b) = a != b