Skip to content

Commit 5eca9b8

Browse files
committed
fixed: #305
1 parent 781f007 commit 5eca9b8

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
lines changed

src/FastExpressionCompiler/FastExpressionCompiler.cs

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3332,15 +3332,40 @@ private static bool EmitNewArrayInit(NewArrayExpression expr, IReadOnlyList<PE>
33323332
return true;
33333333
}
33343334

3335-
private static bool TryEmitArrayIndex(Type exprType, ILGenerator il, ParentFlags parent, ref ClosureInfo closure)
3335+
private static bool TryEmitArrayIndex(Type type, ILGenerator il, ParentFlags parent, ref ClosureInfo closure)
33363336
{
3337-
if (!exprType.IsValueType)
3337+
if (!type.IsValueType)
3338+
{
33383339
il.Emit(OpCodes.Ldelem_Ref);
3340+
return true;
3341+
}
3342+
if (type == typeof(Int32))
3343+
il.Emit(OpCodes.Ldelem_I4);
3344+
else if (type == typeof(Int64))
3345+
il.Emit(OpCodes.Ldelem_I8);
3346+
else if (type == typeof(Int16))
3347+
il.Emit(OpCodes.Ldelem_I2);
3348+
else if (type == typeof(SByte))
3349+
il.Emit(OpCodes.Ldelem_I1);
3350+
else if (type == typeof(Single))
3351+
il.Emit(OpCodes.Ldelem_R4);
3352+
else if (type == typeof(Double))
3353+
il.Emit(OpCodes.Ldelem_R8);
3354+
else if (type == typeof(IntPtr))
3355+
il.Emit(OpCodes.Ldelem_I);
3356+
else if (type == typeof(UIntPtr))
3357+
il.Emit(OpCodes.Ldelem_I);
3358+
else if (type == typeof(Byte))
3359+
il.Emit(OpCodes.Ldelem_U1);
3360+
else if (type == typeof(UInt16))
3361+
il.Emit(OpCodes.Ldelem_U2);
3362+
else if (type == typeof(UInt32))
3363+
il.Emit(OpCodes.Ldelem_U4);
33393364
else if ((parent & (ParentFlags.MemberAccess | ParentFlags.Call)) == 0)
3340-
il.Emit(OpCodes.Ldelem, exprType);
3365+
il.Emit(OpCodes.Ldelem, type);
33413366
else
33423367
{
3343-
il.Emit(OpCodes.Ldelema, exprType);
3368+
il.Emit(OpCodes.Ldelema, type);
33443369
closure.LastEmitIsAddress = true;
33453370
}
33463371
return true;
@@ -3917,10 +3942,31 @@ private static bool TryEmitIndexAssign(IndexExpression indexExpr, Type instType,
39173942

39183943
if (indexExpr.Arguments.Count == 1) // one dimensional array
39193944
{
3920-
if (elementType.IsValueType)
3921-
il.Emit(OpCodes.Stelem, elementType);
3922-
else
3945+
if (!elementType.IsValueType)
3946+
{
39233947
il.Emit(OpCodes.Stelem_Ref);
3948+
return true;
3949+
}
3950+
3951+
if (elementType == typeof(Int32))
3952+
il.Emit(OpCodes.Stelem_I4);
3953+
else if (elementType == typeof(Int64))
3954+
il.Emit(OpCodes.Stelem_I8);
3955+
else if (elementType == typeof(Int16))
3956+
il.Emit(OpCodes.Stelem_I2);
3957+
else if (elementType == typeof(SByte))
3958+
il.Emit(OpCodes.Stelem_I1);
3959+
else if (elementType == typeof(Single))
3960+
il.Emit(OpCodes.Stelem_R4);
3961+
else if (elementType == typeof(Double))
3962+
il.Emit(OpCodes.Stelem_R8);
3963+
else if (elementType == typeof(IntPtr))
3964+
il.Emit(OpCodes.Stelem_I);
3965+
else if (elementType == typeof(UIntPtr))
3966+
il.Emit(OpCodes.Stelem_I);
3967+
else
3968+
il.Emit(OpCodes.Stelem, elementType);
3969+
//todo: UInt64 as there is no OpCodes? Stelem_Ref?
39243970
return true;
39253971
}
39263972

test/FastExpressionCompiler.IssueTests/Issue305_CompileFast_generates_incorrect_code_with_arrays_and_printing.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ public int Run()
1919
return 1;
2020
}
2121

22-
// [Test]
22+
public static void WriteLine(double d) {}
23+
24+
[Test]
2325
public void Test1()
2426
{
2527
var arr = Variable(typeof(double[]), "arr");
2628

27-
var printDouble = typeof(Console).GetMethod(nameof(Console.WriteLine), new[] { typeof(double) });
29+
var printDouble = this.GetType().GetMethod(nameof(WriteLine), new[] { typeof(double) });
2830

2931
var expr = Lambda<Func<double>>(
3032
Block(new[] { arr },

test/FastExpressionCompiler.TestsRunner.Net472/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ void Run(Func<int> run, string name = null)
192192

193193
Run(new Issue302_Error_compiling_expression_with_array_access().Run);
194194

195+
Run(new Issue305_CompileFast_generates_incorrect_code_with_arrays_and_printing().Run);
196+
Run(new FastExpressionCompiler.LightExpression.IssueTests.Issue305_CompileFast_generates_incorrect_code_with_arrays_and_printing().Run);
197+
195198
Console.WriteLine($"============={Environment.NewLine}IssueTests are passing in {sw.ElapsedMilliseconds} ms.");
196199
});
197200

test/FastExpressionCompiler.TestsRunner/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public static void Main()
1111
{
1212
RunAllTests();
1313

14+
// new FastExpressionCompiler.LightExpression.IssueTests.Issue305_CompileFast_generates_incorrect_code_with_arrays_and_printing().Run();
1415
// new FastExpressionCompiler.LightExpression.IssueTests.Issue261_Loop_wih_conditions_fails().Run();
1516
// new FastExpressionCompiler.LightExpression.IssueTests.Issue222_Make_AutoMapper_FEC_branch_to_pass_all_the_tests().Run();
1617
// new FastExpressionCompiler.LightExpression.IssueTests.Issue300_Bad_label_content_in_ILGenerator_in_the_Mapster_benchmark_with_FEC_V3().Run();
@@ -212,6 +213,9 @@ void Run(Func<int> run, string name = null)
212213
Run(new FastExpressionCompiler.LightExpression.IssueTests.Issue300_Bad_label_content_in_ILGenerator_in_the_Mapster_benchmark_with_FEC_V3().Run);
213214

214215
Run(new Issue302_Error_compiling_expression_with_array_access().Run);
216+
217+
Run(new Issue305_CompileFast_generates_incorrect_code_with_arrays_and_printing().Run);
218+
Run(new FastExpressionCompiler.LightExpression.IssueTests.Issue305_CompileFast_generates_incorrect_code_with_arrays_and_printing().Run);
215219

216220
Console.WriteLine($"============={Environment.NewLine}IssueTests are passing in {sw.ElapsedMilliseconds} ms.");
217221
});

0 commit comments

Comments
 (0)