dotnet/roslyn

Incorrect IDE0007: Use 'var' instead of explicit type when the type implements multiple instantiations of a generic interface which influence overload resolution

開放

#59,337 建立於 2022年2月7日

 (0 則留言) (0 個反應) (0 位負責人)C# (4,257 個分叉)batch import
Area-IDEBugFeature - IDE0007help wanted

倉庫指標

星標
 (20,414 顆星)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 256 個 PR)

描述

Version Used: Visual Studio 2022 (17.0.5) C# Tools 4.0.1-1.21568.1+6ab6601178d9fba8c680b56934cd1742e0816bff

Steps to Reproduce:

  1. Set csharp_style_var_when_type_is_apparent = true:suggestion in your .editorconfig
  2. Enter the following code:
public interface ITyped<T>
{
}
public interface IUntyped
    : ITyped<string>
    , ITyped<int>
{
}
public static class Extensions
{
    public static void Run()
    {
        IUntyped o = null;
        o.Invokee(out string value); // HERE
        _ = value;
    }
    public static void Invokee(
        this IUntyped o)
    {
        _ = o;
    }

    public static void Invokee<T>(
        this ITyped<T> o,
        out T value)
    {
        _ = o;
        value = default;
    }
}

Expected Behavior:

No codefix offered on the line marked // HERE.

Actual Behavior:

A diagnostic+codefix “IDE0007: use 'var' instead of explicit type” is offered on the line marked // HERE. Accepting replaces string with `var, breaking compilation. Compilation is broken because, without specifying the type of the parameter, the two conflicting generic interfaces are excluded during method resolution—and if they were not ignored but considered possible, there would be an ambiguity error (but that doesn’t seem to be how C# works).

貢獻者指南