mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
- conversion of positive literals to signed int
- tests
This commit is contained in:
parent
b7e5c1d9d2
commit
382ac85aa5
12
AST.cpp
12
AST.cpp
@ -686,9 +686,15 @@ 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(createTypeError("Type " + type.toString() +
|
BOOST_THROW_EXCEPTION(
|
||||||
" not implicitly convertible to expected type "
|
createTypeError(
|
||||||
+ _expectedType.toString() + "."));
|
"Type " +
|
||||||
|
type.toString() +
|
||||||
|
" is not implicitly convertible to expected type " +
|
||||||
|
_expectedType.toString() +
|
||||||
|
"."
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Expression::requireLValue()
|
void Expression::requireLValue()
|
||||||
|
32
Types.cpp
32
Types.cpp
@ -361,17 +361,28 @@ IntegerConstantType::IntegerConstantType(Literal const& _literal)
|
|||||||
|
|
||||||
bool IntegerConstantType::isImplicitlyConvertibleTo(Type const& _convertTo) const
|
bool IntegerConstantType::isImplicitlyConvertibleTo(Type const& _convertTo) const
|
||||||
{
|
{
|
||||||
shared_ptr<IntegerType const> integerType = getIntegerType();
|
if (IntegerType const* integerType = dynamic_cast<IntegerType const*>(&_convertTo))
|
||||||
if (!integerType)
|
{
|
||||||
|
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 false;
|
||||||
|
}
|
||||||
|
else
|
||||||
if (_convertTo.getCategory() == Category::FixedBytes)
|
if (_convertTo.getCategory() == Category::FixedBytes)
|
||||||
{
|
{
|
||||||
FixedBytesType const& convertTo = dynamic_cast<FixedBytesType const&>(_convertTo);
|
FixedBytesType const& fixedBytes = dynamic_cast<FixedBytesType const&>(_convertTo);
|
||||||
return convertTo.getNumBytes() * 8 >= integerType->getNumBits();
|
return fixedBytes.getNumBytes() * 8 >= getIntegerType()->getNumBits();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
return integerType->isImplicitlyConvertibleTo(_convertTo);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IntegerConstantType::isExplicitlyConvertibleTo(Type const& _convertTo) const
|
bool IntegerConstantType::isExplicitlyConvertibleTo(Type const& _convertTo) const
|
||||||
@ -514,9 +525,10 @@ shared_ptr<IntegerType const> IntegerConstantType::getIntegerType() const
|
|||||||
if (value > u256(-1))
|
if (value > u256(-1))
|
||||||
return shared_ptr<IntegerType const>();
|
return shared_ptr<IntegerType const>();
|
||||||
else
|
else
|
||||||
return make_shared<IntegerType>(max(bytesRequired(value), 1u) * 8,
|
return make_shared<IntegerType>(
|
||||||
negative ? IntegerType::Modifier::Signed
|
max(bytesRequired(value), 1u) * 8,
|
||||||
: IntegerType::Modifier::Unsigned);
|
negative ? IntegerType::Modifier::Signed : IntegerType::Modifier::Unsigned
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
shared_ptr<FixedBytesType> FixedBytesType::smallestTypeForLiteral(string const& _literal)
|
shared_ptr<FixedBytesType> FixedBytesType::smallestTypeForLiteral(string const& _literal)
|
||||||
|
Loading…
Reference in New Issue
Block a user