Skip to content

Commit 35b9a8f

Browse files
committed
fixed the #307
1 parent fa32ef9 commit 35b9a8f

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

src/FastExpressionCompiler/FastExpressionCompiler.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4357,19 +4357,20 @@ private static bool TryEmitSwitch(SwitchExpression expr, IReadOnlyList<PE> param
43574357
//- if switch SwitchValue is a nullable parameter, we should call getValue only once and store the result.
43584358
//- use comparison methods (when defined)
43594359

4360-
var cases = expr.Cases;
43614360
var endLabel = il.DefineLabel();
4361+
var cases = expr.Cases;
43624362
var labels = new Label[cases.Count];
4363-
for (var index = 0; index < cases.Count; index++)
4363+
var dontIgnoreTestResult = parent & ~ParentFlags.IgnoreResult;
4364+
for (var caseIndex = 0; caseIndex < cases.Count; ++caseIndex)
43644365
{
4365-
var switchCase = cases[index];
4366-
labels[index] = il.DefineLabel();
4366+
var cs = cases[caseIndex];
4367+
labels[caseIndex] = il.DefineLabel();
43674368

4368-
foreach (var switchCaseTestValue in switchCase.TestValues)
4369+
foreach (var caseTestValue in cs.TestValues)
43694370
{
4370-
if (!TryEmitComparison(expr.SwitchValue, switchCaseTestValue, ExpressionType.Equal, paramExprs, il, ref closure, setup, parent))
4371+
if (!TryEmitComparison(expr.SwitchValue, caseTestValue, ExpressionType.Equal, paramExprs, il, ref closure, setup, dontIgnoreTestResult))
43714372
return false;
4372-
il.Emit(OpCodes.Brtrue, labels[index]);
4373+
il.Emit(OpCodes.Brtrue, labels[caseIndex]);
43734374
}
43744375
}
43754376

@@ -4380,19 +4381,18 @@ private static bool TryEmitSwitch(SwitchExpression expr, IReadOnlyList<PE> param
43804381
il.Emit(OpCodes.Br, endLabel);
43814382
}
43824383

4383-
for (var index = 0; index < cases.Count; ++index)
4384+
for (var caseIndex = 0; caseIndex < cases.Count; ++caseIndex)
43844385
{
4385-
var switchCase = cases[index];
4386-
il.MarkLabel(labels[index]);
4387-
if (!TryEmit(switchCase.Body, paramExprs, il, ref closure, setup, parent))
4386+
il.MarkLabel(labels[caseIndex]);
4387+
var cs = cases[caseIndex];
4388+
if (!TryEmit(cs.Body, paramExprs, il, ref closure, setup, parent))
43884389
return false;
43894390

4390-
if (index != cases.Count - 1)
4391+
if (caseIndex != cases.Count - 1)
43914392
il.Emit(OpCodes.Br, endLabel);
43924393
}
43934394

43944395
il.MarkLabel(endLabel);
4395-
43964396
return true;
43974397
}
43984398

@@ -4414,8 +4414,8 @@ private static bool TryEmitComparison(Expression exprLeft, Expression exprRight,
44144414
}
44154415

44164416
int lVarIndex = -1, rVarIndex = -1;
4417-
if (!TryEmit(exprLeft, paramExprs, il, ref closure, setup,
4418-
parent & ~ParentFlags.IgnoreResult & ~ParentFlags.InstanceAccess))
4417+
var operandParent = parent & ~ParentFlags.IgnoreResult & ~ParentFlags.InstanceAccess;
4418+
if (!TryEmit(exprLeft, paramExprs, il, ref closure, setup, operandParent))
44194419
return false;
44204420

44214421
if (leftIsNullable)
@@ -4425,8 +4425,7 @@ private static bool TryEmitComparison(Expression exprLeft, Expression exprRight,
44254425
leftOpType = Nullable.GetUnderlyingType(leftOpType);
44264426
}
44274427

4428-
if (!TryEmit(exprRight, paramExprs, il, ref closure, setup,
4429-
parent & ~ParentFlags.IgnoreResult & ~ParentFlags.InstanceAccess))
4428+
if (!TryEmit(exprRight, paramExprs, il, ref closure, setup, operandParent))
44304429
return false;
44314430

44324431
if (leftOpType != rightOpType)

test/FastExpressionCompiler.IssueTests/Issue307_Switch_with_fall_through_throws_InvalidProgramException.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public int Run()
1919
return 1;
2020
}
2121

22-
// [Test]
22+
[Test]
2323
public void Test1()
2424
{
2525
var param = Parameter(typeof(int), "p");
@@ -39,6 +39,18 @@ public void Test1()
3939
param);
4040

4141
lambda.PrintCSharp();
42+
// (Func<int, string>)((int p) => //$
43+
// {
44+
// switch (p)
45+
// {
46+
// case (int)1:
47+
// case (int)2:
48+
// return "foo";
49+
// default:
50+
// return "bar";
51+
// }
52+
// string__58225482:;
53+
// });
4254

4355
var compiled = lambda.CompileSys();
4456
compiled.PrintIL();

test/FastExpressionCompiler.TestsRunner.Net472/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ void Run(Func<int> run, string name = null)
195195
Run(new Issue305_CompileFast_generates_incorrect_code_with_arrays_and_printing().Run);
196196
Run(new FastExpressionCompiler.LightExpression.IssueTests.Issue305_CompileFast_generates_incorrect_code_with_arrays_and_printing().Run);
197197

198+
Run(new Issue307_Switch_with_fall_through_throws_InvalidProgramException().Run);
199+
Run(new FastExpressionCompiler.LightExpression.IssueTests.Issue307_Switch_with_fall_through_throws_InvalidProgramException().Run);
200+
198201
Console.WriteLine($"============={Environment.NewLine}IssueTests are passing in {sw.ElapsedMilliseconds} ms.");
199202
});
200203

test/FastExpressionCompiler.TestsRunner/Program.cs

Lines changed: 3 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.Issue307_Switch_with_fall_through_throws_InvalidProgramException().Run();
1415
// new FastExpressionCompiler.LightExpression.IssueTests.Issue305_CompileFast_generates_incorrect_code_with_arrays_and_printing().Run();
1516
// new FastExpressionCompiler.LightExpression.IssueTests.Issue261_Loop_wih_conditions_fails().Run();
1617
// new FastExpressionCompiler.LightExpression.IssueTests.Issue222_Make_AutoMapper_FEC_branch_to_pass_all_the_tests().Run();
@@ -216,6 +217,8 @@ void Run(Func<int> run, string name = null)
216217

217218
Run(new Issue305_CompileFast_generates_incorrect_code_with_arrays_and_printing().Run);
218219
Run(new FastExpressionCompiler.LightExpression.IssueTests.Issue305_CompileFast_generates_incorrect_code_with_arrays_and_printing().Run);
220+
Run(new Issue307_Switch_with_fall_through_throws_InvalidProgramException().Run);
221+
Run(new FastExpressionCompiler.LightExpression.IssueTests.Issue307_Switch_with_fall_through_throws_InvalidProgramException().Run);
219222

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

0 commit comments

Comments
 (0)