diff --git a/BeefLibs/corlib/src/Nullable.bf b/BeefLibs/corlib/src/Nullable.bf index 1e195af7..6172ee57 100644 --- a/BeefLibs/corlib/src/Nullable.bf +++ b/BeefLibs/corlib/src/Nullable.bf @@ -35,6 +35,15 @@ namespace System } } + public T ValueOrDefault + { + [Inline] + get + { + return mValue; + } + } + public ref T ValueRef { [Inline] @@ -96,9 +105,19 @@ namespace System [Inline] public static explicit operator T(Nullable value) { + if (!value.mHasValue) + Debug.FatalError("Value requested for null nullable."); return value.mValue; } + [Inline] + public static ref T operator->(ref Nullable value) + { + if (!value.mHasValue) + Debug.FatalError("Value requested for null nullable."); + return ref value.mValue; + } + [Inline, Commutable] public static bool operator==(Nullable lhs, T rhs) { diff --git a/BeefLibs/corlib/src/Result.bf b/BeefLibs/corlib/src/Result.bf index a4aa719c..3f98d150 100644 --- a/BeefLibs/corlib/src/Result.bf +++ b/BeefLibs/corlib/src/Result.bf @@ -27,6 +27,22 @@ namespace System } } + public ref T ValueRef + { + [Inline] + get mut + { + switch (this) + { + case .Ok(var ref val): return ref val; + case .Err: + { + Internal.FatalError("Unhandled error in result", 2); + } + } + } + } + [Inline] public static implicit operator Result(T value) { @@ -39,6 +55,19 @@ namespace System return result.Unwrap(); } + [Inline] + public static mut T operator->(ref Result result) + { + switch (result) + { + case .Ok(var mut val): return mut val; + case .Err: + { + Internal.FatalError("Unhandled error in result", 2); + } + } + } + [Inline] public void IgnoreError() { @@ -116,9 +145,9 @@ namespace System switch (this) { case .Ok(var val): return val; - case .Err(var err): + case .Err: { - Internal.FatalError(scope String()..AppendF("Unhandled error in result:\n {}", err), 2); + Internal.FatalError("Unhandled error in result", 2); } } } @@ -131,16 +160,47 @@ namespace System } } + public ref T ValueRef + { + [Inline] + get mut + { + switch (this) + { + case .Ok(var ref val): return ref val; + case .Err: + { + Internal.FatalError("Unhandled error in result", 2); + } + } + } + } + + [Inline] public static implicit operator Result(T value) { return .Ok(value); } + [Inline] public static implicit operator T(Result result) { return result.Unwrap(); } + [Inline] + public static mut T operator->(ref Result result) + { + switch (result) + { + case .Ok(var mut val): return mut val; + case .Err: + { + Internal.FatalError("Unhandled error in result", 2); + } + } + } + public void IgnoreError() { } diff --git a/IDEHelper/Backend/BeModule.cpp b/IDEHelper/Backend/BeModule.cpp index 80a1e309..93e3f1da 100644 --- a/IDEHelper/Backend/BeModule.cpp +++ b/IDEHelper/Backend/BeModule.cpp @@ -3409,14 +3409,17 @@ BeStoreInst* BeModule::CreateAlignedStore(BeValue* val, BeValue* ptr, int alignm BeGEPInst* BeModule::CreateGEP(BeValue* ptr, BeValue* idx0, BeValue* idx1) { +#ifdef _DEBUG + BF_ASSERT(ptr->GetType()->IsPointer()); +#endif + auto inst = mAlloc.Alloc(); inst->mPtr = ptr; inst->mIdx0 = idx0; inst->mIdx1 = idx1; AddInst(inst); -#ifdef _DEBUG - BF_ASSERT(ptr->GetType()->IsPointer()); +#ifdef _DEBUG inst->GetType(); #endif diff --git a/IDEHelper/Compiler/BfExprEvaluator.cpp b/IDEHelper/Compiler/BfExprEvaluator.cpp index 159e359a..d2b65911 100644 --- a/IDEHelper/Compiler/BfExprEvaluator.cpp +++ b/IDEHelper/Compiler/BfExprEvaluator.cpp @@ -21939,9 +21939,9 @@ void BfExprEvaluator::PerformUnaryOperation_OnResult(BfExpression* unaryOpExpr, CheckResultForReading(mResult); - if ((unaryOp == BfUnaryOp_Mut) && (!mResult.mType->IsComposite()) && (!mResult.mType->IsGenericParam())) + if ((unaryOp == BfUnaryOp_Mut) && (!mResult.mType->IsValueType()) && (!mResult.mType->IsGenericParam())) { - // Non-composite types are already mutable, leave them alone... + // Non-valuetypes types are already mutable, leave them alone... break; } diff --git a/IDEHelper/Compiler/BfStmtEvaluator.cpp b/IDEHelper/Compiler/BfStmtEvaluator.cpp index 09702ddf..9dc2fc18 100644 --- a/IDEHelper/Compiler/BfStmtEvaluator.cpp +++ b/IDEHelper/Compiler/BfStmtEvaluator.cpp @@ -1945,9 +1945,7 @@ BfLocalVariable* BfModule::HandleVariableDeclaration(BfVariableDeclaration* varD if (auto varRefTypeReference = BfNodeDynCast(varDecl->mTypeRef)) { - BF_ASSERT(val.IsAddr()); isRef = true; - isLet = varRefTypeReference->mVarToken->GetToken() == BfToken_Let; isVar = varRefTypeReference->mVarToken->GetToken() == BfToken_Var; } @@ -2010,8 +2008,13 @@ BfLocalVariable* BfModule::HandleVariableDeclaration(BfVariableDeclaration* varD localDef->mAddr = AllocLocalVariable(localDef->mResolvedType, localDef->mName); if ((val.mValue) && (!localDef->mResolvedType->IsValuelessType()) && (!localDef->mResolvedType->IsVar())) { + if (localDef->mResolvedType->IsRef()) + val = MakeAddressable(val, true, true); + if (val.IsSplat()) + { AggregateSplatIntoAddr(val, localDef->mAddr); + } else mBfIRBuilder->CreateAlignedStore(val.mValue, localDef->mAddr, localDef->mResolvedType->mAlign); }