From 79342f45e71cf13a7ff65992d99940c7def66494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Tue, 21 Mar 2023 13:26:45 +0100 Subject: [PATCH] Tests for suffixes used to define operators --- .../literal_suffix_as_operator_definition.sol | 24 +++++++++++++++++++ ...g_operator_with_literal_suffix_on_udvt.sol | 10 ++++++++ ...ator_with_literal_suffix_on_value_type.sol | 13 ++++++++++ 3 files changed, 47 insertions(+) create mode 100644 test/libsolidity/semanticTests/operators/userDefined/literal_suffix_as_operator_definition.sol create mode 100644 test/libsolidity/syntaxTests/operators/userDefined/implementing_operator_with_literal_suffix_on_udvt.sol create mode 100644 test/libsolidity/syntaxTests/operators/userDefined/implementing_operator_with_literal_suffix_on_value_type.sol diff --git a/test/libsolidity/semanticTests/operators/userDefined/literal_suffix_as_operator_definition.sol b/test/libsolidity/semanticTests/operators/userDefined/literal_suffix_as_operator_definition.sol new file mode 100644 index 000000000..7852967f4 --- /dev/null +++ b/test/libsolidity/semanticTests/operators/userDefined/literal_suffix_as_operator_definition.sol @@ -0,0 +1,24 @@ +type B is bool; +using {bitnot as ~, bitor as |} for B global; + +// NOTE: There are no literals of type B so these cannot be actually used on anything +function bitnot(B x) pure suffix returns (B) { + return B.wrap(!B.unwrap(x)); +} + +function bitor(B x, B y) pure suffix returns (B) { + return B.wrap(B.unwrap(x) || B.unwrap(y)); +} + +contract C { + function testBinary() pure public returns (B) { + B.wrap(true) | B.wrap(false); + } + + function testUnary() pure public returns (B) { + -B.wrap(true); + } +} +// ---- +// testUnary() -> true +// testBinary() -> false diff --git a/test/libsolidity/syntaxTests/operators/userDefined/implementing_operator_with_literal_suffix_on_udvt.sol b/test/libsolidity/syntaxTests/operators/userDefined/implementing_operator_with_literal_suffix_on_udvt.sol new file mode 100644 index 000000000..9cf3b9e4b --- /dev/null +++ b/test/libsolidity/syntaxTests/operators/userDefined/implementing_operator_with_literal_suffix_on_udvt.sol @@ -0,0 +1,10 @@ +type Int is int; +using {add as +, unsub as -} for Int global; + +function add(Int x, Int y) pure suffix returns (Int) { + return Int.wrap(Int.unwrap(x) + Int.unwrap(y)); +} + +function unsub(Int x) pure suffix returns (Int) { + return Int.wrap(-Int.unwrap(x)); +} diff --git a/test/libsolidity/syntaxTests/operators/userDefined/implementing_operator_with_literal_suffix_on_value_type.sol b/test/libsolidity/syntaxTests/operators/userDefined/implementing_operator_with_literal_suffix_on_value_type.sol new file mode 100644 index 000000000..14d6290be --- /dev/null +++ b/test/libsolidity/syntaxTests/operators/userDefined/implementing_operator_with_literal_suffix_on_value_type.sol @@ -0,0 +1,13 @@ +using {add as +, unsub as -} for uint global; + +function add(uint x, uint y) pure suffix returns (uint) { + return x + y; +} + +function unsub(uint x) pure suffix returns (uint) { + return uint(-int(x)); +} +// ---- +// TypeError 8841: (0-45): Can only use "global" with user-defined types. +// TypeError 5332: (7-10): Operators can only be implemented for user-defined value types. +// TypeError 5332: (17-22): Operators can only be implemented for user-defined value types.