dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.89k stars 4.01k forks source link

Type inference with method groups does not support variance for signatures with ref parameters #55909

Open cston opened 3 years ago

cston commented 3 years ago

Compile:

class Program
{
    static void F1<T>(T t, object o) { }
    static void F2<T>(T t, ref object o) { }

    static void Main()
    {
        _ = new[] { F1<object>, F1<string> }; // ok
        _ = new[] { F2<object>, F2<string> }; // error CS0826: No best type found for implicitly-typed array
    }
}

Expected: Compiler infers a common delegate signature void *(object, ref object) for the second array.

Actual: (9,13): error CS0826: No best type found for implicitly-typed array

Relates to test plan https://github.com/dotnet/roslyn/issues/52192

cston commented 2 years ago

The type parameters for synthesized delegate types should have in variance for parameters passed by value and out variance for out parameters and return type.