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

Add new analyzer for deprecating implementing an interface #7419

Merged
merged 1 commit into from
Sep 27, 2024

Conversation

333fred
Copy link
Member

@333fred 333fred commented Sep 24, 2024

We're officially marking ISourceGenerator as deprecated, but we can't actually deprecate the interface, because it's used in several APIs like AnalyzerFileReference. So instead, we just add an analyzer to indicate that the interface should not be implemented. I've made this more general so we can do this with more interfaces in the future if necessary, based the implementation on InternalImplementationOnlyAnalyzer.

We're officially marking `ISourceGenerator` as deprecated, but we can't actually deprecate the interface, because it's used in several APIs like AnalyzerFileReference. So instead, we just add an analyzer to indicate that the interface should not be implemented. I've made this more general so we can do this with more interfaces in the future if necessary, based the implementation on InternalImplementationOnlyAnalyzer.
@333fred 333fred requested a review from a team as a code owner September 24, 2024 20:31
333fred added a commit to 333fred/roslyn that referenced this pull request Sep 24, 2024
Analyzer that will flag implementation is dotnet/roslyn-analyzers#7419. We don't just mark `ISourceGenerator` as obsolete directly because we do use it in public APIs like `AnalyzerFileReference` and `GeneratorDriver`, and we can't remove it from there.
Copy link

codecov bot commented Sep 24, 2024

Codecov Report

Attention: Patch coverage is 99.54955% with 1 line in your changes missing coverage. Please review.

Project coverage is 96.50%. Comparing base (f4c8475) to head (0480b7f).
Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #7419    +/-   ##
========================================
  Coverage   96.50%   96.50%            
========================================
  Files        1443     1445     +2     
  Lines      346376   346598   +222     
  Branches    11387    11390     +3     
========================================
+ Hits       334263   334488   +225     
+ Misses       9232     9229     -3     
  Partials     2881     2881            

// CodeAnalysis.dll itself has this attribute and if the user assembly also had it, symbol equality will fail
// but we should still issue the error.
if (attributes.FirstOrDefault(a => a is { AttributeClass.Name: ImplementationIsObsoleteAttributeName, ConstructorArguments: [{ Value: string }] }
&& a.AttributeClass.ToDisplayString().Equals(ImplementationIsObsoleteAttributeFullName, StringComparison.Ordinal)) is { } attr &&
Copy link
Member

Choose a reason for hiding this comment

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

a.AttributeClass.ToDisplayString()

This probably allocates. Consider doing part-by-part comparison

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a copy/paste from our existing DoNotImplementInternalInterfaces analyzer (the one we use to flag IOperation/ISymbol). If this was a perf problem, I would expect that to have been updated a while ago.

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

LGTM Thanks (iteration 1) with minor comments to consider

@333fred 333fred merged commit ef8dad3 into dotnet:main Sep 27, 2024
14 checks passed
@333fred 333fred deleted the implementationobsolete branch September 27, 2024 19:10
</data>
<data name="ImplementationIsObsoleteMessage" xml:space="preserve">
<value>Type {0} cannot implement interface {1} because {1} is obsolete for implementation. See {2} for more details.</value>
Copy link
Member

Choose a reason for hiding this comment

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

📝 Placeholders should be surrounded by ' (single quotes)

333fred added a commit to dotnet/roslyn that referenced this pull request Oct 5, 2024
* Mark ISourceGenerator as obsolete for implementation

Analyzer that will flag implementation is dotnet/roslyn-analyzers#7419. We don't just mark `ISourceGenerator` as obsolete directly because we do use it in public APIs like `AnalyzerFileReference` and `GeneratorDriver`, and we can't remove it from there.

* Suppress warnings

* Fix attribute name

* Update Microsoft.CodeAnalysis.Analyzers

* Address warnings
AptiviCEO added a commit to Aptivi/Terminaux that referenced this pull request Oct 6, 2024
---

There was a breaking change between Microsoft.CodeAnalysis.Analyzers 3.3.4 and 3.11.0 that necessitated the usage of IIncrementalGenerator according to https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md, https://github.com/dotnet/roslyn/blob/main/docs/features/incremental-generators.md, and dotnet/roslyn-analyzers#7419. As a result, we've made necessary changes to achieve the same result, cleaning up clutter in the process.

---

Type: imp
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants