Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# compiler should report CS7003 (or similar) when call to IServiceProvider.GetService<T>() is missing generic type argument and is followed by another call #41779

Closed
jpd30 opened this issue Feb 19, 2020 · 5 comments
Assignees
Milestone

Comments

@jpd30
Copy link

jpd30 commented Feb 19, 2020

Version Used:

Microsoft .NET Core SDK 3.1.100 (x64)
Microsoft Visual Studio Community 2019 16.4.2

Steps to Reproduce:

Build the attached project FailedToEmitModule.zip
, or:

  1. Create a new .NET Core console app
  2. Add <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.2" /> to the .csproj file
  3. Copy the following into Program.cs:
using System;
using Microsoft.Extensions.DependencyInjection;

namespace FailedToEmitModule
{
    public static class Program
    {
        public static void Main()
        {
            IServiceProvider serviceProvider = null;
            var foo = serviceProvider.GetService<>().Foo();
        }
    }
}
  1. Build the project

Expected Behavior:

Build error along the lines of:

Program.cs(11,8,11,44): error CS7003: Unexpected use of an unbound generic name

This behaviour can be observed by removing the call to Foo().

Actual Behavior:

Build error:

CSC : error CS7038: Failed to emit module 'FailedToEmitModule'
@jaredpar jaredpar added this to the 16.6 milestone Feb 19, 2020
@jpd30 jpd30 changed the title C# compiler should report CS7003 (or similar) when call to IServiceProvider.GetService<T>() is missing generic argument and is followed by another call C# compiler should report CS7003 (or similar) when call to IServiceProvider.GetService<T>() is missing generic type argument and is followed by another call Feb 20, 2020
@jpd30
Copy link
Author

jpd30 commented Feb 20, 2020

CS0305 might be a better bet

#6236 is similar (and still Open, although the scenario described by that issue appears to have been resolved)

@RikkiGibson
Copy link
Contributor

Here's a minimal repro:

interface IServiceProvider
{
    object GetService();
}

static class Program
{
    static T GetService<T>(this IServiceProvider obj) => default;
    
    static void M(IServiceProvider provider)
    {
        var service = provider.GetService<>().INVALID();
    }
}

I feel like I have seen a similar bug like this in the past year but I can't find it.

@RikkiGibson
Copy link
Contributor

It feels like it's easy for analysis of calls like these to fall into a hole in conversions or overload resolution or wherever else. But these kinds of generic names are only allowed in 'typeof' expressions. Maybe when we bind such a name we could just check if it is contained in a typeof expression and if not give a diagnostic.

@kevinsun-dev
Copy link
Contributor

Fixed in #45033, thanks for reporting the issue @jpd30!

@jpd30
Copy link
Author

jpd30 commented Jun 25, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants