dotnet/roslyn

C# overload resolution diagnostic involving generic extension methods could be more clear

Open

#4,248 opened on Jul 30, 2015

View on GitHub
 (0 comments) (0 reactions) (0 assignees)C# (20,414 stars) (4,257 forks)batch import
Area-CompilersBugConcept-Diagnostic ClarityLanguage-C#Pedantic ;)help wanted

Description

This is a follow-up to #2985.

When compiling the code

using FluentAssertions;
using Extensions;
using System;
using System.Collections.Generic;
using System.Collections;

namespace FluentAssertions
{
    public static class AssertionExtensions
    {
        public static dynamic Should(this object actualValue) { throw null; }
        public static dynamic Should(this IEnumerable actualValue) { throw null; }
        public static dynamic Should<T>(this IEnumerable<T> actualValue) { throw null; }
        public static dynamic Should<TKey, TValue>(this IDictionary<TKey, TValue> actualValue) { throw null; }
    }
}

namespace Extensions
{
    public static class TestExtensions
    {
        public static dynamic Should<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> actualValue) { throw null; }
    }
}

namespace ClassLibraryOverloadResolution
{    
    public class Class1
    {
        void foo()
        {
            Dictionary<String, String> dict = null;
            dict.Should().ContainKey("");
        }
    }
}

The desired diagnostic (and what the old compiler produces) is something like

error CS0121: The call is ambiguous between the following methods or properties: 'Extensions.TestExtensions.Should<string,string>(System.Collections.Generic.IReadOnlyDictionary<string,string>)' and 'FluentAssertions.AssertionExtensions.Should<string,string>(System.Collections.Generic.IDictionary<string,string>)'

The actual diagnostic is

Error CS0121: The call is ambiguous between the following methods or properties: 'FluentAssertions.AssertionExtensions.Should<TKey, TValue>(System.Collections.Generic.IDictionary<TKey, TValue>)' and 'Extensions.TestExtensions.Should<TKey, TValue>(System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>)'

but the types TKey and TValue are not in scope. We should be reporting the diagnostic on the instantiated extension methods (as previous compilers did) rather than on the generic extension methods.

Contributor guide

C# overload resolution diagnostic involving generic extension methods could be more clear · dotnet/roslyn#4248 | Good First Issue