Merge pull request #2758 from ethereum/warnShift

Warn about shift of literals.
This commit is contained in:
Alex Beregszaszi 2017-08-16 16:54:06 +01:00 committed by GitHub
commit 4449820be3
3 changed files with 42 additions and 2 deletions

View File

@ -7,6 +7,7 @@ Features:
* Static Analyzer: Warn about large storage structures. * Static Analyzer: Warn about large storage structures.
* Metadata: Store experimental flag in metadata CBOR. * Metadata: Store experimental flag in metadata CBOR.
* Type Checker: More detailed error message for invalid overrides. * Type Checker: More detailed error message for invalid overrides.
* Type Checker: Warn about shifting a literal.
Bugfixes: Bugfixes:
* Parser: Enforce commas between array and tuple elements. * Parser: Enforce commas between array and tuple elements.

View File

@ -1305,8 +1305,9 @@ void TypeChecker::endVisit(BinaryOperation const& _operation)
_operation.leftExpression().annotation().isPure && _operation.leftExpression().annotation().isPure &&
_operation.rightExpression().annotation().isPure; _operation.rightExpression().annotation().isPure;
if (_operation.getOperator() == Token::Exp) if (_operation.getOperator() == Token::Exp || _operation.getOperator() == Token::SHL)
{ {
string operation = _operation.getOperator() == Token::Exp ? "exponentiation" : "shift";
if ( if (
leftType->category() == Type::Category::RationalNumber && leftType->category() == Type::Category::RationalNumber &&
rightType->category() != Type::Category::RationalNumber rightType->category() != Type::Category::RationalNumber
@ -1320,7 +1321,7 @@ void TypeChecker::endVisit(BinaryOperation const& _operation)
)) ))
m_errorReporter.warning( m_errorReporter.warning(
_operation.location(), _operation.location(),
"Result of exponentiation has type " + commonType->toString() + " and thus " "Result of " + operation + " has type " + commonType->toString() + " and thus "
"might overflow. Silence this warning by converting the literal to the " "might overflow. Silence this warning by converting the literal to the "
"expected type." "expected type."
); );

View File

@ -1765,6 +1765,44 @@ BOOST_AUTO_TEST_CASE(exp_warn_literal_base)
CHECK_SUCCESS(sourceCode); CHECK_SUCCESS(sourceCode);
} }
BOOST_AUTO_TEST_CASE(shift_warn_literal_base)
{
char const* sourceCode = R"(
contract test {
function f() returns(uint) {
uint8 x = 100;
return 10 << x;
}
}
)";
CHECK_WARNING(sourceCode, "might overflow");
sourceCode = R"(
contract test {
function f() returns(uint) {
uint8 x = 100;
return uint8(10) << x;
}
}
)";
CHECK_SUCCESS(sourceCode);
sourceCode = R"(
contract test {
function f() returns(uint) {
return 2 << 80;
}
}
)";
CHECK_SUCCESS(sourceCode);
sourceCode = R"(
contract test {
function f() returns(uint) {
uint8 x = 100;
return 10 >> x;
}
}
)";
CHECK_SUCCESS(sourceCode);
}
BOOST_AUTO_TEST_CASE(warn_var_from_zero) BOOST_AUTO_TEST_CASE(warn_var_from_zero)
{ {