Browse Source

- conversion of positive literals to signed int

- tests
cl-refactor
Liana Husikyan 10 years ago
parent
commit
4757651b64
  1. 12
      libsolidity/AST.cpp
  2. 36
      libsolidity/Types.cpp
  3. 4
      test/libsolidity/SolidityEndToEndTest.cpp
  4. 40
      test/libsolidity/SolidityNameAndTypeResolution.cpp

12
libsolidity/AST.cpp

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

36
libsolidity/Types.cpp

@ -361,17 +361,28 @@ IntegerConstantType::IntegerConstantType(Literal const& _literal)
bool IntegerConstantType::isImplicitlyConvertibleTo(Type const& _convertTo) const
{
shared_ptr<IntegerType const> integerType = getIntegerType();
if (!integerType)
return false;
if (_convertTo.getCategory() == Category::FixedBytes)
if (IntegerType const* integerType = dynamic_cast<IntegerType const*>(&_convertTo))
{
FixedBytesType const& convertTo = dynamic_cast<FixedBytesType const&>(_convertTo);
return convertTo.getNumBytes() * 8 >= integerType->getNumBits();
if (m_value == 0)
return true;
int forSignBit = (integerType->isSigned() ? 1 : 0);
if (m_value > 0)
{
if (m_value <= (u256(-1) >> (256 - integerType->getNumBits() + forSignBit)))
return true;
}
else if (-m_value <= (u256(1) << (integerType->getNumBits() - forSignBit)))
return true;
return false;
}
return integerType->isImplicitlyConvertibleTo(_convertTo);
else
if (_convertTo.getCategory() == Category::FixedBytes)
{
FixedBytesType const& fixedBytes = dynamic_cast<FixedBytesType const&>(_convertTo);
return fixedBytes.getNumBytes() * 8 >= getIntegerType()->getNumBits();
}
else
return false;
}
bool IntegerConstantType::isExplicitlyConvertibleTo(Type const& _convertTo) const
@ -514,9 +525,10 @@ shared_ptr<IntegerType const> IntegerConstantType::getIntegerType() const
if (value > u256(-1))
return shared_ptr<IntegerType const>();
else
return make_shared<IntegerType>(max(bytesRequired(value), 1u) * 8,
negative ? IntegerType::Modifier::Signed
: IntegerType::Modifier::Unsigned);
return make_shared<IntegerType>(
max(bytesRequired(value), 1u) * 8,
negative ? IntegerType::Modifier::Signed : IntegerType::Modifier::Unsigned
);
}
shared_ptr<FixedBytesType> FixedBytesType::smallestTypeForLiteral(string const& _literal)

4
test/libsolidity/SolidityEndToEndTest.cpp

@ -4177,10 +4177,14 @@ BOOST_AUTO_TEST_CASE(positive_integers_to_signed)
char const* sourceCode = R"(
contract test {
int8 public x = 2;
int8 public y = 127;
int16 public q = 250;
}
)";
compileAndRun(sourceCode, 0, "test");
BOOST_CHECK(callContractFunction("x()") == encodeArgs(2));
BOOST_CHECK(callContractFunction("y()") == encodeArgs(127));
BOOST_CHECK(callContractFunction("q()") == encodeArgs(250));
}
BOOST_AUTO_TEST_SUITE_END()

40
test/libsolidity/SolidityNameAndTypeResolution.cpp

@ -1816,6 +1816,46 @@ BOOST_AUTO_TEST_CASE(string_length)
BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
}
BOOST_AUTO_TEST_CASE(negative_integers_to_signed_out_of_bound)
{
char const* sourceCode = R"(
contract test {
int8 public i = -129;
}
)";
BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
}
BOOST_AUTO_TEST_CASE(negative_integers_to_signed_min)
{
char const* sourceCode = R"(
contract test {
int8 public i = -128;
}
)";
BOOST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCode));
}
BOOST_AUTO_TEST_CASE(positive_integers_to_signed_out_of_bound)
{
char const* sourceCode = R"(
contract test {
int8 public j = 128;
}
)";
BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
}
BOOST_AUTO_TEST_CASE(positive_integers_to_signed_out_of_bound_max)
{
char const* sourceCode = R"(
contract test {
int8 public j = 127;
}
)";
BOOST_CHECK_NO_THROW(parseTextAndResolveNames(sourceCode));
}
BOOST_AUTO_TEST_SUITE_END()
}

Loading…
Cancel
Save