Browse Source

Deeper binop interface check for generic param

Brian Fiete 1 year ago
parent
commit
bf3dec931a

+ 24 - 0
IDEHelper/Compiler/BfExprEvaluator.cpp

@@ -69,11 +69,21 @@ BfBaseClassWalker::BfBaseClassWalker(BfType* typeA, BfType* typeB, BfModule* mod
 		AddConstraints(typeA, module->GetGenericParamInstance((BfGenericParamType*)typeA));
 	}
 
+	if ((typeA != NULL) && (typeA->IsInterface()))
+	{
+		AddInterfaces(typeA, typeA->ToTypeInstance());
+	}
+
 	if ((typeB != NULL) && (typeB->IsGenericParam()))
 	{
 		mMayBeFromInterface = true;
 		AddConstraints(typeB, module->GetGenericParamInstance((BfGenericParamType*)typeB));
 	}
+
+	if ((typeB != NULL) && (typeB->IsInterface()))
+	{
+		AddInterfaces(typeB, typeB->ToTypeInstance());
+	}
 }
 
 /*BfBaseClassWalker::BfBaseClassWalker(BfTypeInstance* typeA, BfTypeInstance* typeB)
@@ -95,6 +105,8 @@ void BfBaseClassWalker::AddConstraints(BfType* srcType, BfGenericParamInstance*
 	{
 		auto typeInst = genericParam->mTypeConstraint->ToTypeInstance();
 		{
+			if (typeInst->IsInterface())
+				AddInterfaces(srcType, typeInst->ToTypeInstance());
 			Entry entry(srcType, typeInst);
 			if ((typeInst != NULL) && (!mManualList.Contains(entry)))
 				mManualList.Add(entry);
@@ -103,12 +115,24 @@ void BfBaseClassWalker::AddConstraints(BfType* srcType, BfGenericParamInstance*
 
 	for (auto typeInst : genericParam->mInterfaceConstraints)
 	{
+		if (typeInst->IsInterface())
+			AddInterfaces(srcType, typeInst->ToTypeInstance());
 		Entry entry(srcType, typeInst);
 		if ((typeInst != NULL) && (!mManualList.Contains(entry)))
 			mManualList.Add(entry);
 	}
 }
 
+void BfBaseClassWalker::AddInterfaces(BfType* srcType, BfTypeInstance* typeInst)
+{
+	for (auto ifaceEntry : typeInst->mInterfaces)
+	{
+		Entry entry(srcType, ifaceEntry.mInterfaceType);
+		if ((typeInst != NULL) && (!mManualList.Contains(entry)))
+			mManualList.Add(entry);
+	}
+}
+
 BfBaseClassWalker::Entry BfBaseClassWalker::Next()
 {
 	if (!mManualList.IsEmpty())

+ 1 - 0
IDEHelper/Compiler/BfExprEvaluator.h

@@ -313,6 +313,7 @@ public:
 	BfBaseClassWalker();
 	BfBaseClassWalker(BfType* typeA, BfType* typeB, BfModule* module);
 	void AddConstraints(BfType* srcType, BfGenericParamInstance* genericParam);
+	void AddInterfaces(BfType* srcType, BfTypeInstance* typeInst);
 
 public:
 	Entry Next();

+ 19 - 0
IDEHelper/Tests/src/Interfaces.bf

@@ -413,6 +413,25 @@ namespace Tests
 			}
 		}
 
+	    public interface IAdd
+	    {
+	        static Self operator+(Self a, Self b);
+	    }
+
+	    public interface IAdd2 : IAdd
+	    {
+	    }
+		    
+        public static void MyFunction<T>(T a, T b) where T : IAdd
+        {
+            T sum = a + b;
+        }
+
+        public static void MyFunction2<T>(T a, T b) where T : IAdd2
+        {
+            T sum = a + b;
+        }
+
 		[Test]
 		public static void TestDefaults()
 		{