Kaynağa Gözat

Improved constraint check in CastToValue

Brian Fiete 3 yıl önce
ebeveyn
işleme
aa58c864f7

+ 15 - 21
IDEHelper/Compiler/BfModuleTypeUtils.cpp

@@ -13961,8 +13961,7 @@ BfIRValue BfModule::CastToValue(BfAstNode* srcNode, BfTypedValue typedVal, BfTyp
 		BfBaseClassWalker baseClassWalker(walkFromType, walkToType, this);
 		BfBaseClassWalker baseClassWalker(walkFromType, walkToType, this);
 		
 		
 		bool isConstraintCheck = ((castFlags & BfCastFlags_IsConstraintCheck) != 0);		
 		bool isConstraintCheck = ((castFlags & BfCastFlags_IsConstraintCheck) != 0);		
-
-		BfType* operatorConstraintReturnType = NULL;
+		
 		BfType* bestSelfType = NULL;
 		BfType* bestSelfType = NULL;
 		while (true)
 		while (true)
 		{
 		{
@@ -13986,14 +13985,24 @@ BfIRValue BfModule::CastToValue(BfAstNode* srcNode, BfTypedValue typedVal, BfTyp
 						// Try without arg
 						// Try without arg
 						args.mSize = 0;
 						args.mSize = 0;
 					}
 					}
-										
+
 					if (isConstraintCheck)
 					if (isConstraintCheck)
 					{
 					{
 						auto returnType = CheckOperator(checkType, operatorDef, typedVal, BfTypedValue());
 						auto returnType = CheckOperator(checkType, operatorDef, typedVal, BfTypedValue());
 						if (returnType != NULL)
 						if (returnType != NULL)
 						{
 						{
-							operatorConstraintReturnType = returnType;
-							methodMatcher.mBestMethodDef = operatorDef;
+							auto result = BfTypedValue(mBfIRBuilder->GetFakeVal(), returnType);
+							if (result)
+							{
+								if (result.mType != toType)
+								{
+									auto castedResult = CastToValue(srcNode, result, toType, (BfCastFlags)(castFlags | BfCastFlags_Explicit | BfCastFlags_NoConversionOperator), resultFlags);
+									if (castedResult)
+										return castedResult;
+								}
+								else
+									return result.mValue;
+							}
 						}
 						}
 					}
 					}
 					else
 					else
@@ -14087,22 +14096,7 @@ BfIRValue BfModule::CastToValue(BfAstNode* srcNode, BfTypedValue typedVal, BfTyp
 					}
 					}
 				}
 				}
 			}
 			}
-		}
-		else if (isConstraintCheck)
-		{
-			auto result = BfTypedValue(mBfIRBuilder->GetFakeVal(), operatorConstraintReturnType);
-			if (result)
-			{
-				if (result.mType != toType)
-				{
-					auto castedResult = CastToValue(srcNode, result, toType, (BfCastFlags)(castFlags | BfCastFlags_Explicit | BfCastFlags_NoConversionOperator), resultFlags);
-					if (castedResult)
-						return castedResult;
-				}
-				else
-					return result.mValue;
-			}
-		}
+		}		
 		else
 		else
 		{
 		{
 			BfTypedValue result;
 			BfTypedValue result;

+ 5 - 0
IDEHelper/Tests/src/Nullable.bf

@@ -1,3 +1,5 @@
+#pragma warning disable 168
+
 using System;
 using System;
 
 
 namespace Tests
 namespace Tests
@@ -92,6 +94,9 @@ namespace Tests
 
 
 			Test.Assert(DoAdd(iNull, iNull) == 200);
 			Test.Assert(DoAdd(iNull, iNull) == 200);
 			Test.Assert(DoAdd(iNull, null) == null);
 			Test.Assert(DoAdd(iNull, null) == null);
+
+			String str = "Abc";
+			StringView? svn = str;
 		}
 		}
 	}
 	}
 }
 }