aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2018-05-02 21:56:59 +0800
committerGitHub <noreply@github.com>2018-05-02 21:56:59 +0800
commit8debded7431cb852bd66edc8ebb615cb39f95231 (patch)
tree8cd4db92d6d7b5aa7c31995ff9a5026c4e9312a3
parent42289b642ff12d9275dedf10c257cfec8295feed (diff)
downloaddexon-solidity-8debded7431cb852bd66edc8ebb615cb39f95231.tar.gz
dexon-solidity-8debded7431cb852bd66edc8ebb615cb39f95231.tar.zst
dexon-solidity-8debded7431cb852bd66edc8ebb615cb39f95231.zip
Revert "BREAKING: Bool variables should not allow arithmetic comparison"
-rw-r--r--Changelog.md5
-rw-r--r--libsolidity/ast/Types.cpp2
-rw-r--r--libsolidity/formal/SMTChecker.cpp6
-rw-r--r--test/libsolidity/SMTChecker.cpp29
-rw-r--r--test/libsolidity/syntaxTests/types/bool_ops.sol53
5 files changed, 35 insertions, 60 deletions
diff --git a/Changelog.md b/Changelog.md
index 0e8b26ea..1cb96833 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -1,8 +1,3 @@
-### 0.5.0 (unreleased)
-Features:
- * Type Checker: Disallow arithmetic operations for Boolean variables.
-
-
### 0.4.24 (unreleased)
Features:
diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp
index f4198016..11d7160c 100644
--- a/libsolidity/ast/Types.cpp
+++ b/libsolidity/ast/Types.cpp
@@ -1359,7 +1359,7 @@ TypePointer BoolType::binaryOperatorResult(Token::Value _operator, TypePointer c
{
if (category() != _other->category())
return TypePointer();
- if (_operator == Token::Equal || _operator == Token::NotEqual || _operator == Token::And || _operator == Token::Or)
+ if (Token::isCompareOp(_operator) || _operator == Token::And || _operator == Token::Or)
return _other;
else
return TypePointer();
diff --git a/libsolidity/formal/SMTChecker.cpp b/libsolidity/formal/SMTChecker.cpp
index 7facdf92..c4dee22d 100644
--- a/libsolidity/formal/SMTChecker.cpp
+++ b/libsolidity/formal/SMTChecker.cpp
@@ -472,7 +472,11 @@ void SMTChecker::compareOperation(BinaryOperation const& _op)
solUnimplementedAssert(SSAVariable::isBool(_op.annotation().commonType->category()), "Operation not yet supported");
value = make_shared<smt::Expression>(
op == Token::Equal ? (left == right) :
- /*op == Token::NotEqual*/ (left != right)
+ op == Token::NotEqual ? (left != right) :
+ op == Token::LessThan ? (!left && right) :
+ op == Token::LessThanOrEqual ? (!left || right) :
+ op == Token::GreaterThan ? (left && !right) :
+ /*op == Token::GreaterThanOrEqual*/ (left || !right)
);
}
// TODO: check that other values for op are not possible.
diff --git a/test/libsolidity/SMTChecker.cpp b/test/libsolidity/SMTChecker.cpp
index 10f64a7f..beb933a4 100644
--- a/test/libsolidity/SMTChecker.cpp
+++ b/test/libsolidity/SMTChecker.cpp
@@ -388,6 +388,35 @@ BOOST_AUTO_TEST_CASE(bool_simple)
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
+ text = R"(
+ contract C {
+ function f(bool x) public pure {
+ bool y;
+ assert(x <= y);
+ }
+ }
+ )";
+ CHECK_WARNING(text, "Assertion violation happens here");
+ text = R"(
+ contract C {
+ function f(bool x) public pure {
+ bool y;
+ assert(x >= y);
+ }
+ }
+ )";
+ CHECK_SUCCESS_NO_WARNINGS(text);
+ text = R"(
+ contract C {
+ function f(bool x) public pure {
+ require(x);
+ bool y;
+ assert(x > y);
+ assert(y < x);
+ }
+ }
+ )";
+ CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_CASE(bool_int_mixed)
diff --git a/test/libsolidity/syntaxTests/types/bool_ops.sol b/test/libsolidity/syntaxTests/types/bool_ops.sol
deleted file mode 100644
index 91033906..00000000
--- a/test/libsolidity/syntaxTests/types/bool_ops.sol
+++ /dev/null
@@ -1,53 +0,0 @@
-contract C {
- function f(bool a, bool b) public pure {
- bool c;
- // OK
- c = !a;
- c = !b;
- c = a == b;
- c = a != b;
- c = a || b;
- c = a && b;
-
- // Not OK
- c = a > b;
- c = a < b;
- c = a >= b;
- c = a <= b;
- c = a & b;
- c = a | b;
- c = a ^ b;
- c = ~a;
- c = ~b;
- c = a + b;
- c = a - b;
- c = -a;
- c = -b;
- c = a * b;
- c = a / b;
- c = a ** b;
- c = a % b;
- c = a << b;
- c = a >> b;
- }
-}
-// ----
-// TypeError: (231-236): Operator > not compatible with types bool and bool
-// TypeError: (250-255): Operator < not compatible with types bool and bool
-// TypeError: (269-275): Operator >= not compatible with types bool and bool
-// TypeError: (289-295): Operator <= not compatible with types bool and bool
-// TypeError: (309-314): Operator & not compatible with types bool and bool
-// TypeError: (328-333): Operator | not compatible with types bool and bool
-// TypeError: (347-352): Operator ^ not compatible with types bool and bool
-// TypeError: (366-368): Unary operator ~ cannot be applied to type bool
-// TypeError: (382-384): Unary operator ~ cannot be applied to type bool
-// TypeError: (398-403): Operator + not compatible with types bool and bool
-// TypeError: (417-422): Operator - not compatible with types bool and bool
-// TypeError: (436-438): Unary operator - cannot be applied to type bool
-// TypeError: (452-454): Unary operator - cannot be applied to type bool
-// TypeError: (468-473): Operator * not compatible with types bool and bool
-// TypeError: (487-492): Operator / not compatible with types bool and bool
-// TypeError: (506-512): Operator ** not compatible with types bool and bool
-// TypeError: (526-531): Operator % not compatible with types bool and bool
-// TypeError: (545-551): Operator << not compatible with types bool and bool
-// TypeError: (565-571): Operator >> not compatible with types bool and bool