Skip to content

InvalidProgramException when using a conditional return null with hyperbee.expressions #494

@victorr99

Description

@victorr99

Hi!

I'm trying to compile an expression which uses the BlockAsync and Await methods of hyperbee.expressions. The following code produces an InvalidProgramException when being run:

Task<object> Something()
{
    return Task.FromResult<object>(new object());
}

var dlgarg = Parameter(typeof(Func<Task<object>>), "dlg");
var resvar = Variable(typeof(object), "res");
var retlbl = Label(typeof(object), "return");

var lambda = Lambda<Func<Func<Task<object>>, Task<object>>>(
    BlockAsync(
        [resvar],
        Assign(
            resvar,
            Await(
                Invoke(dlgarg))),
        IfThen(
            Equal(resvar, Constant(null, typeof(object))),
            Return(retlbl, Constant(null, typeof(object)))),
        Return(retlbl, resvar, typeof(object)),
        Label(
            retlbl,
            Constant(null, typeof(object)))),
    dlgarg);

var fn = lambda.CompileFast();
Console.WriteLine(await fn(Something));

The exception being raised:

Unhandled exception. System.InvalidProgramException: Common Language Runtime detected an invalid program.
   at (ArrayClosure, StateMachine1)
   at FastExpressionCompiler.ExpressionCompiler.CurryClosureActions.<>c__DisplayClass2_0`2.<Curry>b__0(T1 t1)
   at StateMachine1.IAsyncStateMachine.MoveNext()
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
   at (ArrayClosure, Func`1)
   at Program.Main() in C:\something\Program.cs:line 51
   at Program.<Main>()

The expression tree does compile correctly with the .NETs Compile() method. If you remove the IfThen from the expression tree, like stated in the comment in the example code, it is also working like expected.
The code also works properly when the BlockAsync and Await methods are removed. Since it is working properly with .NET's Compile() method, I assume this is a bug in FEC?

I'm using version 1.3.2 of hyperbee.expressions.

Edit 2026-02-02:
I've tried to narrow the issue down in such a way that it doesn't involve hyperbee.extensions, but I haven't had success so far. I'll try again at a later time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions