dotnet/roslyn

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

Open

#59,337 opened on Feb 7, 2022

View on GitHub
 (0 comments) (0 reactions) (0 assignees)C# (4,257 forks)batch import
Area-IDEBugFeature - IDE0007help wanted

Repository metrics

Stars
 (20,414 stars)
PR merge metrics
 (Avg merge 6d 17h) (256 merged PRs in 30d)

Description

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).

Contributor guide