Skip to content

Comparers don't work for messy generic corner cases #89767

Closed

Description

Compile the repro below with/without optimizations.

Without optimizations, this prints:

Here
False
True

That's good.

With optimizations, this prints:

Here
False
Here
False

That's not so good.

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
{
    MyStruct<object, object> s1 = default;
    MyStruct<object, object> s2 = default;
    Console.WriteLine(Equals(s1, s2));
}

{
    MyStruct<object, string> s1 = default;
    MyStruct<object, string> s2 = default;
    Console.WriteLine(Equals(s1, s2));
}

[MethodImpl(MethodImplOptions.NoInlining)]
static bool Equals<T>(T x, T y)
{
    return EqualityComparer<T>.Default.Equals(x, y);
}

struct MyStruct<T, U> : IEquatable<MyStruct<U, U>>
{
    public bool Equals(MyStruct<U, U> other)
    {
        Console.WriteLine("Here");
        return false;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions