Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

generic type annotation - #2478

Merged
LakshanF merged 16 commits into
dotnet:feature/damAnalyzerfrom
LakshanF:DamGenericsTestPort
Feb 1, 2022
Merged

LakshanF merged 16 commits into
dotnet:feature/damAnalyzerfrom
LakshanF:DamGenericsTestPort

Conversation

@LakshanF

@LakshanF LakshanF commented Jan 4, 2022 •

Copy link
Copy Markdown
Contributor

Initial effort for #2364.

Using Syntax API and refactored GenericParameterDataFlow test into 2 tests

Switched to registering RegisterSyntaxNodeAction to process INamedTypeSymbol and IMethodSymbol for processing generic parameter warnings. Also refactored the GenericParameterDataFlow by keeping test scenarios that handle MakeGenericXXX (that is awaiting Intrinsics) in the original test and the rest of test scenarios to GenericParameterDataFlowWithoutMakeGeneric and enabled that test in DataFlowTests.

Some comments on exceptions,

  • No warning (x2) generated for constructors for generic derived class (does generate for the derived class). These warnings are excluded from Analyzer since Linker can see the base ctor call in the IL.

DerivedTypeWithOpenGenericOnBase does not generate a warning 2091.

PartialyInstantiatedMethods - does not generate a warning 2091.

  • Warning message content (x3) was slightly different between the linker and analyzer and was aligned

InnerTypeWithNoAddedGenerics's

StaticPartialInstantiationUnrecognized

Analyzer warning is on PublicMethodsProperty instead of the getter and setter

  • Constraint warning (x3) generated were specially handled similarly to the linker but further down the stack,

Warning 2091 generated for RequiresParameterlessConstructor and are suppressed for following tests: TestNewConstraintSatisfiesParameterlessConstructor, TestStructConstraintSatisfiesParameterlessConstructor, TestUnmanagedConstraintSatisfiesParameterlessConstructor

@vitek-karas vitek-karas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - but please wait for Sven or Andy to take another look as well.

@vitek-karas

Copy link
Copy Markdown
Member

@sbomer

Personally I would like to produce warnings even for generic types inside nameof - even though this might be noisier than the linker. I think it makes sense to think of the generic annotations as an extension of the type system, and to warn for any offending reference an instantiated type. It's a little painful for the linker repo but I don't know if it will be common in the wild. I think this is similar to local variable declarations where (IIRC) the linker would not warn but the analyzer would. @vitek-karas do you have an opinion on this?

The testing problem can be solved - we could modify our validator to ignore warnings from within test attributes (assuming there's a reasonable way to detect them).
So in that sense I don't mind.

I just checked and Roslyn does raise warnings for generic constraints even inside nameof:

Console.WriteLine(nameof(WithNewConstraint<WithoutNew>)); // CS0310 ....

So it would be good to follow suite I guess.

In general I still think that analyzer should not report warnings which the linker doesn't - mainly because if there's a warning which we think the analyzer SHOULD report and the linker doesn't -> it's almost certainly a bug in the linker.
This is a specific case though, since it's at the level the linker doesn't have visibility into - and it might help developers. So in this case I think it would be good, but only in this case 😉.

@agocke

agocke commented Jan 31, 2022

Copy link
Copy Markdown
Member

I'd be fine generating a warning here, just because it seems like it might be simpler to implement and understand.

@LakshanF

LakshanF commented Jan 31, 2022 •

Copy link
Copy Markdown
Contributor Author

I agree this is a grey area but fall on the side of (slightly) preferring not to warn. My reasoning is that this warning will cause developer confusion (its not intuitive to anyone that a compiler enhancement used to generate a string will have trimming implications) and cost time with the warning for no real benefit. After the developer understands the warning, I believe they will almost always result in taking no positive action but workarounds (as in use the literal string for example) circumventing the good programming practice nameof was meant to add.

Given the views expressed above for generating warning are not strong, I will go with the no-warn option

@agocke

agocke commented Jan 31, 2022

Copy link
Copy Markdown
Member

To be clear, I'm fine either way 😄

@LakshanF
LakshanF merged commit 56421a1 into dotnet:feature/damAnalyzer Feb 1, 2022
@LakshanF
LakshanF deleted the DamGenericsTestPort branch February 1, 2022 00:31
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
* generic type annotation - take 1

* fb

* FB2

* Using Syntax API and refactored GenericParameterDataFlow test into 2

* incorprate FB

* fix remaining issues

* CI build break fix

* FB

* FB

* FB

* enhanced comments

Commit migrated from dotnet/linker@56421a1
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants