Skip to content

Correctly supress/elevate diagnostics produced by source generators#52544

Merged
chsienki merged 5 commits intodotnet:mainfrom
chsienki:fix_52527
Apr 13, 2021
Merged

Correctly supress/elevate diagnostics produced by source generators#52544
chsienki merged 5 commits intodotnet:mainfrom
chsienki:fix_52527

Conversation

@chsienki
Copy link
Copy Markdown
Member

We were not filtering the diagnostics produced by source generators, meaning there was no way for a user to suppress them.

Fixes #52527

@chsienki chsienki added this to the 16.10 milestone Apr 10, 2021
@chsienki chsienki requested a review from a team as a code owner April 10, 2021 22:28

private static ImmutableArray<Diagnostic> FilterDiagnostics(Compilation compilation, ImmutableArray<Diagnostic> diagnostics, CancellationToken cancellationToken)
{
DiagnosticBag filteredDiagnostics = DiagnosticBag.GetInstance();
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.

A DiagnosticBag instance is somewhat heavy weight because it's designed for concurrent writes. In this case we're just building up a collection in a single threaded environment and returning it to the caller. Consider using a normal ImmutableArrayBuilder here.

Given that this is immediately consumed and written into a different bag there isn't really a need for the return to be immutable here if that adds extra cost.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks. Changed to ArrayBuilder but also passing in the overal driver bag, so we can just add them as we go rather than adding them as a bunch at the end.

- Simplify diag filterning
- add vb tests
@chsienki
Copy link
Copy Markdown
Member Author

@dotnet/roslyn-compiler for a second review please

@RikkiGibson RikkiGibson self-assigned this Apr 13, 2021
(var sources, var diagnostics) = context.ToImmutableAndFree();
stateBuilder[i] = new GeneratorState(generatorState.Info, generatorState.PostInitTrees, ParseAdditionalSources(generator, sources, cancellationToken), diagnostics);
diagnosticsBag?.AddRange(diagnostics);
(var sources, var generatorDiagnostics) = context.ToImmutableAndFree();
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.

it would be nice if we could avoid the allocation (just allocate the final array of filtered diagnostics, not the intermediate one), but I am not extremely concerned about it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@RikkiGibson Yeah I actually thought about this, but with the upcoming work we'll be collecting diagnostics from multiple places. I figured it made sense to centralize the filtering upfront now, rather than having it spread across multiple places down the line.

@chsienki chsienki merged commit d43e561 into dotnet:main Apr 13, 2021
@ghost ghost modified the milestones: 16.10, Next Apr 13, 2021
@chsienki chsienki deleted the fix_52527 branch April 13, 2021 16:42
@dibarbet dibarbet modified the milestones: Next, 16.10.P3 Apr 26, 2021
@eerhardt
Copy link
Copy Markdown
Member

@chsienki - I'm still seeing warnings in the Error List using VS Version 16.11.0 Preview 2.0 and .NET SDK 6.0.100-preview.5.21302.13.

What version did this fix make it into?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Diagnostics from Source Generators do not respect suppression

5 participants