Skip to content

InvalidProgramException with System.Linq.Expressions and user-defined conditional operators #103197

Closed

Description

@MarkMpn

Description

Using System.Linq.Expressions to build expression trees dynamically, the repro code worked as expected in .NET Framework but fails in .NET (tested .NET 6 & .NET 8)

So far I've managed to narrow this down to when I'm using a type that implements custom conditional operators (true, false, & and |) and using a TryCatch expression on the right hand side of the conditional logic operator (AndAlso or OrElse). If I reorder the arguments so the TryCatch expression is on the left hand side of the operator it works as expected.

Reproduction Steps

using System.Data.SqlTypes;
using System.Linq.Expressions;

var expr = Expression.OrElse(
    Expression.Constant(SqlBoolean.True),
    Expression.TryCatch(
        Expression.Constant(SqlBoolean.True),
        Expression.Catch(
            typeof(SqlTypeException),
            Expression.Constant(SqlBoolean.False)
        )
    )
);
var lambda = Expression.Lambda<Func<SqlBoolean>>(expr);
var func = lambda.Compile();
Console.WriteLine(func());

Expected behavior

The expression should compile and True should be printed to the console.

Actual behavior

A System.InvalidProgramException is thrown by lambda.Compile()

Regression?

Yes - this same code runs as expected in .NET Framework 4.6.2 and 4.8

Known Workarounds

No response

Configuration

.NET 6 and .NET 8
Windows 11 22631.3593
x64
Doesn't appear to be specific to this.

Other information

No response

Activity

  1. added
    untriaged
    on Jun 8, 2024
  2. dotnet-policy-service commented on Jun 8, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @cston
    See info in area-owners.md if you want to be subscribed.

  3. EgorBo commented on Jun 11, 2024

    @EgorBo
    Member

    I presume in C# it would look like condition || try { ... . Checked JIT reports your snippet as an invalid program due to:

    Evaluation stack must be empty on entry into a try block
    

    My understanding that it is an expected behavior according to ECMA-335 I.12.4.2.8.1 Fall Through:

    Entry to protected blocks can be accomplished by fall-through, at which time the evaluation
    stack shall be empty.
    

    so I am not sure why and how it used to work in .NET Framework.

    so the TryCatch expression is on the left hand side of the operator it works as expected.

    if it's on the left side then the evaluation stack is empty

    cc @jkotas

  4. jkotas commented on Jun 11, 2024

    @jkotas
    Member

    I am not sure why and how it used to work in .NET Framework.

    The IL produced by the expression compiler on .NET Framework has an extra spill to make the stack empty. It looks like a regression in System.Linq.Expressions to me.

  5. jkotas commented on Jun 11, 2024

    @jkotas
    Member

    Note that we merged a ton of optimizations in S.L.Expressions in the early .NET Core days: https://github.com/dotnet/corefx/pulls?q=label%3Aarea-System.Linq.Expressions+is%3Aclosed+optimize . It is likely one of those of changes.

  6. jkotas commented on Jun 26, 2024

    @jkotas
    Member

    It's the changes in LambdaCompiler.EmitMethodAndAlso and LambdaCompiler.EmitMethodOrElse in this commit: 2ecfbbd

    cc @VSadov

  7. added this to the 9.0.0 milestone on Jul 29, 2024
  8. removed
    untriaged
    on Jul 29, 2024
  9. modified the milestones: 9.0.0, 10.0.0 on Sep 9, 2024
  10. added
    in-pr
    on Dec 15, 2024
  11. modified the milestones: 10.0.0, Future on Jul 31, 2025
  12. added a commit that references this issue on Jun 24, 2026
    37d45de
  13. added a commit that references this issue on Jul 15, 2026
    f3e2ab7
  14. locked and limited conversation to collaborators on Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Linq.Expressionsin-pr

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions