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

Fixed payload enum switch 'case .A:' after a 'case .A(let value):'

This commit is contained in:
Brian Fiete 2023-07-24 12:52:23 -07:00
parent 11bde5caf2
commit 71dc0ab9d7
2 changed files with 47 additions and 6 deletions

View file

@ -4640,6 +4640,7 @@ void BfModule::Visit(BfSwitchStatement* switchStmt)
bool prevHadFallthrough = false; bool prevHadFallthrough = false;
Dictionary<int64, _CaseState> handledCases; Dictionary<int64, _CaseState> handledCases;
HashSet<int64> condCases;
for (BfSwitchCase* switchCase : switchStmt->mSwitchCases) for (BfSwitchCase* switchCase : switchStmt->mSwitchCases)
{ {
deferredLocalAssignDataVec[blockIdx].mScopeData = mCurMethodState->mCurScope; deferredLocalAssignDataVec[blockIdx].mScopeData = mCurMethodState->mCurScope;
@ -4810,22 +4811,42 @@ void BfModule::Visit(BfSwitchStatement* switchStmt)
} }
else if ((constantInt != NULL) && (!hadWhen) && (!isConstSwitch)) else if ((constantInt != NULL) && (!hadWhen) && (!isConstSwitch))
{ {
if (!hadConditional) if (hadConditional)
{
condCases.Add(constantInt->mInt64);
}
else
{ {
_CaseState* caseState = NULL; _CaseState* caseState = NULL;
handledCases.TryAdd(constantInt->mInt64, NULL, &caseState); handledCases.TryAdd(constantInt->mInt64, NULL, &caseState);
if (caseState->mUncondBlock) if (condCases.Contains(constantInt->mInt64))
{ {
_ShowCaseError(constantInt->mInt64, caseExpr); // This is a 'case .A:' after a 'case .A(let value):'
eqResult = mBfIRBuilder->CreateCmpEQ(enumTagVal.mValue, caseValue.mValue);
notEqBB = mBfIRBuilder->CreateBlock(StrFormat("switch.notEq.%d", blockIdx));
mayHaveMatch = true;
mBfIRBuilder->CreateCondBr(eqResult, caseBlock, notEqBB);
mBfIRBuilder->AddBlock(notEqBB);
mBfIRBuilder->SetInsertPoint(notEqBB);
} }
else else
{ {
caseState->mUncondBlock = doBlock; if (caseState->mUncondBlock)
mBfIRBuilder->AddSwitchCase(switchStatement, caseIntVal.mValue, doBlock); {
hadConstIntVals = true; _ShowCaseError(constantInt->mInt64, caseExpr);
}
else
{
caseState->mUncondBlock = doBlock;
mBfIRBuilder->AddSwitchCase(switchStatement, caseIntVal.mValue, doBlock);
hadConstIntVals = true;
}
} }
} }
mayHaveMatch = true; mayHaveMatch = true;
} }
else if (!handled) else if (!handled)

View file

@ -112,6 +112,26 @@ namespace Tests
Test.Assert(false); Test.Assert(false);
} }
switch (ee)
{
case .B(123):
Test.Assert(true);
case .B:
Test.Assert(false);
default:
Test.Assert(false);
}
switch (ee)
{
case .A:
case .B(123):
Test.Assert(true);
case .B:
Test.Assert(false);
case .C:
}
EnumF ef = .EE(.C(3, 4)); EnumF ef = .EE(.C(3, 4));
switch (ef) switch (ef)
{ {