Move error message somewhere else.

This commit is contained in:
chriseth 2021-08-17 16:27:29 +02:00
parent 83ed624e9d
commit 5ecd2f2287
2 changed files with 37 additions and 72 deletions

View File

@ -1152,7 +1152,8 @@ void TypeChecker::endVisit(Return const& _return)
"Return argument type " + "Return argument type " +
type(*_return.expression())->toString() + type(*_return.expression())->toString() +
" is not implicitly convertible to expected type " + " is not implicitly convertible to expected type " +
TupleType(returnTypes).toString(false) + ".", TupleType(returnTypes).toString(false) +
".",
result.message() result.message()
); );
} }
@ -1170,7 +1171,8 @@ void TypeChecker::endVisit(Return const& _return)
"Return argument type " + "Return argument type " +
type(*_return.expression())->toString() + type(*_return.expression())->toString() +
" is not implicitly convertible to expected type (type of first return variable) " + " is not implicitly convertible to expected type (type of first return variable) " +
expected->toString() + ".", expected->toString() +
".",
result.message() result.message()
); );
} }
@ -1264,41 +1266,16 @@ bool TypeChecker::visit(VariableDeclarationStatement const& _statement)
var.accept(*this); var.accept(*this);
BoolResult result = valueComponentType->isImplicitlyConvertibleTo(*var.annotation().type); BoolResult result = valueComponentType->isImplicitlyConvertibleTo(*var.annotation().type);
if (!result) if (!result)
{ m_errorReporter.typeErrorConcatenateDescriptions(
auto errorMsg = "Type " + 9574_error,
_statement.location(),
"Type " +
valueComponentType->toString() + valueComponentType->toString() +
" is not implicitly convertible to expected type " + " is not implicitly convertible to expected type " +
var.annotation().type->toString(); var.annotation().type->toString() +
if ( ".",
valueComponentType->category() == Type::Category::RationalNumber && result.message()
dynamic_cast<RationalNumberType const&>(*valueComponentType).isFractional() && );
valueComponentType->mobileType()
)
{
if (var.annotation().type->operator==(*valueComponentType->mobileType()))
m_errorReporter.typeError(
5107_error,
_statement.location(),
errorMsg + ", but it can be explicitly converted."
);
else
m_errorReporter.typeError(
4486_error,
_statement.location(),
errorMsg +
". Try converting to type " +
valueComponentType->mobileType()->toString() +
" or use an explicit conversion."
);
}
else
m_errorReporter.typeErrorConcatenateDescriptions(
9574_error,
_statement.location(),
errorMsg + ".",
result.message()
);
}
} }
if (valueTypes.size() != variables.size()) if (valueTypes.size() != variables.size())
@ -3449,40 +3426,16 @@ bool TypeChecker::expectType(Expression const& _expression, Type const& _expecte
BoolResult result = type(_expression)->isImplicitlyConvertibleTo(_expectedType); BoolResult result = type(_expression)->isImplicitlyConvertibleTo(_expectedType);
if (!result) if (!result)
{ {
auto errorMsg = "Type " + m_errorReporter.typeErrorConcatenateDescriptions(
7407_error,
_expression.location(),
"Type " +
type(_expression)->toString() + type(_expression)->toString() +
" is not implicitly convertible to expected type " + " is not implicitly convertible to expected type " +
_expectedType.toString(); _expectedType.toString() +
if ( ".",
type(_expression)->category() == Type::Category::RationalNumber && result.message()
dynamic_cast<RationalNumberType const*>(type(_expression))->isFractional() && );
type(_expression)->mobileType()
)
{
if (_expectedType.operator==(*type(_expression)->mobileType()))
m_errorReporter.typeError(
4426_error,
_expression.location(),
errorMsg + ", but it can be explicitly converted."
);
else
m_errorReporter.typeErrorConcatenateDescriptions(
2326_error,
_expression.location(),
errorMsg +
". Try converting to type " +
type(_expression)->mobileType()->toString() +
" or use an explicit conversion.",
result.message()
);
}
else
m_errorReporter.typeErrorConcatenateDescriptions(
7407_error,
_expression.location(),
errorMsg + ".",
result.message()
);
return false; return false;
} }
return true; return true;

View File

@ -954,7 +954,7 @@ BoolResult RationalNumberType::isImplicitlyConvertibleTo(Type const& _convertTo)
case Category::Integer: case Category::Integer:
{ {
if (isFractional()) if (isFractional())
return false; return BoolResult::err("Rational number is fractional, use an explicit conversion instead.");
IntegerType const& targetType = dynamic_cast<IntegerType const&>(_convertTo); IntegerType const& targetType = dynamic_cast<IntegerType const&>(_convertTo);
return fitsIntegerType(m_value.numerator(), targetType); return fitsIntegerType(m_value.numerator(), targetType);
} }
@ -963,14 +963,26 @@ BoolResult RationalNumberType::isImplicitlyConvertibleTo(Type const& _convertTo)
FixedPointType const& targetType = dynamic_cast<FixedPointType const&>(_convertTo); FixedPointType const& targetType = dynamic_cast<FixedPointType const&>(_convertTo);
// Store a negative number into an unsigned. // Store a negative number into an unsigned.
if (isNegative() && !targetType.isSigned()) if (isNegative() && !targetType.isSigned())
return false; return BoolResult::err("Rational number is negative, use a signed fixed point type instead.");
if (!isFractional()) if (!isFractional())
return (targetType.minIntegerValue() <= m_value) && (m_value <= targetType.maxIntegerValue()); {
if (m_value < targetType.minIntegerValue())
return BoolResult::err("Number is too small for type.");
else if (m_value > targetType.maxIntegerValue())
return BoolResult::err("Number is too large for type.");
else
return true;
}
rational value = m_value * pow(bigint(10), targetType.fractionalDigits()); rational value = m_value * pow(bigint(10), targetType.fractionalDigits());
// Need explicit conversion since truncation will occur. // Need explicit conversion since truncation will occur.
if (value.denominator() != 1) if (value.denominator() != 1)
return false; return BoolResult::err("Conversion incurs precision loss. Use an explicit conversion instead.");
return fitsIntoBits(value.numerator(), targetType.numBits(), targetType.isSigned()); if (m_value < targetType.minValue())
return BoolResult::err("Number is too small for type.");
else if (m_value > targetType.maxValue())
return BoolResult::err("Number is too large for type.");
solAssert(fitsIntoBits(value.numerator(), targetType.numBits(), targetType.isSigned()), "");
return true;
} }
case Category::FixedBytes: case Category::FixedBytes:
return (m_value == rational(0)) || (m_compatibleBytesType && *m_compatibleBytesType == _convertTo); return (m_value == rational(0)) || (m_compatibleBytesType && *m_compatibleBytesType == _convertTo);