Reduce the types of errors outputted by ConstantEvaluator

This commit is contained in:
Alex Beregszaszi 2017-11-22 12:41:33 +00:00
parent 5226d54ed1
commit 7ff9a85592
2 changed files with 10 additions and 12 deletions

View File

@ -33,7 +33,7 @@ void ConstantEvaluator::endVisit(UnaryOperation const& _operation)
{ {
TypePointer const& subType = _operation.subExpression().annotation().type; TypePointer const& subType = _operation.subExpression().annotation().type;
if (!dynamic_cast<RationalNumberType const*>(subType.get())) if (!dynamic_cast<RationalNumberType const*>(subType.get()))
m_errorReporter.fatalTypeError(_operation.subExpression().location(), "Invalid constant expression."); return;
TypePointer t = subType->unaryOperatorResult(_operation.getOperator()); TypePointer t = subType->unaryOperatorResult(_operation.getOperator());
_operation.annotation().type = t; _operation.annotation().type = t;
} }
@ -44,9 +44,9 @@ void ConstantEvaluator::endVisit(BinaryOperation const& _operation)
TypePointer const& leftType = _operation.leftExpression().annotation().type; TypePointer const& leftType = _operation.leftExpression().annotation().type;
TypePointer const& rightType = _operation.rightExpression().annotation().type; TypePointer const& rightType = _operation.rightExpression().annotation().type;
if (!dynamic_cast<RationalNumberType const*>(leftType.get())) if (!dynamic_cast<RationalNumberType const*>(leftType.get()))
m_errorReporter.fatalTypeError(_operation.leftExpression().location(), "Invalid constant expression."); return;
if (!dynamic_cast<RationalNumberType const*>(rightType.get())) if (!dynamic_cast<RationalNumberType const*>(rightType.get()))
m_errorReporter.fatalTypeError(_operation.rightExpression().location(), "Invalid constant expression."); return;
TypePointer commonType = leftType->binaryOperatorResult(_operation.getOperator(), rightType); TypePointer commonType = leftType->binaryOperatorResult(_operation.getOperator(), rightType);
if (!commonType) if (!commonType)
{ {
@ -71,8 +71,6 @@ void ConstantEvaluator::endVisit(BinaryOperation const& _operation)
void ConstantEvaluator::endVisit(Literal const& _literal) void ConstantEvaluator::endVisit(Literal const& _literal)
{ {
_literal.annotation().type = Type::forLiteral(_literal); _literal.annotation().type = Type::forLiteral(_literal);
if (!_literal.annotation().type)
m_errorReporter.fatalTypeError(_literal.location(), "Invalid literal value.");
} }
void ConstantEvaluator::endVisit(Identifier const& _identifier) void ConstantEvaluator::endVisit(Identifier const& _identifier)
@ -81,11 +79,11 @@ void ConstantEvaluator::endVisit(Identifier const& _identifier)
if (!variableDeclaration) if (!variableDeclaration)
return; return;
if (!variableDeclaration->isConstant()) if (!variableDeclaration->isConstant())
m_errorReporter.fatalTypeError(_identifier.location(), "Identifier must be declared constant."); return;
ASTPointer<Expression> value = variableDeclaration->value(); ASTPointer<Expression> const& value = variableDeclaration->value();
if (!value) if (!value)
m_errorReporter.fatalTypeError(_identifier.location(), "Constant identifier declaration must have a constant value."); return;
if (!value->annotation().type) if (!value->annotation().type)
{ {

View File

@ -7419,25 +7419,25 @@ BOOST_AUTO_TEST_CASE(array_length_invalid_expression)
uint[-true] ids; uint[-true] ids;
} }
)"; )";
CHECK_ERROR(text, TypeError, "Invalid constant expression."); CHECK_ERROR(text, TypeError, "Invalid array length, expected integer literal or constant expression.");
text = R"( text = R"(
contract C { contract C {
uint[true/1] ids; uint[true/1] ids;
} }
)"; )";
CHECK_ERROR(text, TypeError, "Invalid constant expression."); CHECK_ERROR(text, TypeError, "Invalid array length, expected integer literal or constant expression.");
text = R"( text = R"(
contract C { contract C {
uint[1/true] ids; uint[1/true] ids;
} }
)"; )";
CHECK_ERROR(text, TypeError, "Invalid constant expression."); CHECK_ERROR(text, TypeError, "Invalid array length, expected integer literal or constant expression.");
text = R"( text = R"(
contract C { contract C {
uint[1.111111E1111111111111] ids; uint[1.111111E1111111111111] ids;
} }
)"; )";
CHECK_ERROR(text, TypeError, "Invalid literal value."); CHECK_ERROR(text, TypeError, "Invalid array length, expected integer literal or constant expression.");
text = R"( text = R"(
contract C { contract C {
uint[3/0] ids; uint[3/0] ids;