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

Fixes to variable assignment detection

This commit is contained in:
Brian Fiete 2022-07-05 08:41:16 -07:00
parent 5277797d73
commit 4d1d972599
3 changed files with 60 additions and 19 deletions

View file

@ -416,5 +416,38 @@ namespace IDETest
} }
while (read > 0); //FAIL while (read > 0); //FAIL
} }
public void Local13()
{
int a = 123;
int b;
switch (a)
{
default: b = 0;
}
int c = b;
}
public void Local14()
{
int a = 123;
int b;
switch (a)
{
default: b = 0; break;
}
int c = b;
}
public void Local15()
{
int a = 123;
int b;
switch (a)
{
default: break;
}
int c = b; //FAIL
}
} }
} }

View file

@ -327,24 +327,21 @@ void BfMethodState::ApplyDeferredLocalAssignData(const BfDeferredLocalAssignData
if (deferredLocalAssignData.mLeftBlockUncond) if (deferredLocalAssignData.mLeftBlockUncond)
{ {
if (mLeftBlockUncond) for (int localIdx = 0; localIdx < (int)mLocals.size(); localIdx++)
{ {
for (int localIdx = 0; localIdx < (int)mLocals.size(); localIdx++) auto localDef = mLocals[localIdx];
if (localDef->mAssignedKind == BfLocalVarAssignKind_None)
{ {
auto localDef = mLocals[localIdx]; bool hadAssignment = false;
if (localDef->mAssignedKind == BfLocalVarAssignKind_None) if (mDeferredLocalAssignData != NULL)
{ {
bool hadAssignment = false; for (auto& entry : mDeferredLocalAssignData->mAssignedLocals)
if (mDeferredLocalAssignData != NULL) if (entry.mLocalVar == localDef)
{ hadAssignment = true;
for (auto& entry : mDeferredLocalAssignData->mAssignedLocals) }
if (entry.mLocalVar == localDef) if (!hadAssignment)
hadAssignment = true; {
} LocalDefined(localDef);
if (!hadAssignment)
{
LocalDefined(localDef);
}
} }
} }
} }

View file

@ -5061,12 +5061,20 @@ void BfModule::Visit(BfSwitchStatement* switchStmt)
SetAndRestoreValue<BfDeferredLocalAssignData*> prevDLA(mCurMethodState->mDeferredLocalAssignData, &deferredLocalAssignDataVec[blockIdx]); SetAndRestoreValue<BfDeferredLocalAssignData*> prevDLA(mCurMethodState->mDeferredLocalAssignData, &deferredLocalAssignDataVec[blockIdx]);
mCurMethodState->mDeferredLocalAssignData->mVarIdBarrier = startingLocalVarId; mCurMethodState->mDeferredLocalAssignData->mVarIdBarrier = startingLocalVarId;
BfScopeData caseScopeData;
caseScopeData.mOuterIsConditional = true;
caseScopeData.mIsSharedTempBlock = true;
mCurMethodState->AddScope(&caseScopeData);
NewScopeState();
bool hadReturn = false; bool hadReturn = false;
VisitCodeBlock(switchCase->mCodeBlock, BfIRBlock(), endBlock, BfIRBlock(), true, &hadReturn, switchStmt->mLabelNode); VisitCodeBlock(switchCase->mCodeBlock, BfIRBlock(), endBlock, BfIRBlock(), true, &hadReturn, switchStmt->mLabelNode);
deferredLocalAssignDataVec[blockIdx].mHadReturn = hadReturn; deferredLocalAssignDataVec[blockIdx].mHadReturn = hadReturn;
caseCount++; caseCount++;
if (!hadReturn) if (!hadReturn)
allHadReturns = false; allHadReturns = false;
RestoreScopeState();
} }
} }
else else
@ -5799,6 +5807,9 @@ void BfModule::Visit(BfRepeatStatement* repeatStmt)
RestoreScopeState(); RestoreScopeState();
prevDLA.Restore();
mCurMethodState->ApplyDeferredLocalAssignData(deferredLocalAssignData);
if (isInfiniteLoop) if (isInfiniteLoop)
EmitDefaultReturn(); EmitDefaultReturn();
} }