Skip to content

Commit af6e571

Browse files
committed
fixing #305 again
1 parent 5eca9b8 commit af6e571

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/FastExpressionCompiler/FastExpressionCompiler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3361,7 +3361,7 @@ private static bool TryEmitArrayIndex(Type type, ILGenerator il, ParentFlags par
33613361
il.Emit(OpCodes.Ldelem_U2);
33623362
else if (type == typeof(UInt32))
33633363
il.Emit(OpCodes.Ldelem_U4);
3364-
else if ((parent & (ParentFlags.MemberAccess | ParentFlags.Call)) == 0)
3364+
else if ((parent & (ParentFlags.MemberAccess | ParentFlags.InstanceAccess)) == 0)
33653365
il.Emit(OpCodes.Ldelem, type);
33663366
else
33673367
{

test/FastExpressionCompiler.IssueTests/Issue305_CompileFast_generates_incorrect_code_with_arrays_and_printing.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using NUnit.Framework;
22
using System;
3+
using System.Linq;
4+
using System.Collections.Generic;
35
using System.Linq.Expressions;
46
#if LIGHT_EXPRESSION
57
using System.Text;
@@ -19,33 +21,32 @@ public int Run()
1921
return 1;
2022
}
2123

22-
public static void WriteLine(double d) {}
24+
private List<double> _items = new List<double>();
25+
26+
public void WriteLine(double n) => _items.Add(n);
2327

2428
[Test]
2529
public void Test1()
2630
{
2731
var arr = Variable(typeof(double[]), "arr");
2832

29-
var printDouble = this.GetType().GetMethod(nameof(WriteLine), new[] { typeof(double) });
33+
var print = this.GetType().GetMethod(nameof(WriteLine), new[] { typeof(double) });
3034

3135
var expr = Lambda<Func<double>>(
3236
Block(new[] { arr },
3337
Assign(arr, NewArrayBounds(typeof(double), Constant(1))),
3438
Assign(ArrayAccess(arr, Constant(0)), Constant(123.456)),
3539

36-
Call(printDouble, ArrayAccess(arr, Constant(0))),
37-
Call(printDouble, ArrayAccess(arr, Constant(0))),
38-
Call(printDouble, ArrayAccess(arr, Constant(0))),
39-
Call(printDouble, ArrayAccess(arr, Constant(0))),
40-
Call(printDouble, ArrayAccess(arr, Constant(0))),
41-
Call(printDouble, ArrayAccess(arr, Constant(0))),
42-
Call(printDouble, ArrayAccess(arr, Constant(0))),
40+
Call(Constant(this), print, ArrayAccess(arr, Constant(0))),
41+
Call(Constant(this), print, ArrayAccess(arr, Constant(0))),
4342

4443
ArrayAccess(arr, Constant(0))
4544
));
4645

4746
expr.PrintCSharp();
4847

48+
_items.Clear();
49+
4950
var fs = expr.CompileSys();
5051
fs.PrintIL();
5152

@@ -57,6 +58,7 @@ public void Test1()
5758

5859
var res2 = ff();
5960
Assert.AreEqual(123.456, res2);
61+
Assert.AreEqual(4, _items.Count(x => x == 123.456));
6062
}
6163
}
6264
}

0 commit comments

Comments
 (0)