Moved a check related to constants to TypeChecker

And added a proper error message when constant types containing (nested) mapping types are used.
This commit is contained in:
hrkrshnn
2021-10-26 18:43:04 +02:00
parent 51009c005d
commit 8815d6f5f0
15 changed files with 30 additions and 21 deletions
@@ -437,14 +437,16 @@ void DeclarationTypeChecker::endVisit(VariableDeclaration const& _variable)
type = TypeProvider::withLocation(ref, typeLoc, isPointer);
}
if (_variable.isConstant() && !type->isValueType())
{
bool allowed = false;
if (auto arrayType = dynamic_cast<ArrayType const*>(type))
allowed = arrayType->isByteArray();
if (!allowed)
m_errorReporter.fatalDeclarationError(9259_error, _variable.location(), "Constants of non-value type not yet implemented.");
}
if (
_variable.isConstant() &&
!dynamic_cast<UserDefinedValueType const*>(type) &&
type->containsNestedMapping()
)
m_errorReporter.fatalDeclarationError(
3530_error,
_variable.location(),
"The type contains a (nested) mapping and therefore cannot be a constant."
);
_variable.annotation().type = type;
}
+9
View File
@@ -530,6 +530,15 @@ bool TypeChecker::visit(VariableDeclaration const& _variable)
}
if (_variable.isConstant())
{
if (!varType->isValueType())
{
bool allowed = false;
if (auto arrayType = dynamic_cast<ArrayType const*>(varType))
allowed = arrayType->isByteArray();
if (!allowed)
m_errorReporter.fatalTypeError(9259_error, _variable.location(), "Constants of non-value type not yet implemented.");
}
if (!_variable.value())
m_errorReporter.typeError(4266_error, _variable.location(), "Uninitialized \"constant\" variable.");
else if (!*_variable.value()->annotation().isPure)
+2
View File
@@ -1124,6 +1124,8 @@ public:
bool containsNestedMapping() const override
{
solAssert(nameable(), "Called for a non nameable type.");
// DeclarationTypeChecker::endVisit(VariableDeclaration const&)
// assumes that this will never be true.
solAssert(!underlyingType().containsNestedMapping(), "");
return false;
}