Skip to content

VM: (bug) open virtual delegates compare equal but have different hash codes #134218

Closed

Description

@EgorBo

Description

Two open delegates created over the same virtual slot (from Base.M and from Derived.M) behave inconsistently on main: Release reports Equals == true with different hash codes, violating the Equals/GetHashCode contract and breaking dictionary lookups; Checked reports Equals == false. .NET 10.0.12 is self-consistent.

Repro

using System;
using System.Collections.Generic;

public class Base { public virtual void M() { } }
public class Derived : Base { public override void M() { } }

public static class Program
{
    public static void Main()
    {
        var d1 = (Action<Derived>)Delegate.CreateDelegate(typeof(Action<Derived>), typeof(Base).GetMethod("M")!);
        var d2 = (Action<Derived>)Delegate.CreateDelegate(typeof(Action<Derived>), typeof(Derived).GetMethod("M")!);

        Console.WriteLine($"Equals={d1.Equals(d2)} hashEq={d1.GetHashCode() == d2.GetHashCode()}");

        var dict = new Dictionary<Action<Derived>, int> { { d1, 1 } };
        Console.WriteLine($"ContainsKey(d2)={dict.ContainsKey(d2)}");
    }
}

Expected

Equals=True hashEq=True
ContainsKey(d2)=True

Actual

main Release:

Equals=True hashEq=False
ContainsKey(d2)=False

main Checked:

Equals=False hashEq=False
ContainsKey(d2)=False

Analysis

In Release, Delegate.Equals treats matching _methodPtrAux virtual-dispatch stubs as equal, and that stub is keyed by slot, so Base.M and Derived.M produce the same stub. Delegate.GetHashCode instead hashes the MethodDesc, which stays distinct. Checked compiles out the _methodPtrAux shortcut and returns false from Equals.

Either way, Equals and GetHashCode must agree.

Regression?

Yes. .NET 10.0.12 reports Equals=True hashEq=True and finds the dictionary key.

Platform

Windows x64, CoreCLR main (Checked and Release); .NET 10.0.12 is unaffected.

Activity

  1. added this to the 12.0.0 milestone on Sep 18, 2026
  2. dotnet-policy-service commented on Sep 18, 2026

    @dotnet-policy-service
    Contributor

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

  3. MichalPetryka commented on Sep 18, 2026

    @MichalPetryka
    Contributor

    @jkotas Should CreateDelegate normalize the derived case to the more concrete method when creating the delegate? I'd expect either that or for Equals to be false here since d1.Method == d2.Method would be.

  4. jkotas commented on Sep 18, 2026

    @jkotas
    Member

    var d1 = (Action)Delegate.CreateDelegate(typeof(Action), typeof(Base).GetMethod("M")!);
    var d2 = (Action)Delegate.CreateDelegate(typeof(Action), typeof(Derived).GetMethod("M")!);

    I would expect d1.Equals(d2) to be false. The two delegates are not pointing to the same method.

  5. jkotas commented on Sep 18, 2026

    @jkotas
    Member

    @MichalPetryka Could you please create a PR with the fix? This is regression from last release, so it is a backport candidate.

  6. added a commit that references this issue on Sep 18, 2026
    162f108
  7. added a commit that references this issue on Sep 19, 2026
    7adc767
  8. locked and limited conversation to collaborators on Oct 19, 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

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions