From 633424b6da47c961e9e5a7139951dd138dc42045 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Thu, 20 Jan 2022 16:24:18 -0500 Subject: [PATCH] Improved casting of typed primitives with conversion operators --- IDEHelper/Compiler/BfModuleTypeUtils.cpp | 34 +++++++++++++++--------- IDEHelper/Tests/src/Operators.bf | 8 +++--- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/IDEHelper/Compiler/BfModuleTypeUtils.cpp b/IDEHelper/Compiler/BfModuleTypeUtils.cpp index 128993de..7d0b0b67 100644 --- a/IDEHelper/Compiler/BfModuleTypeUtils.cpp +++ b/IDEHelper/Compiler/BfModuleTypeUtils.cpp @@ -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; + } } } diff --git a/IDEHelper/Tests/src/Operators.bf b/IDEHelper/Tests/src/Operators.bf index b374aeaf..13e61bc5 100644 --- a/IDEHelper/Tests/src/Operators.bf +++ b/IDEHelper/Tests/src/Operators.bf @@ -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); // {