Skip to content

Shared variables with nested lambdas returning incorrect values #437

@MattEdwardsWaggleBee

Description

@MattEdwardsWaggleBee

I found an issue with variables that are shared across lambdas.

In the test below I would assume myVar would be 3 after it's assigned inside the lambda, but instead this returns 5. Not sure if this should be throwing NotSupportedExpressionException similar to: ParameterIsNotVariableNorInPassedParameters

[Test]
public void Nested_lambda_with_shared_variable()
{
    var myVar = Variable( typeof( int ), "myVar" );
    Expression<Action<Action>> invokeParamLambda = ( lambda ) => lambda();

    var withNestedLambda = Lambda<Func<int>>( 
        Block( 
            [myVar],
            Assign( myVar, Constant( 5 ) ),
            Invoke( invokeParamLambda, 
                Lambda<Action>( Assign( myVar, Constant( 3 ) ) ) 
            ),
            myVar
        )
    );
    var compileLambda = withNestedLambda.Compile();
    var result = compileLambda();  // returns 3

    var fastCompileLambda = withNestedLambda.CompileFast( false, CompilerFlags.ThrowOnNotSupportedExpression );
    var fastResult = fastCompileLambda();  // returns 5
        
    Assert.AreEqual( result, fastResult );
}

Thanks for the great library!

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions