Merge pull request #61 from aeternity/PT-165597438-equality-on-bytes

Support equality on bytes(N)
This commit is contained in:
Ulf Norell 2019-04-26 08:47:14 +02:00 committed by GitHub
commit 71b97cba62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 5 deletions

View File

@ -579,15 +579,29 @@ 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 =
_ -> gen_error({cant_compare, Ann, Op, Type}) 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})
end;
_ ->
gen_error({cant_compare, Ann, Op, Type})
end,
Neg(Builtin)
end; 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} },

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