mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Improve error message when trying to modify constant variables
This commit is contained in:
@@ -1529,6 +1529,8 @@ bool TypeChecker::visit(Identifier const& _identifier)
|
||||
!!annotation.referencedDeclaration,
|
||||
"Referenced declaration is null after overload resolution."
|
||||
);
|
||||
auto variableDeclaration = dynamic_cast<VariableDeclaration const*>(annotation.referencedDeclaration);
|
||||
annotation.isConstant = variableDeclaration != nullptr && variableDeclaration->isConstant();
|
||||
annotation.isLValue = annotation.referencedDeclaration->isLValue();
|
||||
annotation.type = annotation.referencedDeclaration->type();
|
||||
if (!annotation.type)
|
||||
@@ -1612,7 +1614,10 @@ void TypeChecker::requireLValue(Expression const& _expression)
|
||||
{
|
||||
_expression.annotation().lValueRequested = true;
|
||||
_expression.accept(*this);
|
||||
if (!_expression.annotation().isLValue)
|
||||
|
||||
if (_expression.annotation().isConstant)
|
||||
typeError(_expression.location(), "Cannot assign to a constant variable.");
|
||||
else if (!_expression.annotation().isLValue)
|
||||
typeError(_expression.location(), "Expression has to be an lvalue.");
|
||||
}
|
||||
|
||||
|
||||
@@ -154,6 +154,8 @@ struct ExpressionAnnotation: ASTAnnotation
|
||||
{
|
||||
/// Inferred type of the expression.
|
||||
TypePointer type;
|
||||
/// Whether the expression is a constant variable
|
||||
bool isConstant = false;
|
||||
/// Whether it is an LValue (i.e. something that can be assigned to).
|
||||
bool isLValue = false;
|
||||
/// Whether the expression is used in a context where the LValue is actually required.
|
||||
|
||||
Reference in New Issue
Block a user