User-defined operators on structs

This commit is contained in:
Kamil Śliwak
2023-01-25 00:30:03 +01:00
parent 5657515f45
commit 05f4617275
27 changed files with 685 additions and 49 deletions
@@ -0,0 +1,22 @@
struct S {
uint v;
}
using {bitand as &} for S;
function bitand(S storage, S storage) returns (S storage) {
S storage rTmp;
return rTmp;
}
contract C {
S s;
function f() public {
S storage sTmp;
sTmp & s;
}
}
// ----
// TypeError 3464: (145-149): This variable is of storage pointer type and can be accessed without prior assignment, which would lead to undefined behaviour.
// TypeError 3464: (234-238): This variable is of storage pointer type and can be accessed without prior assignment, which would lead to undefined behaviour.
@@ -0,0 +1,22 @@
struct S {
uint v;
}
using {bitor as |} for S;
function bitor(S storage, S storage) returns (S storage) {
S storage rTmp;
return rTmp;
}
contract C {
S s;
function f() public {
S storage sTmp;
sTmp | s;
}
}
// ----
// TypeError 3464: (143-147): This variable is of storage pointer type and can be accessed without prior assignment, which would lead to undefined behaviour.
// TypeError 3464: (232-236): This variable is of storage pointer type and can be accessed without prior assignment, which would lead to undefined behaviour.
@@ -0,0 +1,17 @@
struct S { Z z; }
struct Z { int x; }
using {addS as +} for S;
using {addZ as +} for Z;
function addS(S memory, S memory) pure returns (S memory) {}
function addZ(Z memory, Z memory) pure returns (Z memory) { revert(); }
contract C {
function f() public pure {
S(Z(1)) + S(Z(2) + Z(3));
S(Z(4)) + S(Z(5)); // Unreachable
}
}
// ----
// Warning 5740: (310-327): Unreachable code.
@@ -0,0 +1,17 @@
struct S { Z z; }
struct Z { int x; }
using {unsubS as -} for S;
using {unsubZ as -} for Z;
function unsubS(S memory) pure returns (S memory) {}
function unsubZ(Z memory) pure returns (Z memory) { revert(); }
contract C {
function f() public pure {
-S(-Z(1));
-S(Z(2)); // Unreachable
}
}
// ----
// Warning 5740: (283-291): Unreachable code.