Tests for suffixes used to define operators

This commit is contained in:
Kamil Śliwak 2023-03-21 13:26:45 +01:00
parent 2c7ab0cf48
commit 79342f45e7
3 changed files with 47 additions and 0 deletions

View File

@ -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

View File

@ -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));
}

View File

@ -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.