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.