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

Correctly supress/elevate diagnostics produced by source generators #52544

Merged
merged 5 commits into from
Apr 13, 2021

Conversation

chsienki
Copy link
Contributor

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
@@ -438,6 +440,20 @@ private static GeneratorState SetGeneratorException(CommonMessageProvider provid
return new GeneratorState(generatorState.Info, e, diagnostic);
}

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

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
Contributor 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
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