-
-
Notifications
You must be signed in to change notification settings - Fork 94
System.InvalidProgramException when using loop #498
Description
I'm trying to compile an expression which uses Loop. The following code produces an System.InvalidProgramException when being run. The expression tree does compile correctly with the .NETs Compile() method.
` [TestMethod]
public void Nest_loop_with_break_return_value()
{
// Creating a label that represents the return value
var result = Variable(typeof(int), "result");
var x = Variable(typeof(int), "x");
LabelTarget endMain = Label(typeof(int), "endMain");
LabelTarget endSub1 = Label(typeof(void), "endSub1");
LabelTarget continue1 = Label(typeof(void), "continue1");
var loopExpression = Loop(
Block(
Loop(
Block(
IfThen(GreaterThanOrEqual(x, Constant(10)),
Break(endMain, result)),
AddAssign(result, Constant(1)),
AddAssign(x, Constant(1))
),
endSub1,
continue1
)
),
endMain
);
BlockExpression body = Block(
new ParameterExpression[] { result, x },
loopExpression
);
var loopFunc = Expression.Lambda<Func<int>>(body).CompileFast(true);
//var loopFunc = Expression.Lambda<Func<int>>(body).Compile();
var calResult = loopFunc();
Assert.AreEqual(10, calResult);
}`
Debug Trace:
MarkLabel: loopBodyLabel -- TryEmitLoop:2536
MarkLabel: loopBodyLabel -- TryEmitLoop:2536
MarkLabel: continueLabel -- TryEmitLoop:2544
ldloc.1 -- EmitLoadLocalVariable:6578
ldc.i4.s 10 -- EmitLoadConstantInt:6545
clt -- TryEmitComparison:6063
ldc.i4.0 -- EmitEqualToZeroOrNull:6482
ceq -- EmitEqualToZeroOrNull:6483
brfalse labelIfFalse -- TryEmitConditional:6460
ldloc.0 -- EmitLoadLocalVariable:6576
br labelInfo.GetOrDefineLabel(il) -- TryEmitGoto:2672
MarkLabel: labelIfFalse -- TryEmitConditional:6466
ldloc.0 -- EmitLoadLocalVariable:6576
ldc.i4.1 -- EmitLoadConstantInt:6535
add -- TryEmitArithmeticOperation:6328
stloc.0 -- EmitStoreLocalVariable:6603
ldloc.1 -- EmitLoadLocalVariable:6578
ldc.i4.1 -- EmitLoadConstantInt:6535
add -- TryEmitArithmeticOperation:6328
stloc.2 -- EmitStoreAndLoadLocalVariable:6641
ldloc.2 -- EmitStoreAndLoadLocalVariable:6642
stloc.1 -- EmitStoreLocalVariable:6605
ldloc.2 -- EmitLoadLocalVariable:6580
br loopBodyLabel -- TryEmitLoop:2551
MarkLabel: breakLabel -- TryEmitLoop:2559
br loopBodyLabel -- TryEmitLoop:2551
MarkLabel: breakLabel -- TryEmitLoop:2559
ret -- TryCompileBoundToFirstClosureParam:562