Extract reasonOfFailure to lambda function.

This commit is contained in:
Daniel Kirchner 2019-08-13 18:58:50 +02:00
parent b0a5666b43
commit e545103ec8

View File

@ -2518,27 +2518,17 @@ void TypeChecker::requireLValue(Expression const& _expression)
if (_expression.annotation().isLValue) if (_expression.annotation().isLValue)
return; return;
return m_errorReporter.typeError(_expression.location(), [&]() {
if (_expression.annotation().isConstant) if (_expression.annotation().isConstant)
{ return "Cannot assign to a constant variable.";
m_errorReporter.typeError(_expression.location(), "Cannot assign to a constant variable.");
return;
}
if (auto indexAccess = dynamic_cast<IndexAccess const*>(&_expression)) if (auto indexAccess = dynamic_cast<IndexAccess const*>(&_expression))
{ {
if (auto arrayType = dynamic_cast<ArrayType const*>(type(indexAccess->baseExpression()))) if (type(indexAccess->baseExpression())->category() == Type::Category::FixedBytes)
{ return "Single bytes in fixed bytes arrays cannot be modified.";
else if (auto arrayType = dynamic_cast<ArrayType const*>(type(indexAccess->baseExpression())))
if (arrayType->dataStoredIn(DataLocation::CallData)) if (arrayType->dataStoredIn(DataLocation::CallData))
{ return "Calldata arrays are read-only.";
m_errorReporter.typeError(_expression.location(), "Calldata arrays are read-only.");
return;
}
}
else if (type(indexAccess->baseExpression())->category() == Type::Category::FixedBytes)
{
m_errorReporter.typeError(_expression.location(), "Single bytes in fixed bytes arrays cannot be modified.");
return;
}
} }
if (auto memberAccess = dynamic_cast<MemberAccess const*>(&_expression)) if (auto memberAccess = dynamic_cast<MemberAccess const*>(&_expression))
@ -2546,38 +2536,26 @@ void TypeChecker::requireLValue(Expression const& _expression)
if (auto structType = dynamic_cast<StructType const*>(type(memberAccess->expression()))) if (auto structType = dynamic_cast<StructType const*>(type(memberAccess->expression())))
{ {
if (structType->dataStoredIn(DataLocation::CallData)) if (structType->dataStoredIn(DataLocation::CallData))
{ return "Calldata structs are read-only.";
m_errorReporter.typeError(_expression.location(), "Calldata structs are read-only.");
return;
}
} }
else if (auto arrayType = dynamic_cast<ArrayType const*>(type(memberAccess->expression()))) else if (auto arrayType = dynamic_cast<ArrayType const*>(type(memberAccess->expression())))
{
if (memberAccess->memberName() == "length") if (memberAccess->memberName() == "length")
{
switch (arrayType->location()) switch (arrayType->location())
{ {
case DataLocation::Memory: case DataLocation::Memory:
m_errorReporter.typeError(_expression.location(), "Memory arrays cannot be resized."); return "Memory arrays cannot be resized.";
return;
break;
case DataLocation::CallData: case DataLocation::CallData:
m_errorReporter.typeError(_expression.location(), "Calldata arrays cannot be resized."); return "Calldata arrays cannot be resized.";
return;
case DataLocation::Storage: case DataLocation::Storage:
break; break;
} }
} }
}
}
if (auto identifier = dynamic_cast<Identifier const*>(&_expression)) if (auto identifier = dynamic_cast<Identifier const*>(&_expression))
if (auto varDecl = dynamic_cast<VariableDeclaration const*>(identifier->annotation().referencedDeclaration)) if (auto varDecl = dynamic_cast<VariableDeclaration const*>(identifier->annotation().referencedDeclaration))
if (varDecl->isExternalCallableParameter() && dynamic_cast<ReferenceType const*>(identifier->annotation().type)) if (varDecl->isExternalCallableParameter() && dynamic_cast<ReferenceType const*>(identifier->annotation().type))
{ return "External function arguments of reference type are read-only.";
m_errorReporter.typeError(_expression.location(), "External function arguments of reference type are read-only.");
return;
}
m_errorReporter.typeError(_expression.location(), "Expression has to be an lvalue."); return "Expression has to be an lvalue.";
}());
} }