You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Add <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.2" /> to the .csproj file
Copy the following into Program.cs:
using System;using Microsoft.Extensions.DependencyInjection;namespaceFailedToEmitModule{publicstaticclassProgram{publicstaticvoidMain(){IServiceProviderserviceProvider=null;varfoo= serviceProvider.GetService<>().Foo();}}}
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'
The text was updated successfully, but these errors were encountered:
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
interfaceIServiceProvider{objectGetService();}staticclassProgram{static T GetService<T>(thisIServiceProviderobj)=>default;staticvoidM(IServiceProviderprovider){varservice= provider.GetService<>().INVALID();}}
I feel like I have seen a similar bug like this in the past year but I can't find it.
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.
Thanks very much for the fix - really appreciate the effort you and the
team put in to ensure error messages are clear and lead straight to the
offending code, even in relatively obscure cases such as this.
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:
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.2" />
to the.csproj
fileProgram.cs
:Expected Behavior:
Build error along the lines of:
This behaviour can be observed by removing the call to
Foo()
.Actual Behavior:
Build error:
The text was updated successfully, but these errors were encountered: