Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/FastExpressionCompiler/FastExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1378,9 +1378,15 @@ public static bool TryCollectBoundConstants(ref CompilerContext context, Express

/// <summary>Collects the information about closure constants, nested lambdas, non-passed parameters, goto labels and variables in blocks.
/// Returns `OK` result if everything is fine and other result for error.</summary>
[MethodImpl((MethodImplOptions)256)]
public static Result TryCollectInfo(ref CompilerContext context, Expression expr,
NestedLambdaInfo nestedLambda, ref SmallList<NestedLambdaInfo> rootNestedLambdas,
short scopeIdx, bool enterable = false)
short scopeIdx, bool enterable = false) =>
TryCollectInfo(ref context, expr, nestedLambda, ref rootNestedLambdas, scopeIdx, enterable, false);

private static Result TryCollectInfo(ref CompilerContext context, Expression expr,
NestedLambdaInfo nestedLambda, ref SmallList<NestedLambdaInfo> rootNestedLambdas,
short scopeIdx, bool enterable, bool switchBody)
{
var r = Result.OK;
var quotedData = (context.Status & ClosureStatus.QuotedData) != 0;
Expand Down Expand Up @@ -1635,11 +1641,12 @@ public static Result TryCollectInfo(ref CompilerContext context, Expression expr
return r; // yeah, this is the real case - the block may not contain any expressions

// The enterable block without the direct labels is transparent for the validation, so it does not need its own scope
var needsScope = !enterable;
for (var i = 0; !needsScope & i < blockExprCount; ++i)
var needsScope = !switchBody && !enterable;
for (var i = 0; !switchBody && !needsScope && i < blockExprCount; ++i)
needsScope = blockExprs[i].NodeType == ExpressionType.Label;
if (needsScope)
scopeIdx = (short)context.Scopes.Add(new LabelScope { Parent = new ScopeIdx(scopeIdx, enterable) });
switchBody = false;

var varExprs = blockExpr.Variables;
var varExprCount = varExprs?.Count ?? 0; // todo: @perf optimize for an empty and a single variable
Expand Down Expand Up @@ -1755,9 +1762,13 @@ public static Result TryCollectInfo(ref CompilerContext context, Expression expr
}
return r; // no matched body and no default -> nothing to collect
}
if ((r = TryCollectInfo(ref context, switchExpr.SwitchValue, nestedLambda, ref rootNestedLambdas, scopeIdx)) != Result.OK ||
if ((r = TryCollectInfo(ref context, switchExpr.SwitchValue, nestedLambda, ref rootNestedLambdas, scopeIdx)) != Result.OK)
return r;
// Case bodies share statement-label visibility within one switch boundary.
scopeIdx = (short)context.Scopes.Add(new LabelScope { Parent = new ScopeIdx(scopeIdx, false) });
if (
switchExpr.DefaultBody != null && // todo: @check is the order of collection affects the result?
(r = TryCollectInfo(ref context, switchExpr.DefaultBody, nestedLambda, ref rootNestedLambdas, scopeIdx, enterable)) != Result.OK)
(r = TryCollectInfo(ref context, switchExpr.DefaultBody, nestedLambda, ref rootNestedLambdas, scopeIdx, true, switchBody: true)) != Result.OK)
return r;

var switchCases = switchExpr.Cases;
Expand All @@ -1772,9 +1783,10 @@ public static Result TryCollectInfo(ref CompilerContext context, Expression expr
return r;
}
for (var i = 0; i < switchCases.Count - 1; i++)
if ((r = TryCollectInfo(ref context, switchCases[i].Body, nestedLambda, ref rootNestedLambdas, scopeIdx, enterable)) != Result.OK)
if ((r = TryCollectInfo(ref context, switchCases[i].Body, nestedLambda, ref rootNestedLambdas, scopeIdx, true, switchBody: true)) != Result.OK)
return r;
expr = switchCases[switchCases.Count - 1].Body;
enterable = switchBody = true;
continue;
}
return r;
Expand Down
2 changes: 2 additions & 0 deletions test/FastExpressionCompiler.TestsRunner.Net472/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ static void RunLightExpressionTests(object state)
t.Run(new LightExpression.UnitTests.NestedBindingSpillsTests());
t.Run(new LightExpression.UnitTests.RuntimeExpressionInvokeTests());
t.Run(new LightExpression.UnitTests.IgnoredMemberInitializerTests());
t.Run(new LightExpression.UnitTests.SwitchCaseLabelTests());
t.Run(new LightExpression.UnitTests.VoidLabelValueRegressionTests());
t.Run(new LightExpression.UnitTests.ReferenceInstanceCallRegressionTests());
t.Run(new LightExpression.UnitTests.NestedBindingNullReceiversTests());
Expand Down Expand Up @@ -174,6 +175,7 @@ static void RunLightExpressionTests(object state)
fecTests.Run(new NestedBindingSpillsTests());
fecTests.Run(new RuntimeExpressionInvokeTests());
fecTests.Run(new IgnoredMemberInitializerTests());
fecTests.Run(new SwitchCaseLabelTests());
fecTests.Run(new VoidLabelValueRegressionTests());
fecTests.Run(new ReferenceInstanceCallRegressionTests());
fecTests.Run(new NestedBindingNullReceiversTests());
Expand Down
2 changes: 2 additions & 0 deletions test/FastExpressionCompiler.TestsRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public static void Main(string[] args)
lt.Run(new LightExpression.UnitTests.NestedBindingSpillsTests());
lt.Run(new LightExpression.UnitTests.RuntimeExpressionInvokeTests());
lt.Run(new LightExpression.UnitTests.IgnoredMemberInitializerTests());
lt.Run(new LightExpression.UnitTests.SwitchCaseLabelTests());
lt.Run(new LightExpression.UnitTests.VoidLabelValueRegressionTests());
lt.Run(new LightExpression.UnitTests.ReferenceInstanceCallRegressionTests());
lt.Run(new LightExpression.UnitTests.NestedBindingNullReceiversTests());
Expand Down Expand Up @@ -200,6 +201,7 @@ public static void Main(string[] args)
st.Run(new NestedBindingSpillsTests());
st.Run(new RuntimeExpressionInvokeTests());
st.Run(new IgnoredMemberInitializerTests());
st.Run(new SwitchCaseLabelTests());
st.Run(new VoidLabelValueRegressionTests());
st.Run(new ReferenceInstanceCallRegressionTests());
st.Run(new NestedBindingNullReceiversTests());
Expand Down
35 changes: 35 additions & 0 deletions test/FastExpressionCompiler.UnitTests/SwitchCaseLabelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using S = System.Linq.Expressions.Expression;
#if LIGHT_EXPRESSION
namespace FastExpressionCompiler.LightExpression.UnitTests;
#else
namespace FastExpressionCompiler.UnitTests;
#endif

public struct SwitchCaseLabelTests : ITestX
{
public void Run(TestRun t) { Cross_case_jumps_stay_inside_switch_scope(t); Outside_jumps_cannot_enter_the_switch(t); }
public void Cross_case_jumps_stay_inside_switch_scope(TestContext t)
{
foreach(var withDefault in new[]{false,true})
{
var input=S.Parameter(typeof(int));var target=S.Label(typeof(int));
var body=S.Switch(typeof(int),input,withDefault?S.Block(S.Label(target,S.Constant(2))):S.Constant(0),null,
S.SwitchCase(S.Block(S.Goto(target,S.Constant(29)),S.Constant(1)),S.Constant(0)),
S.SwitchCase(withDefault?(System.Linq.Expressions.Expression)S.Constant(2):S.Block(S.Label(target,S.Constant(2))),S.Constant(1)));
var tree=S.Lambda<Func<int,int>>(body,input);
foreach(var flags in new[]{CompilerFlags.Default,CompilerFlags.NoInvocationLambdaInlining,CompilerFlags.EnableDelegateDebugInfo})
{
var fast=tree.FromSysExpression().CompileFast(true,flags);if(!t.IsNotNull(fast))return;
foreach(var n in new[]{0,1,2})t.AreEqual(tree.Compile()(n),fast(n));
}
}
}
public void Outside_jumps_cannot_enter_the_switch(TestContext t)
{
var input=S.Parameter(typeof(int));var target=S.Label(typeof(int));
var body=S.Switch(input,S.Constant(0),S.SwitchCase(S.Block(S.Label(target,S.Constant(2))),S.Constant(1)));
var tree=S.Lambda<Func<int,int>>(S.Block(S.Goto(target,S.Constant(7)),body),input);
t.Throws<InvalidOperationException>(()=>tree.Compile());t.Throws<InvalidOperationException>(()=>tree.FromSysExpression().CompileFast(true));
}
}
Loading