dotnet/roslyn

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

Open

#4,248 创建于 2015年7月30日

在 GitHub 查看
 (0 评论) (0 反应) (0 负责人)C# (20,414 star) (4,257 fork)batch import
Area-CompilersBugConcept-Diagnostic ClarityLanguage-C#Pedantic ;)help wanted

描述

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.

贡献者指南