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

Improved casting of typed primitives with conversion operators

This commit is contained in:
Brian Fiete 2022-01-20 16:24:18 -05:00
parent 83bed6b004
commit 633424b6da
2 changed files with 26 additions and 16 deletions

View file

@ -12818,15 +12818,13 @@ BfIRValue BfModule::CastToValue(BfAstNode* srcNode, BfTypedValue typedVal, BfTyp
else if ((returnCanCast) &&
(!underlyingCanCast))
{
doCall = true;
// Can do
}
else if ((CanCast(GetFakeTypedValue(underlyingType), returnType, implicitCastFlags)) &&
(!CanCast(GetFakeTypedValue(returnType), underlyingType)))
{
doCall = true;
}
else
doCall = false;
else if ((!CanCast(GetFakeTypedValue(underlyingType), returnType, implicitCastFlags)) &&
(CanCast(GetFakeTypedValue(returnType), underlyingType, implicitCastFlags)))
{
doCall = false;
}
}
}
@ -12834,15 +12832,27 @@ BfIRValue BfModule::CastToValue(BfAstNode* srcNode, BfTypedValue typedVal, BfTyp
if ((explicitCast) && (toType->IsTypedPrimitive()))
{
auto underlyingType = toType->GetUnderlyingType();
if ((paramType != underlyingType) && (CanCast(typedVal, underlyingType, (BfCastFlags)(castFlags | BfCastFlags_NoConversionOperator))))
if ((paramType != underlyingType) &&
(CanCast(typedVal, underlyingType, (BfCastFlags)(castFlags | BfCastFlags_NoConversionOperator))))
{
if ((CanCast(GetFakeTypedValue(underlyingType), paramType, implicitCastFlags)) &&
(!CanCast(GetFakeTypedValue(paramType), underlyingType, implicitCastFlags)))
float underlyingCanCast = CanCast(typedVal, underlyingType, implicitCastFlags);
float paramCanCast = CanCast(typedVal, paramType, implicitCastFlags);
if ((underlyingType) &&
(!paramCanCast))
{
doCall = false;
}
else if ((paramCanCast) &&
(!underlyingType))
{
doCall = true;
}
else
else if ((!CanCast(GetFakeTypedValue(underlyingType), paramType, implicitCastFlags)) &&
(CanCast(GetFakeTypedValue(paramType), underlyingType, implicitCastFlags)))
{
doCall = false;
}
}
}

View file

@ -283,7 +283,7 @@ namespace Tests
public static explicit operator MyEnum(Self self)
{
return .Case1;
return .Case2;
}
}
@ -572,9 +572,9 @@ namespace Tests
int32 b = a + 100;
Test.Assert(b == 223);
MyOtherEnum myEnum = .One;
MyEnum me = (MyEnum)myEnum;
Test.Assert(me == .Case1);
MyOtherEnum moe = .One;
MyEnum me = (MyEnum)moe;
Test.Assert(me == .Case2);
//
{