-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Ensure an empty run result doesn't throw when generators are present. #74034
Conversation
@dotnet/roslyn-compiler for reviews please :) |
@@ -39,7 +39,7 @@ internal GeneratorDriver(GeneratorDriverState state) | |||
internal GeneratorDriver(ParseOptions parseOptions, ImmutableArray<ISourceGenerator> generators, AnalyzerConfigOptionsProvider optionsProvider, ImmutableArray<AdditionalText> additionalTexts, GeneratorDriverOptions driverOptions) | |||
{ | |||
var incrementalGenerators = GetIncrementalGenerators(generators, SourceExtension); | |||
_state = new GeneratorDriverState(parseOptions, optionsProvider, generators, incrementalGenerators, additionalTexts, ImmutableArray.Create(new GeneratorState[generators.Length]), DriverStateTable.Empty, SyntaxStore.Empty, driverOptions, runtime: TimeSpan.Zero, parseOptionsChanged: true); | |||
_state = new GeneratorDriverState(parseOptions, optionsProvider, generators, incrementalGenerators, additionalTexts, generators.SelectAsArray(g => GeneratorState.Empty), DriverStateTable.Empty, SyntaxStore.Empty, driverOptions, runtime: TimeSpan.Zero, parseOptionsChanged: true); |
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.
Could you please explain how this change prevents an NRE which was occurring in the original code?
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.
I'm guessing it's in getGeneratorSources
access to generatorState.PostInitTrees.Length
. The value for PostInitTrees
is a default immutable array for default(GeneratorState)
, as opposed to an empty immutable array for GeneratorState.Empty
.
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.
When calling .GetRunResult()
we call into a local function getGeneratorSources
that creates an array to handle the generated sources of the given generator
ArrayBuilder<GeneratedSourceResult> sources = ArrayBuilder<GeneratedSourceResult>.GetInstance(generatorState.PostInitTrees.Length + generatorState.GeneratedTrees.Length); |
That calls into the collections of the individual states to get their sizes. Prior to the change, we created default
GeneratorState
s, so the PostInitTrees
and GeneratedTrees
were uninitialized immutable arrays which throw when attempting to get their length.
Instead we now store GeneratorState.Empty
instances, which in turn have empty result arrays rather than uninitialized ones.
We had a test that checked you could call GetRunResult
before calling RunGenerators
but that test didn't add any generators, so the generatorStates
array was empty. Adding a generator ensures that array has content, and we see the issue in the added test.
So, the change itself looks fine to me, but there are a lot of failures in CI here. Is something not merging correctly? |
Test failures seem weird and unrelated, kicked off a re-run and will investigate further if we still see it. |
Seems like some investigation may be required. |
All the bootstrap legs broke with this message. Guessing something fairly fundamental about generators broke with this change. |
Yeah, |
- Fix replace generators not running init for new generators and add a test
Turns out a bunch of generator tests were failing; they were just hiding amongst the mass of other issues that I didn't see them. @dotnet/roslyn-compiler for reviews please :) |
@dotnet/roslyn-compiler for review please :) |
@333fred, @RikkiGibson PTAL |
…dotnet#74034) * Ensure an empty run result doesn't throw when generators are present. * Fix replace generators not running init for new generators and add a test
Fixes #74033