Skip to content

Commit 3441c6d

Browse files
authored
Merge pull request #312 from exyi/nullable-conversion-ignoreresult
Fix #310 - nullable conversion with ignored result
2 parents 682a90e + cd55971 commit 3441c6d

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

src/FastExpressionCompiler/FastExpressionCompiler.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2763,6 +2763,12 @@ private static bool TryEmitConvert(UnaryExpression expr, IReadOnlyList<PE> param
27632763
var underlyingNullableSourceType = Nullable.GetUnderlyingType(sourceType);
27642764
var targetType = expr.Type;
27652765

2766+
if (targetType.IsAssignableFrom(sourceType) && (parent & ParentFlags.IgnoreResult) != 0)
2767+
{
2768+
// quick path for ignored result & conversion which can't cause exception: just do nothing
2769+
return TryEmit(opExpr, paramExprs, il, ref closure, setup, parent);
2770+
}
2771+
27662772
if (sourceTypeIsNullable && targetType == underlyingNullableSourceType)
27672773
{
27682774
if (!TryEmit(opExpr, paramExprs, il, ref closure, setup,
@@ -2813,9 +2819,7 @@ private static bool TryEmitConvert(UnaryExpression expr, IReadOnlyList<PE> param
28132819
if (method != null && method.DeclaringType == targetType && method.GetParameters()[0].ParameterType == sourceType)
28142820
{
28152821
il.Emit(OpCodes.Call, method);
2816-
if ((parent & ParentFlags.IgnoreResult) != 0)
2817-
il.Emit(OpCodes.Pop);
2818-
return true;
2822+
return il.EmitPopIfIgnoreResult(parent);
28192823
}
28202824

28212825
var actualSourceType = sourceTypeIsNullable ? underlyingNullableSourceType : sourceType;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.Linq.Expressions;
4+
#if LIGHT_EXPRESSION
5+
using static FastExpressionCompiler.LightExpression.Expression;
6+
namespace FastExpressionCompiler.LightExpression.IssueTests
7+
#else
8+
using static System.Linq.Expressions.Expression;
9+
namespace FastExpressionCompiler.IssueTests
10+
#endif
11+
{
12+
[TestFixture]
13+
public class Issue310_InvalidProgramException_ignored_nullable
14+
{
15+
public int Run()
16+
{
17+
Test1();
18+
Test2();
19+
return 2;
20+
}
21+
22+
[Test]
23+
public void Test1()
24+
{
25+
var p = Parameter(typeof(int), "tmp0");
26+
var expr =
27+
Lambda<Action<int>>(Block(
28+
Convert(p, typeof(int?)),
29+
Default(typeof(void))), p);
30+
31+
var f = expr.CompileFast();
32+
f(2);
33+
}
34+
35+
[Test]
36+
public void Test2()
37+
{
38+
var p = Parameter(typeof(int), "tmp0");
39+
var expr =
40+
Lambda<Action<int>>(Convert(p, typeof(int?)), new[] { p });
41+
var f = expr.CompileFast();
42+
f(2);
43+
}
44+
}
45+
}

test/FastExpressionCompiler.TestsRunner.Net472/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ void Run(Func<int> run, string name = null)
201201
Run(new Issue308_Wrong_delegate_type_returned_with_closure().Run);
202202
Run(new FastExpressionCompiler.LightExpression.IssueTests.Issue308_Wrong_delegate_type_returned_with_closure().Run);
203203

204+
Run(new Issue310_InvalidProgramException_ignored_nullable().Run);
205+
Run(new FastExpressionCompiler.LightExpression.IssueTests.Issue310_InvalidProgramException_ignored_nullable().Run);
206+
204207
Console.WriteLine($"============={Environment.NewLine}IssueTests are passing in {sw.ElapsedMilliseconds} ms.");
205208
});
206209

test/FastExpressionCompiler.TestsRunner/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ void Run(Func<int> run, string name = null)
226226
Run(new Issue308_Wrong_delegate_type_returned_with_closure().Run);
227227
Run(new FastExpressionCompiler.LightExpression.IssueTests.Issue308_Wrong_delegate_type_returned_with_closure().Run);
228228

229+
Run(new Issue310_InvalidProgramException_ignored_nullable().Run);
230+
Run(new FastExpressionCompiler.LightExpression.IssueTests.Issue310_InvalidProgramException_ignored_nullable().Run);
231+
229232
Console.WriteLine($"============={Environment.NewLine}IssueTests are passing in {sw.ElapsedMilliseconds} ms.");
230233
});
231234

0 commit comments

Comments
 (0)