|
| 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 | +} |
0 commit comments