-
Notifications
You must be signed in to change notification settings - Fork 480
New Analyzer: Implement Generic Math Interfaces Correctly #6126
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
Conversation
.../UnitTests/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectlyTests.cs
Outdated
Show resolved
Hide resolved
.../UnitTests/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectlyTests.cs
Show resolved
Hide resolved
.../UnitTests/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectlyTests.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectly.cs
Show resolved
Hide resolved
.../UnitTests/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectlyTests.cs
Outdated
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectly.cs
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectly.cs
Outdated
Show resolved
Hide resolved
...rs/CSharp/Microsoft.NetCore.Analyzers/Usage/CSharpImplementGenericMathInterfacesCorrectly.cs
Outdated
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectly.cs
Outdated
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectly.cs
Outdated
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectly.cs
Outdated
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectly.cs
Outdated
Show resolved
Hide resolved
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #6126 +/- ##
==========================================
+ Coverage 96.02% 96.03% +0.01%
==========================================
Files 1343 1351 +8
Lines 309474 310585 +1111
Branches 9950 9986 +36
==========================================
+ Hits 297184 298283 +1099
- Misses 9890 9901 +11
- Partials 2400 2401 +1 |
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
.../UnitTests/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectlyTests.cs
Outdated
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectly.cs
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectly.cs
Outdated
Show resolved
Hide resolved
4078d05 to
0b94d3b
Compare
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectly.cs
Outdated
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectly.cs
Outdated
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectly.cs
Outdated
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectly.cs
Show resolved
Hide resolved
...tAnalyzers/Core/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectly.cs
Show resolved
Hide resolved
eerhardt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This LGTM. Thanks for the great work here @buyaa-n.
tannergooding
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests coverage looks to cover the scenarios that I was thinking about and the code generally makes sense to me. Not an expert in reviewing analyzer code, however :)
...rs/CSharp/Microsoft.NetCore.Analyzers/Usage/CSharpImplementGenericMathInterfacesCorrectly.cs
Show resolved
Hide resolved
|
Thanks for the thorough review @Youssef1313 @eerhardt @tannergooding @mavasani @jmarolf @JoeRobich this one of the new analyzers we want to merge in 7.0, please take a look and let us know if we need to get any additional approval. The analyzer severity is warning but it is only apply for new APIs added in 7.0 (some of them have been a preview feature in 6.0), therefore I would not expect it will broke people plus if they had this issue they would most likely would like to know/warned as the APIs would not function properly to if not implemented properly CC @jeffhandley |
jeffhandley
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
|
Ping @jmarolf again, we need this merged in 7.0 |
|
I am gonna merge the PR now, feel free to leave feedbacks, I will apply them separately |
* upstream/main: Localized file check-in by OneLocBuild Task: Build definition ID 830: Build ID 2004411 Run CI using the new VM images Localized file check-in by OneLocBuild Task: Build definition ID 830: Build ID 2000239 New Analyzer: Prevent behavioral change in built-in operators for IntPtr and UIntPtr (dotnet#6153) GM interfaces implementations can be self constrained (dotnet#6168) Localized file check-in by OneLocBuild Task: Build definition ID 830: Build ID 1993001 New Analyzer: Implement Generic Math Interfaces Correctly (dotnet#6126)
Fixes dotnet/runtime#69775
With Static abstract members in interfaces the Generic Math Support feature uses the Curiously Recurring Template Pattern (CRTP) to enable scenarios where interfaces can declare that a method takes or returns the concrete type that implements the interface. For example: https://github.com/dotnet/runtime/blob/6ca8c9bc0c4a5fc1082c690b6768ab3be8761b11/src/libraries/System.Private.CoreLib/src/System/IParsable.cs#L10-L27
In this example,
TSelfwill be filled by the deriving type with its own type:The Issue
An issue is that nothing is enforcing that a type implementing a curiously recurring template interface fills the generic type with its same type. For example:
The above definition will compile successfully - it is a valid definition from a language perspective. However,
MyDatecannot be passed to the aboveParseAndWrite<T>(string data)method. Trying to useMyDatein this way will result in a compiler error:Which is confusing. A similar error
CS0311is raised ifMyDateis aclass.Even worse, the
MyDatetype could have been shipped publicly in a library. And only once consumers of this library try to use it asIParsable<DateOnly>, they get the compiler error. When the real error was thatMyDatewas not defined correctly.In the future, we hope to adopt a full-fledged self-type mechanism (such as dotnet/csharplang#5413) in the Generic Math interfaces. We would like to enforce that any implementers of the Generic Math interfaces now would not be broken with the adoption of a self-type C# feature in the future.
The Analyzer Requirement:
The analyzer should be thought of as a stop gap solution for generic math until there is a C# feature.
We will have list of generic math interfaces that analyzer would use, the analyzer will flag any Type that implements one of the interfaces and fills
TSelfwith a non-generic Type other than itself. If theTSelfis filled with another generic Type, such as:The analyzer will not flag the above Type. But should flag:
The list of generic math interfaces
Severity: Warning
Category: Usage