Disallow defining operators with non-pure functions

This commit is contained in:
Kamil Śliwak
2023-01-25 00:29:10 +01:00
parent 56ebb5f901
commit e0722732f6
7 changed files with 22 additions and 49 deletions
@@ -0,0 +1,24 @@
type Int is int16;
using {add as +} for Int;
function add(Int, Int) returns (Int) {
B b = new B();
return b.f();
}
contract B {
Int s;
function f() external returns (Int) {
s = Int.wrap(3);
return s;
}
}
contract C {
function test() public returns (Int) {
return Int.wrap(0) + Int.wrap(0);
}
}
// ----
// TypeError 7775: (27-30): Only pure functions can be used to define operators.
@@ -0,0 +1,12 @@
using {add as +, sub as -, mul as *} for A;
function add(A, A) view returns (A) {}
function sub(A, A) returns (A) {}
function mul(A, A) payable returns (A) {}
type A is address payable;
// ----
// TypeError 7775: (7-10): Only pure functions can be used to define operators.
// TypeError 7775: (17-20): Only pure functions can be used to define operators.
// TypeError 7775: (27-30): Only pure functions can be used to define operators.
// TypeError 9559: (118-159): Free functions cannot be payable.
@@ -1,17 +0,0 @@
type Int is uint8;
using {
add as +
} for Int;
function f_view() view {}
function add(Int, Int) view returns (Int) {
f_view();
return Int.wrap(0);
}
function f() view {
Int.wrap(0) + Int.wrap(0);
}
@@ -1,28 +0,0 @@
type Int is uint8;
using {
add as +
} for Int;
function f_view() view {}
function add(Int, Int) view returns (Int) {
f_view();
return Int.wrap(0);
}
function f() view {
Int.wrap(0) + Int.wrap(0);
}
function g() {
Int.wrap(0) + Int.wrap(0);
}
function h() pure {
Int.wrap(0) + Int.wrap(0);
}
// ----
// Warning 2018: (220-267): Function state mutability can be restricted to view
// TypeError 2527: (293-318): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view".