Disallow immutable initialization in try catch statements

Trigger github
This commit is contained in:
Nikola Matic
2023-05-09 14:48:14 +02:00
parent 2da0a861ce
commit 9a87b587d5
4 changed files with 64 additions and 0 deletions
@@ -132,6 +132,19 @@ bool ImmutableValidator::visit(WhileStatement const& _whileStatement)
return false;
}
bool ImmutableValidator::visit(TryStatement const& _tryStatement)
{
ScopedSaveAndRestore constructorGuard{m_inTryStatement, true};
_tryStatement.externalCall().accept(*this);
for (auto&& clause: _tryStatement.clauses())
if (clause)
clause->accept(*this);
return false;
}
void ImmutableValidator::endVisit(IdentifierPath const& _identifierPath)
{
if (auto const callableDef = dynamic_cast<CallableDeclaration const*>(_identifierPath.annotation().referencedDeclaration))
@@ -217,6 +230,12 @@ void ImmutableValidator::analyseVariableReference(VariableDeclaration const& _va
_expression.location(),
"Cannot write to immutable here: Immutable variables cannot be initialized inside an if statement."
);
else if (m_inTryStatement)
m_errorReporter.typeError(
4130_error,
_expression.location(),
"Cannot write to immutable here: Immutable variables cannot be initialized inside a try/catch statement."
);
else if (m_initializedStateVariables.count(&_variableReference))
{
if (!read)
@@ -55,6 +55,7 @@ private:
bool visit(MemberAccess const& _memberAccess);
bool visit(IfStatement const& _ifStatement);
bool visit(WhileStatement const& _whileStatement);
bool visit(TryStatement const& _tryStatement);
void endVisit(IdentifierPath const& _identifierPath);
void endVisit(Identifier const& _identifier);
void endVisit(Return const& _return);
@@ -78,6 +79,7 @@ private:
bool m_inLoop = false;
bool m_inBranch = false;
bool m_inCreationContext = true;
bool m_inTryStatement = false;
};
}