From fa6d69cbddcf92090176b26ed9cd4ea205dc2c00 Mon Sep 17 00:00:00 2001 From: Ivan Bondarev Date: Sun, 22 Jan 2023 12:03:23 +0100 Subject: [PATCH] #2794 --- NETGenerator/NETGenegratorTools.cs | 2 ++ NETGenerator/NETGenerator.cs | 6 ++++++ TestSuite/foreach11.pas | 8 ++++++++ 3 files changed, 16 insertions(+) create mode 100644 TestSuite/foreach11.pas diff --git a/NETGenerator/NETGenegratorTools.cs b/NETGenerator/NETGenegratorTools.cs index 5a83ef21e..6cdda587d 100644 --- a/NETGenerator/NETGenegratorTools.cs +++ b/NETGenerator/NETGenegratorTools.cs @@ -65,6 +65,7 @@ namespace PascalABCCompiler.NETGenerator public static MethodInfo GCHandleAllocPinned; public static MethodInfo OffsetToStringDataProperty; public static MethodInfo StringLengthMethod; + public static MethodInfo CharToString; public static ConstructorInfo IndexOutOfRangeConstructor; public static ConstructorInfo ParamArrayAttributeConstructor; public static MethodInfo StringCopyMethod; @@ -126,6 +127,7 @@ namespace PascalABCCompiler.NETGenerator GCHandleFreeMethod = typeof(GCHandle).GetMethod("Free"); GetTypeFromHandleMethod = typeof(Type).GetMethod("GetTypeFromHandle"); StringCopyMethod = typeof(string).GetMethod("Copy"); + CharToString = typeof(char).GetMethod("ToString", BindingFlags.Static | BindingFlags.Public); } public static bool IsStandType(Type t) diff --git a/NETGenerator/NETGenerator.cs b/NETGenerator/NETGenerator.cs index 8f2e605ed..0a366eaa6 100644 --- a/NETGenerator/NETGenerator.cs +++ b/NETGenerator/NETGenerator.cs @@ -11529,6 +11529,8 @@ namespace PascalABCCompiler.NETGenerator } if (vi.lb.LocalType == TypeFactory.ObjectType && get_current_meth.ReturnType.IsValueType) il.Emit(OpCodes.Box, get_current_meth.ReturnType); + else if (vi.lb.LocalType == TypeFactory.StringType && get_current_meth.ReturnType == TypeFactory.CharType) + il.Emit(OpCodes.Call, TypeFactory.CharToString); il.Emit(OpCodes.Stloc, vi.lb); } else if (vi.kind == VarKind.vkGlobal) @@ -11542,6 +11544,8 @@ namespace PascalABCCompiler.NETGenerator } if (vi.fb.FieldType == TypeFactory.ObjectType && get_current_meth.ReturnType.IsValueType) il.Emit(OpCodes.Box, get_current_meth.ReturnType); + else if (vi.fb.FieldType == TypeFactory.StringType && get_current_meth.ReturnType == TypeFactory.CharType) + il.Emit(OpCodes.Call, TypeFactory.CharToString); il.Emit(OpCodes.Stsfld, vi.fb); } else @@ -11555,6 +11559,8 @@ namespace PascalABCCompiler.NETGenerator } if (vi.fb.FieldType == TypeFactory.ObjectType && get_current_meth.ReturnType.IsValueType) il.Emit(OpCodes.Box, get_current_meth.ReturnType); + else if (vi.fb.FieldType == TypeFactory.StringType && get_current_meth.ReturnType == TypeFactory.CharType) + il.Emit(OpCodes.Call, TypeFactory.CharToString); il.Emit(OpCodes.Stfld, vi.fb); } labels.Push(leave_label); diff --git a/TestSuite/foreach11.pas b/TestSuite/foreach11.pas new file mode 100644 index 000000000..1cc5d8e55 --- /dev/null +++ b/TestSuite/foreach11.pas @@ -0,0 +1,8 @@ +begin + var l := new List(); + foreach var s: string in 'abc' do + l.Add(s); + assert(l[0] = 'a'); + assert(l[1] = 'b'); + assert(l[1] = 'c'); +end. \ No newline at end of file