1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 20:42:21 +02:00

Fixed nullable null coalescing short circuiting

This commit is contained in:
Brian Fiete 2022-08-03 07:54:35 -07:00
parent 0bedd77f0a
commit e6352571c1
3 changed files with 34 additions and 30 deletions

View file

@ -420,31 +420,6 @@ namespace System
return Nullable<TResult>(lhs.mValue | rhs.mValue);
}
//
public static T operator??(Nullable<T> lhs, T rhs)
{
return (lhs.mHasValue) ? lhs.mValue : rhs;
}
public static TResult? operator??<TOther, TResult>(TOther lhs, Nullable<T> rhs) where TResult : operator TOther ?? T where TResult : struct
{
if (!rhs.mHasValue) return null;
return Nullable<TResult>(lhs ?? rhs.mValue);
}
public static TResult? operator??<TOther, TResult>(Nullable<T> lhs, TOther rhs) where TResult : operator T ?? TOther where TResult : struct
{
if (!lhs.mHasValue) return null;
return Nullable<TResult>(lhs.mValue ?? rhs);
}
public static TResult? operator??<TOther, TResult>(Nullable<T> lhs, Nullable<TOther> rhs) where TOther : struct where TResult : operator T ?? TOther where TResult : struct
{
if ((!lhs.mHasValue) || (!rhs.mHasValue)) return null;
return Nullable<TResult>(lhs.mValue ?? rhs.mValue);
}
//
public static TResult? operator<< <TOther, TResult>(TOther lhs, Nullable<T> rhs) where TResult : operator TOther << T where TResult : struct

View file

@ -22925,17 +22925,21 @@ void BfExprEvaluator::PerformBinaryOperation(BfExpression* leftExpression, BfExp
bool BfExprEvaluator::PerformBinaryOperation_NullCoalesce(BfTokenNode* opToken, BfExpression* leftExpression, BfExpression* rightExpression, BfTypedValue leftValue, BfType* wantType, BfTypedValue* assignTo)
{
if ((leftValue) && ((leftValue.mType->IsPointer()) || (leftValue.mType->IsFunction()) || (leftValue.mType->IsObject())) /* || (leftValue.mType->IsNullable())*/)
if ((leftValue) && ((leftValue.mType->IsPointer()) || (leftValue.mType->IsFunction()) || (leftValue.mType->IsObject())) || (leftValue.mType->IsNullable()))
{
leftValue = mModule->LoadValue(leftValue);
BfType* nullableElementType = NULL;
BfIRValue nullableHasValue;
BfTypedValue nullableExtractedLeftValue;
if (leftValue.mType->IsNullable())
{
if (wantType == leftValue.mType)
wantType = leftValue.mType->GetUnderlyingType();
nullableHasValue = mModule->mBfIRBuilder->CreateExtractValue(leftValue.mValue, 1);
leftValue = BfTypedValue(mModule->mBfIRBuilder->CreateExtractValue(leftValue.mValue, 0), leftValue.mType->GetUnderlyingType());
nullableElementType = leftValue.mType->GetUnderlyingType();
nullableHasValue = mModule->mBfIRBuilder->CreateExtractValue(leftValue.mValue, nullableElementType->IsValuelessType() ? 1 : 2); // has_value
if (!nullableElementType->IsValuelessType())
nullableExtractedLeftValue = BfTypedValue(mModule->mBfIRBuilder->CreateExtractValue(leftValue.mValue, 1), nullableElementType); // value
else
nullableExtractedLeftValue = BfTypedValue(mModule->mBfIRBuilder->GetFakeVal(), nullableElementType);
}
if (leftValue.mValue.IsConst())
@ -22984,6 +22988,14 @@ bool BfExprEvaluator::PerformBinaryOperation_NullCoalesce(BfTokenNode* opToken,
mModule->AssertErrorState();
return true;
}
if ((assignTo == NULL) && (leftValue.mType->IsNullable()) && (!rightValue.mType->IsNullable()))
{
if (wantType == leftValue.mType)
wantType = nullableElementType;
leftValue = nullableExtractedLeftValue;
}
rightValue = mModule->LoadValue(rightValue);
if (assignTo == NULL)

View file

@ -1,6 +1,7 @@
#pragma warning disable 168
using System;
using System.Collections;
namespace Tests
{
@ -97,6 +98,22 @@ namespace Tests
String str = "Abc";
StringView? svn = str;
iNull = null;
int? iNull2 = 123;
List<int> l = null;
List<int> l2 = scope .();
int a = iNull2 ?? l.Count;
int b = iNull ?? l2.Count;
var c = iNull ?? iNull2;
Test.Assert(a == 123);
Test.Assert(b == 0);
Test.Assert(typeof(decltype(c)) == typeof(int?));
iNull ??= iNull2;
Test.Assert(iNull == 123);
}
}
}