1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-09 12:02:21 +02:00

Improved error for local variable reusing parameter name

This commit is contained in:
Brian Fiete 2022-01-07 08:22:05 -05:00
parent a8f922b112
commit 723f17f139
2 changed files with 14 additions and 9 deletions

View file

@ -17718,13 +17718,13 @@ bool BfExprEvaluator::CheckModifyResult(BfTypedValue typedVal, BfAstNode* refNod
{ {
if (mModule->mCurMethodState->mMixinState != NULL) if (mModule->mCurMethodState->mMixinState != NULL)
error = _Fail(StrFormat("Cannot %s mixin parameter '%s'", modifyType, error = _Fail(StrFormat("Cannot %s mixin parameter '%s'", modifyType,
localVar->mName.c_str(), mModule->MethodToString(mModule->mCurMethodInstance).c_str()), refNode); localVar->mName.c_str()), refNode);
else if ((localVar->mResolvedType->IsGenericParam()) && (onlyNeedsMut)) else if ((localVar->mResolvedType->IsGenericParam()) && (onlyNeedsMut))
error = _Fail(StrFormat("Cannot %s parameter '%s'. Consider adding 'mut' or 'ref' specifier to parameter or copying to a local variable.", modifyType, error = _Fail(StrFormat("Cannot %s parameter '%s'. Consider adding 'mut' or 'ref' specifier to parameter or declaring 'var %s;' to create a mutable copy.", modifyType,
localVar->mName.c_str(), mModule->MethodToString(mModule->mCurMethodInstance).c_str()), refNode); localVar->mName.c_str(), localVar->mName.c_str()), refNode);
else else
error = _Fail(StrFormat("Cannot %s parameter '%s'. Consider adding 'ref' specifier to parameter or copying to a local variable.", modifyType, error = _Fail(StrFormat("Cannot %s parameter '%s'. Consider adding 'ref' specifier to parameter or declaring 'var %s;' to create a mutable copy.", modifyType,
localVar->mName.c_str(), mModule->MethodToString(mModule->mCurMethodInstance).c_str()), refNode); localVar->mName.c_str(), localVar->mName.c_str()), refNode);
return false; return false;
} }
} }

View file

@ -14734,11 +14734,16 @@ void BfModule::CheckVariableDef(BfLocalVariable* variableDef)
if (checkLocal->mIsImplicitParam) if (checkLocal->mIsImplicitParam)
return; // Ignore 'redefinition' return; // Ignore 'redefinition'
if (checkLocal->IsParam()) if (checkLocal->IsParam())
error = Fail(StrFormat("A parameter named '%s' has already been declared.", variableDef->mName.c_str()), variableDef->mNameNode); {
if (variableDef->IsParam())
error = Fail(StrFormat("A parameter named '%s' has already been declared", variableDef->mName.c_str()), variableDef->mNameNode);
else
error = Fail(StrFormat("The name '%s' is already used by a parameter. Consider declaring 'var %s;' if you wish to make a mutable copy of that parameter.", variableDef->mName.c_str(), variableDef->mName.c_str()), variableDef->mNameNode);
}
else if (checkLocal->mLocalVarIdx < mCurMethodState->mCurScope->mLocalVarStart) else if (checkLocal->mLocalVarIdx < mCurMethodState->mCurScope->mLocalVarStart)
error = Fail(StrFormat("A variable named '%s' has already been declared in this parent's scope.", variableDef->mName.c_str()), variableDef->mNameNode); error = Fail(StrFormat("A variable named '%s' has already been declared in this parent's scope", variableDef->mName.c_str()), variableDef->mNameNode);
else else
error = Fail(StrFormat("A variable named '%s' has already been declared in this scope.", variableDef->mName.c_str()), variableDef->mNameNode); error = Fail(StrFormat("A variable named '%s' has already been declared in this scope", variableDef->mName.c_str()), variableDef->mNameNode);
if ((checkLocal->mNameNode != NULL) && (error != NULL)) if ((checkLocal->mNameNode != NULL) && (error != NULL))
mCompiler->mPassInstance->MoreInfo("Previous declaration", checkLocal->mNameNode); mCompiler->mPassInstance->MoreInfo("Previous declaration", checkLocal->mNameNode);
return; return;
@ -14753,7 +14758,7 @@ BfScopeData* BfModule::FindScope(BfAstNode* scopeName, BfMixinState* fromMixinSt
if (auto tokenNode = BfNodeDynCast<BfTokenNode>(scopeName)) if (auto tokenNode = BfNodeDynCast<BfTokenNode>(scopeName))
{ {
if (tokenNode->GetToken() == BfToken_Colon) if (tokenNode->GetToken() == BfToken_Colon)
{ {
if ((!allowAcrossDeferredBlock) && (mCurMethodState->mInDeferredBlock)) if ((!allowAcrossDeferredBlock) && (mCurMethodState->mInDeferredBlock))
{ {
Fail("Cannot access method scope across deferred block boundary", scopeName); Fail("Cannot access method scope across deferred block boundary", scopeName);