Skip to content

Targets of variant static interface method resolution #71319

@MichalStrehovsky

Description

@MichalStrehovsky

For the following program:

using System;

interface IFoo<in T>
{
    static virtual void DoStatic() => Console.WriteLine(typeof(IFoo<T>));
}

class Program : IFoo<object>
{
    static void CallStatic<T, U>() where T : IFoo<U> => T.DoStatic();

    static void Main()
    {
        CallStatic<Program, string>();
    }
}

My expectation is that we're doing a variant dispatch to IFoo<object> implemented on Program. But unexpectedly, the program prints IFoo`1[System.String]. Somehow we dispatched to an interface Program doesn't implement.

If I modify the program to not use default interface methods, the program prints object as expected.

using System;

interface IFoo<in T>
{
    static abstract void DoStatic();
}

class Fooer<T> : IFoo<T>
{
    public static void DoStatic() => Console.WriteLine(typeof(T));
}

class Program
{
    static void CallStatic<T, U>() where T : IFoo<U> => T.DoStatic();

    static void Main()
    {
        CallStatic<Fooer<object>, string>();
    }
}

Cc @trylek @davidwrighton

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions