Skip to content

Commit 7b201f2

Browse files
author
Maksim Volkau
committed
@wip added test for #476
1 parent c20e872 commit 7b201f2

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

src/FastExpressionCompiler/FastExpressionCompiler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5055,8 +5055,8 @@ public static bool TryEmitMemberGet(MemberExpression expr,
50555055
// Value type special treatment to load address of value instance in order to call a method.
50565056
// For the parameters, we will skip the address loading because the `LastEmitIsAddress == true` for `Ldarga`,
50575057
// so the condition here will be skipped
5058-
if (!closure.LastEmitIsAddress && objExpr.Type.IsValueType)
5059-
EmitStoreAndLoadLocalVariableAddress(il, objExpr.Type);
5058+
if (!closure.LastEmitIsAddress && !objExpr.Type.IsByRef && objExpr.Type.IsValueType)
5059+
EmitStoreAndLoadLocalVariableAddress(il, objExpr.Type);
50605060
}
50615061

50625062
closure.LastEmitIsAddress = false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Text.Json;
3+
4+
#if LIGHT_EXPRESSION
5+
using FastExpressionCompiler.LightExpression.ImTools;
6+
using static FastExpressionCompiler.LightExpression.Expression;
7+
namespace FastExpressionCompiler.LightExpression.IssueTests;
8+
#else
9+
using System.Linq.Expressions;
10+
using FastExpressionCompiler.ImTools;
11+
using static System.Linq.Expressions.Expression;
12+
namespace FastExpressionCompiler.IssueTests;
13+
#endif
14+
15+
public struct Issue490_Regression_in_compiling_lambdas_with_ref_struct_parameters : ITestX
16+
{
17+
public void Run(TestRun t)
18+
{
19+
Original_case(t);
20+
}
21+
22+
private delegate int TestDelegate(ref Utf8JsonReader reader);
23+
24+
public void Original_case(TestContext t)
25+
{
26+
var param = Parameter(typeof(Utf8JsonReader).MakeByRefType());
27+
var body = Condition(
28+
Equal(Property(param, "TokenType"), Constant(JsonTokenType.Null)),
29+
Default(typeof(int)),
30+
Call(param, "GetInt32", Type.EmptyTypes)
31+
);
32+
var expr = Lambda<TestDelegate>(body, param);
33+
expr.PrintCSharp();
34+
35+
var fs = expr.CompileSys();
36+
fs.PrintIL();
37+
38+
// make reader and advance to the Null token
39+
var reader = new Utf8JsonReader("null"u8);
40+
reader.Read();
41+
t.AreEqual(JsonTokenType.Null, reader.TokenType);
42+
43+
// the compiled function should return default(int), yet it calls reader.GetInt32 instead
44+
var a = fs(ref reader);
45+
t.AreEqual(default, a);
46+
47+
var ff = expr.CompileFast(false);
48+
ff.PrintIL();
49+
50+
var b = ff(ref reader);
51+
t.AreEqual(default, b);
52+
}
53+
}

test/FastExpressionCompiler.TestsRunner/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static void Main()
2727

2828
var t = new LightExpression.TestRun(LightExpression.TestFlags.RethrowException);
2929

30+
t.Run(new LightExpression.IssueTests.Issue490_Regression_in_compiling_lambdas_with_ref_struct_parameters());
3031
t.Run(new LightExpression.IssueTests.Issue398_Optimize_Switch_with_OpCodes_Switch());
3132
t.Run(new LightExpression.IssueTests.Issue468_Optimize_the_delegate_access_to_the_Closure_object_for_the_modern_NET());
3233
t.Run(new LightExpression.IssueTests.Issue472_TryInterpret_and_Reduce_primitive_arithmetic_and_logical_expressions_during_the_compilation());

0 commit comments

Comments
 (0)