Skip to content

JIT: (bug) Enum.Equals folding returns true for different shared-generic nested enum types #134890

Closed

Description

@EgorBo

In shared generic code, Enum.Equals is folded to a payload compare even when the two enum types differ (C<string>.E vs C<object>.E).

Minimal Repro

using System;
using System.Runtime.CompilerServices;

public class C<T> { public enum E { A, B } }

public class Program
{
    public static void Main()
    {
        Console.WriteLine(Test<string, object>(C<string>.E.A, C<object>.E.A));
    }

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static bool Test<T, U>(C<T>.E x, C<U>.E y) => x.Equals(y);
}

Expected

False

Actual

True

(DOTNET_JitMinOpts=1 prints False.)

Regression?

Yes, regression in .NET 11 (introduced by #122779). .NET 8.0, 9.0 and 10.0 print False. .NET 11.0-rc2 and main print True.

Notes

gtFoldExprCall (NI_System_Enum_Equals) compares canonical class handles (both are C<__Canon>.E) and folds the call to a payload compare. It needs an eeIsSharedInst bailout (or an exact-type check), like the one in gtOptimizeEnumHasFlag.

Activity

  1. added this to the 12.0.0 milestone on Sep 29, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 29, 2026
  3. dotnet-policy-service commented on Sep 29, 2026

    @dotnet-policy-service
    Contributor

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

  4. added a commit that references this issue on Oct 1, 2026
    b74dfca
  5. locked and limited conversation to collaborators on Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions