From 1084a34f286e4fed8c04e69d694bb8d960a8f82e Mon Sep 17 00:00:00 2001 From: wechman Date: Thu, 14 Jul 2022 09:41:51 +0200 Subject: [PATCH] Add control flow tests for "and" and "or" operators --- libsolidity/analysis/ControlFlowBuilder.cpp | 3 +- .../and_operator_err.sol | 11 +++++++ .../localStorageVariables/or_operator_err.sol | 11 +++++++ .../binary_operator.sol | 23 +++++++++----- .../custom/operators_with_pure_modifier.sol | 26 ---------------- .../custom/operators_with_view_modifier.sol | 30 ------------------- .../user_operator_with_pure_modifier.sol | 21 +++++++++++++ .../user_operator_with_view_modifier.sol | 30 +++++++++++++++++++ 8 files changed, 90 insertions(+), 65 deletions(-) create mode 100644 test/libsolidity/syntaxTests/controlFlow/localStorageVariables/and_operator_err.sol create mode 100644 test/libsolidity/syntaxTests/controlFlow/localStorageVariables/or_operator_err.sol delete mode 100644 test/libsolidity/syntaxTests/operators/custom/operators_with_pure_modifier.sol delete mode 100644 test/libsolidity/syntaxTests/operators/custom/operators_with_view_modifier.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/user_operator_with_pure_modifier.sol create mode 100644 test/libsolidity/syntaxTests/viewPureChecker/user_operator_with_view_modifier.sol diff --git a/libsolidity/analysis/ControlFlowBuilder.cpp b/libsolidity/analysis/ControlFlowBuilder.cpp index a1d10cfe8..7b4524e98 100644 --- a/libsolidity/analysis/ControlFlowBuilder.cpp +++ b/libsolidity/analysis/ControlFlowBuilder.cpp @@ -57,6 +57,7 @@ unique_ptr ControlFlowBuilder::createFunctionFlow( bool ControlFlowBuilder::visit(BinaryOperation const& _operation) { solAssert(!!m_currentNode, ""); + visitNode(_operation); switch (_operation.getOperator()) { @@ -65,7 +66,6 @@ bool ControlFlowBuilder::visit(BinaryOperation const& _operation) { visitNode(_operation); solAssert(!_operation.annotation().userDefinedFunction); - visitNode(_operation); appendControlFlow(_operation.leftExpression()); auto nodes = splitFlow<2>(); @@ -76,7 +76,6 @@ bool ControlFlowBuilder::visit(BinaryOperation const& _operation) } default: { - visitNode(_operation); if (_operation.annotation().userDefinedFunction) { visitNode(_operation); diff --git a/test/libsolidity/syntaxTests/controlFlow/localStorageVariables/and_operator_err.sol b/test/libsolidity/syntaxTests/controlFlow/localStorageVariables/and_operator_err.sol new file mode 100644 index 000000000..61db13507 --- /dev/null +++ b/test/libsolidity/syntaxTests/controlFlow/localStorageVariables/and_operator_err.sol @@ -0,0 +1,11 @@ +struct S { bool f; } + +contract C { + function f() public view { + S storage s; + s.f && true; + } +} + +// ---- +// TypeError 3464: (95-96): This variable is of storage pointer type and can be accessed without prior assignment, which would lead to undefined behaviour. diff --git a/test/libsolidity/syntaxTests/controlFlow/localStorageVariables/or_operator_err.sol b/test/libsolidity/syntaxTests/controlFlow/localStorageVariables/or_operator_err.sol new file mode 100644 index 000000000..77f302d64 --- /dev/null +++ b/test/libsolidity/syntaxTests/controlFlow/localStorageVariables/or_operator_err.sol @@ -0,0 +1,11 @@ +struct S { bool f; } + +contract C { + function f() public view { + S storage s; + s.f || true; + } +} + +// ---- +// TypeError 3464: (95-96): This variable is of storage pointer type and can be accessed without prior assignment, which would lead to undefined behaviour. diff --git a/test/libsolidity/syntaxTests/controlFlow/userDefinedTypeOperators/binary_operator.sol b/test/libsolidity/syntaxTests/controlFlow/userDefinedTypeOperators/binary_operator.sol index ee323cbe8..0e3692946 100644 --- a/test/libsolidity/syntaxTests/controlFlow/userDefinedTypeOperators/binary_operator.sol +++ b/test/libsolidity/syntaxTests/controlFlow/userDefinedTypeOperators/binary_operator.sol @@ -2,16 +2,25 @@ struct S { bool f; } using {add as +} for S; -function add(S storage x, S storage) pure returns (S storage) { return x; } +function add(S storage _s, S storage) pure returns (S storage) { + return _s; + _s.f = true; +} contract C { - S s; - function f() public { - S storage x = s; - S storage y; - x + y; + function get() private returns (S storage) { + S storage s; + return s; } + + function f() public { + S storage s; + get() + s; + } + } // ---- -// TypeError 3464: (230-231): This variable is of storage pointer type and can be accessed without prior assignment, which would lead to undefined behaviour. +// Warning 5740: (131-142): Unreachable code. +// TypeError 3464: (238-239): This variable is of storage pointer type and can be accessed without prior assignment, which would lead to undefined behaviour. +// TypeError 3464: (311-312): This variable is of storage pointer type and can be accessed without prior assignment, which would lead to undefined behaviour. diff --git a/test/libsolidity/syntaxTests/operators/custom/operators_with_pure_modifier.sol b/test/libsolidity/syntaxTests/operators/custom/operators_with_pure_modifier.sol deleted file mode 100644 index 6a35cd78a..000000000 --- a/test/libsolidity/syntaxTests/operators/custom/operators_with_pure_modifier.sol +++ /dev/null @@ -1,26 +0,0 @@ -type Int is uint8; - -using { - add as +, - sub as -, - mul as * -} for Int; - -function f_view() view {} - -function add(Int, Int) pure returns (Int) { - return Int.wrap(0); -} - -function sub(Int, Int) returns (Int) { - return Int.wrap(0); -} - -function mul(Int, Int) pure returns (Int) { - f_view(); - return Int.wrap(0); -} - -// ---- -// Warning 2018: (179-243): Function state mutability can be restricted to pure -// TypeError 2527: (293-301): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view". diff --git a/test/libsolidity/syntaxTests/operators/custom/operators_with_view_modifier.sol b/test/libsolidity/syntaxTests/operators/custom/operators_with_view_modifier.sol deleted file mode 100644 index 49493eda5..000000000 --- a/test/libsolidity/syntaxTests/operators/custom/operators_with_view_modifier.sol +++ /dev/null @@ -1,30 +0,0 @@ -type Int is uint8; - -using { - add as +, - sub as -, - mul as * -} for Int; - - -function f_view() view {} -function f() {} - -function add(Int, Int) view returns (Int) { - f_view(); - return Int.wrap(0); -} - -function sub(Int, Int) returns (Int) { - f_view(); - return Int.wrap(0); -} - -function mul(Int, Int) view returns (Int) { - f(); - return Int.wrap(0); -} - -// ---- -// Warning 2018: (210-288): Function state mutability can be restricted to view -// TypeError 8961: (338-341): Function cannot be declared as view because this expression (potentially) modifies the state. diff --git a/test/libsolidity/syntaxTests/viewPureChecker/user_operator_with_pure_modifier.sol b/test/libsolidity/syntaxTests/viewPureChecker/user_operator_with_pure_modifier.sol new file mode 100644 index 000000000..405881f47 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/user_operator_with_pure_modifier.sol @@ -0,0 +1,21 @@ +type Int is uint8; + +using { + add as + +} for Int; + +function add(Int, Int) pure returns (Int) { + return Int.wrap(0); +} + +function f() pure { + Int.wrap(0) + Int.wrap(1); +} + +function g() { + Int.wrap(0) + Int.wrap(1); +} + + +// ---- +// Warning 2018: (178-225): Function state mutability can be restricted to pure diff --git a/test/libsolidity/syntaxTests/viewPureChecker/user_operator_with_view_modifier.sol b/test/libsolidity/syntaxTests/viewPureChecker/user_operator_with_view_modifier.sol new file mode 100644 index 000000000..176889503 --- /dev/null +++ b/test/libsolidity/syntaxTests/viewPureChecker/user_operator_with_view_modifier.sol @@ -0,0 +1,30 @@ +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".