Jelajahi Sumber

Fixed zero-sized sized array loop issues

Brian Fiete 4 tahun lalu
induk
melakukan
c0e19171d4

+ 1 - 3
IDEHelper/Compiler/BfExprEvaluator.cpp

@@ -19161,9 +19161,7 @@ void BfExprEvaluator::Visit(BfIndexerExpression* indexerExpr)
             	mResult = BfTypedValue(mModule->mBfIRBuilder->GetFakeVal(), underlyingType, true);
 			else
 			{
-				mResult = mModule->GetDefaultTypedValue(underlyingType);
-				if (sizedArrayType->mElementCount != 0)
-					mModule->AssertErrorState();
+				mResult = mModule->GetDefaultTypedValue(underlyingType, false, BfDefaultValueKind_Addr);
 			}
 		}
 		else if (target.IsAddr())

+ 0 - 5
IDEHelper/Compiler/BfModule.cpp

@@ -5053,11 +5053,6 @@ BfIRValue BfModule::CreateTypeDataRef(BfType* type)
 		BfMangler::Mangle(typeDataName, mCompiler->GetMangleKind(), type, this);		
 	}
 
-	if (typeDataName == "?sBfTypeData@Zoing@BeefTest@bf@@2HA")
-	{
-		NOP;
-	}
-
 	BfLogSysM("Creating TypeData %s\n", typeDataName.c_str());
 				
 	globalVariable = mBfIRBuilder->CreateGlobalVariable(mBfIRBuilder->MapTypeInst(typeTypeInst, BfIRPopulateType_Full), true, BfIRLinkageType_External, BfIRValue(), typeDataName);

+ 6 - 2
IDEHelper/Compiler/CeMachine.cpp

@@ -3686,7 +3686,7 @@ BfTypedValue CeContext::Call(BfAstNode* targetSrc, BfModule* module, BfMethodIns
 
 	AutoTimer autoTimer(mCeMachine->mRevisionExecuteTime);
 
-	SetAndRestoreValue<CeContext*> prevContext(mCeMachine->mCurContext, this);
+ 	SetAndRestoreValue<CeContext*> prevContext(mCeMachine->mCurContext, this);
 	SetAndRestoreValue<CeEvalFlags> prevEvalFlags(mCurEvalFlags, flags);
 	SetAndRestoreValue<BfAstNode*> prevTargetSrc(mCurTargetSrc, targetSrc);
 	SetAndRestoreValue<BfModule*> prevModule(mCurModule, module);
@@ -3942,9 +3942,13 @@ BfTypedValue CeContext::Call(BfAstNode* targetSrc, BfModule* module, BfMethodIns
 				Fail("Failed to encode return argument");
 			}
 		}
+		else if (returnType->IsComposite())
+		{
+			returnValue = BfTypedValue(module->mBfIRBuilder->CreateConstArrayZero(module->mBfIRBuilder->MapType(returnType)), returnType);
+		}
 		else
 		{
-			returnValue = BfTypedValue(module->mBfIRBuilder->GetFakeVal(), returnType);
+			returnValue = BfTypedValue(module->mBfIRBuilder->GetFakeVal(), returnType);			
 		}
 	}
 

+ 61 - 1
IDEHelper/Tests/src/Comptime.bf

@@ -109,6 +109,58 @@ namespace Tests
 			return (.)val;
 		}
 
+		public struct TypePrinter<T>
+		{
+			const int cFieldCount = GetFieldCount();
+			const (int32, StringView)[cFieldCount] cMembers = Make();
+
+			static int GetFieldCount()
+			{
+				int fieldCount = 0;
+				for (let field in typeof(T).GetFields())
+					if (!field.IsStatic)
+						fieldCount++;
+
+				//Debug.WriteLine($"{fieldCount}");
+
+				return fieldCount;
+			}
+			
+			static decltype(cMembers) Make()
+			{
+				if (cFieldCount == 0)
+					return default(decltype(cMembers));
+
+				decltype(cMembers) fields = ?;
+
+				int i = 0;
+				for (let field in typeof(T).GetFields())
+				{
+					if (!field.IsStatic)
+						fields[i++] = (field.MemberOffset, field.Name);
+				}
+
+				return fields;
+			}
+
+			public override void ToString(String strBuffer)
+			{
+				for (var t in cMembers)
+				{
+					if (@t != 0)
+						strBuffer.Append("\n");
+					strBuffer.AppendF($"{t.0} {t.1}");
+				}
+			}
+		}
+
+		struct TestType
+		{
+			public float mX;
+			public float mY;
+			public float mZ;
+		}
+
 		[Test]
 		public static void TestBasics()
 		{
@@ -122,12 +174,20 @@ namespace Tests
 			Compiler.Mixin("int val = 99;");
 			Test.Assert(val == 99);
 
-
 			MethodA(34, 45).IgnoreError();
 			Debug.Assert(LogAttribute.gLog == "Called Tests.Comptime.MethodA(int a, int b) 34 45\nError: Err(ErrorB)");
 
 			var v0 = GetBigger((int8)123);
 			Test.Assert(v0.GetType() == typeof(int16));
+
+			String str = scope .();
+			TypePrinter<TestType> tp = .();
+			tp.ToString(str);
+			Debug.Assert(str == """
+				0 mX
+				4 mY
+				8 mZ
+				""");
 		}
 	}
 }

+ 15 - 0
IDEHelper/Tests/src/Loops.bf

@@ -1,9 +1,17 @@
+#pragma warning disable 168
+
 using System;
 
 namespace Tests
 {
 	class Loops
 	{
+		struct StructA
+		{
+			public int32 mA;
+			public int32 mB;
+		}
+
 		[Test]
 		public static void TestBasics()
 		{
@@ -11,6 +19,13 @@ namespace Tests
 			{
 
 			}
+
+			StructA[0] zeroLoop = default;
+			for (var val in zeroLoop)
+			{
+				StructA sa = val;
+				int idx = @val;
+			}
 		}
 	}
 }