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

Fallthrough fixes for destructuring and allHadReturns

This commit is contained in:
Brian Fiete 2021-06-19 09:29:36 -07:00
parent fa6bdc0d04
commit 5e9ac07804
3 changed files with 54 additions and 1 deletions

View file

@ -21,5 +21,29 @@ namespace IDETest
int a = val1; //FAIL int a = val1; //FAIL
} }
} }
int Switch1(Result<int> res)
{
switch (res)
{
case .Ok(let a):
fallthrough;
case .Err(let b): //FAIL
return 1;
}
}
int Switch2(Result<int> res)
{
switch (res)
{
case .Ok(let a):
if (a > 0)
return 2;
fallthrough;
case .Err:
return 1;
}
} //FAIL
} }
} }

View file

@ -3621,6 +3621,8 @@ void BfModule::DoIfStatement(BfIfStatement* ifStmt, bool includeTrueStmt, bool i
VisitEmbeddedStatement(ifStmt->mTrueStatement); VisitEmbeddedStatement(ifStmt->mTrueStatement);
} }
prevDLA.Restore(); prevDLA.Restore();
if (mCurMethodState->mDeferredLocalAssignData != NULL)
mCurMethodState->mDeferredLocalAssignData->mLeftBlock = deferredLocalAssignData.mLeftBlock;
bool trueHadReturn = mCurMethodState->mHadReturn; bool trueHadReturn = mCurMethodState->mHadReturn;
@ -4365,6 +4367,14 @@ void BfModule::Visit(BfSwitchStatement* switchStmt)
hadWhen = true; hadWhen = true;
whenExpr = checkWhenExpr; whenExpr = checkWhenExpr;
} }
if (auto invocationExpr = BfNodeDynCast<BfInvocationExpression>(caseExpr))
{
if (prevHadFallthrough)
{
Fail("Destructuring cannot be used when the previous case contains a fallthrough", caseExpr);
}
}
} }
BfIRBlock lastNotEqBlock; BfIRBlock lastNotEqBlock;
@ -4664,7 +4674,8 @@ void BfModule::Visit(BfSwitchStatement* switchStmt)
openedScope = false; openedScope = false;
deferredLocalAssignDataVec[blockIdx].mHadReturn = hadReturn; deferredLocalAssignDataVec[blockIdx].mHadReturn = hadReturn;
caseCount++; caseCount++;
if ((!hadReturn) && (!mCurMethodState->mDeferredLocalAssignData->mHadFallthrough)) if ((!hadReturn) &&
((!mCurMethodState->mDeferredLocalAssignData->mHadFallthrough) || (mCurMethodState->mDeferredLocalAssignData->mLeftBlock)))
allHadReturns = false; allHadReturns = false;
if (auto block = BfNodeDynCast<BfBlock>(switchCase->mCodeBlock)) if (auto block = BfNodeDynCast<BfBlock>(switchCase->mCodeBlock))

View file

@ -0,0 +1,18 @@
using System;
namespace Tests
{
class Switches
{
int Switch0(Result<int> res)
{
switch (res)
{
case .Ok(let a):
return 0;
case .Err(let b):
return 1;
}
}
}
}