Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e072273

Browse files
committed
Disallow defining operators with non-pure functions
1 parent 56ebb5f commit e072273

File tree

7 files changed

+22
-49
lines changed

7 files changed

+22
-49
lines changed

docs/contracts/using-for.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Note that private library functions can only be specified when ``using for`` is
4545

4646
If you define an operator (e.g. ``using {f as +} for T``), then the type (``T``) must be a
4747
:ref:`user-defined value type <user-defined-value-types>`.
48-
The definition of an operator must be a function with the types of all parameters and
48+
The definition of an operator must be a ``pure`` function with the types of all parameters and
4949
the return value matching ``T``, except for comparison operators, where the return value must
5050
be of type ``bool``.
5151

libsolidity/analysis/TypeChecker.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4001,6 +4001,13 @@ void TypeChecker::endVisit(UsingForDirective const& _usingFor)
40014001
}
40024002
solAssert(usingForType->typeDefinition());
40034003

4004+
if (functionType->stateMutability() != StateMutability::Pure)
4005+
m_errorReporter.typeError(
4006+
7775_error,
4007+
path->location(),
4008+
"Only pure functions can be used to define operators."
4009+
);
4010+
40044011
bool identicalFirstTwoParameters = (parameterCount < 2 || *parameterTypes.at(0) == *parameterTypes.at(1));
40054012
bool isUnaryOnlyOperator = (!TokenTraits::isBinaryOp(*operator_) && TokenTraits::isUnaryOp(*operator_));
40064013
bool isBinaryOnlyOperator =

libsolidity/ast/AST.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ class InheritanceSpecifier: public ASTNode
653653
* all functions, and this is checked at the point of the using statement. For versions 1 and
654654
* 2, this check is only done when a function is called.
655655
*
656-
* For version 4, T has to be user-defined value type.
656+
* For version 4, T has to be user-defined value type and the function must be pure.
657657
* All parameters and return value of all the functions have to be of type T.
658658
* This version can be combined with version 3 - a single directive may attach functions to the
659659
* type and define operators on it at the same time.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,4 @@ contract C {
2121
}
2222
}
2323
// ----
24-
// test() -> 3
25-
// gas legacy: 119695
24+
// TypeError 7775: (27-30): Only pure functions can be used to define operators.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using {add as +, sub as -, mul as *} for A;
2+
3+
function add(A, A) view returns (A) {}
4+
function sub(A, A) returns (A) {}
5+
function mul(A, A) payable returns (A) {}
6+
7+
type A is address payable;
8+
// ----
9+
// TypeError 7775: (7-10): Only pure functions can be used to define operators.
10+
// TypeError 7775: (17-20): Only pure functions can be used to define operators.
11+
// TypeError 7775: (27-30): Only pure functions can be used to define operators.
12+
// TypeError 9559: (118-159): Free functions cannot be payable.

test/libsolidity/syntaxTests/viewPureChecker/user_operator_with_view_modifier.sol

Lines changed: 0 additions & 17 deletions
This file was deleted.

test/libsolidity/syntaxTests/viewPureChecker/user_operator_with_view_modifier_can_be_restricted.sol

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)