mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #2758 from ethereum/warnShift
Warn about shift of literals.
This commit is contained in:
commit
4449820be3
@ -7,6 +7,7 @@ Features:
|
||||
* Static Analyzer: Warn about large storage structures.
|
||||
* Metadata: Store experimental flag in metadata CBOR.
|
||||
* Type Checker: More detailed error message for invalid overrides.
|
||||
* Type Checker: Warn about shifting a literal.
|
||||
|
||||
Bugfixes:
|
||||
* Parser: Enforce commas between array and tuple elements.
|
||||
|
@ -1305,8 +1305,9 @@ void TypeChecker::endVisit(BinaryOperation const& _operation)
|
||||
_operation.leftExpression().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 (
|
||||
leftType->category() == Type::Category::RationalNumber &&
|
||||
rightType->category() != Type::Category::RationalNumber
|
||||
@ -1320,7 +1321,7 @@ void TypeChecker::endVisit(BinaryOperation const& _operation)
|
||||
))
|
||||
m_errorReporter.warning(
|
||||
_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 "
|
||||
"expected type."
|
||||
);
|
||||
|
@ -1765,6 +1765,44 @@ BOOST_AUTO_TEST_CASE(exp_warn_literal_base)
|
||||
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)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user