From 805d312c9834117c2e1bea1380a33c4e9b3483e8 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Fri, 7 Mar 2025 12:37:40 -0800 Subject: [PATCH] Fixed conditional var initializer with valueless nullable --- IDEHelper/Backend/BeIRCodeGen.cpp | 8 ++++++++ IDEHelper/Compiler/BfStmtEvaluator.cpp | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/IDEHelper/Backend/BeIRCodeGen.cpp b/IDEHelper/Backend/BeIRCodeGen.cpp index 3f88c7a4..8ad1678a 100644 --- a/IDEHelper/Backend/BeIRCodeGen.cpp +++ b/IDEHelper/Backend/BeIRCodeGen.cpp @@ -1740,6 +1740,14 @@ void BeIRCodeGen::HandleNextCmd() CMD_PARAM(int, idx); BF_ASSERT(val->GetType()->IsComposite()); + if (val->GetType()->mTypeCode == BeTypeCode_Struct) + { + auto structType = (BeStructType*)val->GetType(); + if (idx >= structType->mMembers.mSize) + { + FatalError("ExtractValue OOB"); + } + } auto extractValueInst = mBeModule->AllocInst(); extractValueInst->mAggVal = val; diff --git a/IDEHelper/Compiler/BfStmtEvaluator.cpp b/IDEHelper/Compiler/BfStmtEvaluator.cpp index b0f82bbe..245fd968 100644 --- a/IDEHelper/Compiler/BfStmtEvaluator.cpp +++ b/IDEHelper/Compiler/BfStmtEvaluator.cpp @@ -1585,11 +1585,15 @@ BfLocalVariable* BfModule::HandleVariableDeclaration(BfVariableDeclaration* varD { if (initValue.mType->IsNullable()) { + auto nullableElementType = initValue.mType->GetUnderlyingType(); auto boolType = GetPrimitiveType(BfTypeCode_Boolean); initValue = LoadValue(initValue); - exprEvaluator->mResult = BfTypedValue(mBfIRBuilder->CreateExtractValue(initValue.mValue, 2), boolType); + exprEvaluator->mResult = BfTypedValue(mBfIRBuilder->CreateExtractValue(initValue.mValue, nullableElementType->IsValuelessType() ? 1 : 2), boolType); handledExprBoolResult = true; - initValue = BfTypedValue(mBfIRBuilder->CreateExtractValue(initValue.mValue, 1), initValue.mType->GetUnderlyingType()); + if (!nullableElementType->IsValuelessType()) + initValue = BfTypedValue(mBfIRBuilder->CreateExtractValue(initValue.mValue, 1), initValue.mType->GetUnderlyingType()); + else + initValue = BfTypedValue(mBfIRBuilder->GetFakeVal(), nullableElementType); } else {