From 8a381e5ef1de9f0d44ddea6f592e5f587f3633fa Mon Sep 17 00:00:00 2001 From: Ulf Norell Date: Thu, 25 Apr 2019 16:06:50 +0200 Subject: [PATCH] Support equality on bytes(N) --- src/aeso_ast_to_icode.erl | 22 ++++++++++++++++++---- test/aeso_compiler_tests.erl | 3 ++- test/contracts/bytes_equality.aes | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 test/contracts/bytes_equality.aes diff --git a/src/aeso_ast_to_icode.erl b/src/aeso_ast_to_icode.erl index 62dc31a..bb7447a 100644 --- a/src/aeso_ast_to_icode.erl +++ b/src/aeso_ast_to_icode.erl @@ -579,15 +579,29 @@ ast_binop(Op, Ann, {typed, _, A, Type}, B, Icode) _ when not Monomorphic -> gen_error({cant_compare_polymorphic_type, Ann, Op, Type}); word -> #binop{op = Op, left = ast_body(A, Icode), right = ast_body(B, Icode)}; - string -> + OtherType -> Neg = case Op of '==' -> fun(X) -> X end; '!=' -> fun(X) -> #unop{ op = '!', rand = X } end; _ -> gen_error({cant_compare, Ann, Op, Type}) end, - Neg(#funcall{ function = #var_ref{name = {builtin, str_equal}}, - args = [ast_body(A, Icode), ast_body(B, Icode)] }); - _ -> gen_error({cant_compare, Ann, Op, Type}) + 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}) + end; + _ -> + gen_error({cant_compare, Ann, Op, Type}) + end, + Neg(Builtin) end; ast_binop('++', _, A, B, Icode) -> #funcall{ function = #var_ref{ name = {builtin, list_concat} }, diff --git a/test/aeso_compiler_tests.erl b/test/aeso_compiler_tests.erl index 320a5fb..7bcda3d 100644 --- a/test/aeso_compiler_tests.erl +++ b/test/aeso_compiler_tests.erl @@ -106,7 +106,8 @@ compilable_contracts() -> "include", "basic_auth", "bitcoin_auth", - "address_literals" + "address_literals", + "bytes_equality" ]. %% Contracts that should produce type errors diff --git a/test/contracts/bytes_equality.aes b/test/contracts/bytes_equality.aes new file mode 100644 index 0000000..cd0165b --- /dev/null +++ b/test/contracts/bytes_equality.aes @@ -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 +