Browse Source

- style fixes

- added test for uint8 = -1 which doesn't fail; todo: fix that
cl-refactor
Liana Husikyan 10 years ago
parent
commit
9da464ee05
  1. 15
      libsolidity/AST.cpp
  2. 11
      libsolidity/Types.cpp
  3. 10
      test/libsolidity/SolidityNameAndTypeResolution.cpp

15
libsolidity/AST.cpp

@ -686,14 +686,13 @@ void Expression::expectType(Type const& _expectedType)
checkTypeRequirements(nullptr); checkTypeRequirements(nullptr);
Type const& type = *getType(); Type const& type = *getType();
if (!type.isImplicitlyConvertibleTo(_expectedType)) if (!type.isImplicitlyConvertibleTo(_expectedType))
BOOST_THROW_EXCEPTION( BOOST_THROW_EXCEPTION(createTypeError(
createTypeError( "Type " +
"Type " + type.toString() +
type.toString() + " is not implicitly convertible to expected type " +
" is not implicitly convertible to expected type " + _expectedType.toString() +
_expectedType.toString() + "."
"." )
)
); );
} }

11
libsolidity/Types.cpp

@ -361,22 +361,21 @@ IntegerConstantType::IntegerConstantType(Literal const& _literal)
bool IntegerConstantType::isImplicitlyConvertibleTo(Type const& _convertTo) const bool IntegerConstantType::isImplicitlyConvertibleTo(Type const& _convertTo) const
{ {
if (IntegerType const* integerType = dynamic_cast<IntegerType const*>(&_convertTo)) if (auto targetType = dynamic_cast<IntegerType const*>(&_convertTo))
{ {
if (m_value == 0) if (m_value == 0)
return true; return true;
int forSignBit = (integerType->isSigned() ? 1 : 0); int forSignBit = (targetType->isSigned() ? 1 : 0);
if (m_value > 0) if (m_value > 0)
{ {
if (m_value <= (u256(-1) >> (256 - integerType->getNumBits() + forSignBit))) if (m_value <= (u256(-1) >> (256 - targetType->getNumBits() + forSignBit)))
return true; return true;
} }
else if (-m_value <= (u256(1) << (integerType->getNumBits() - forSignBit))) else if (-m_value <= (u256(1) << (targetType->getNumBits() - forSignBit)))
return true; return true;
return false; return false;
} }
else else if (_convertTo.getCategory() == Category::FixedBytes)
if (_convertTo.getCategory() == Category::FixedBytes)
{ {
FixedBytesType const& fixedBytes = dynamic_cast<FixedBytesType const&>(_convertTo); FixedBytesType const& fixedBytes = dynamic_cast<FixedBytesType const&>(_convertTo);
return fixedBytes.getNumBytes() * 8 >= getIntegerType()->getNumBits(); return fixedBytes.getNumBytes() * 8 >= getIntegerType()->getNumBits();

10
test/libsolidity/SolidityNameAndTypeResolution.cpp

@ -1856,6 +1856,16 @@ BOOST_AUTO_TEST_CASE(positive_integers_to_signed_out_of_bound_max)
BOOST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCode)); BOOST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCode));
} }
BOOST_AUTO_TEST_CASE(negative_integers_to_unsigned)
{
char const* sourceCode = R"(
contract test {
uint8 public x = -1;
}
)";
BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
} }

Loading…
Cancel
Save