[generics] Fixed collecting instances from inverted dependencies #1384
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes the issue causing the collector to miss instances.
Problem
The problem was that we'd scan package X, then check if there are any unprocessed instances in the X package instance set before moving to the next package. However, with generics, package X could contain
Foo[T any]
that isn't instantiated anywhere within package X, then package Y could callFoo[int]
causing the X package instance set to have an unprocessed instance in it.Foo[int]
would never be processed because we had moved onto package Y.This wouldn't be a problem for
Foo[int]
because it still got into the instance set for X. However, if inside ofFoo[T]
was the instantiationBar[T]
andBar[T]
isn't instantiated anywhere else, then we'd have to processFoo[int]
to discover the instanceBar[int]
. SinceFoo[int]
would never be processed,Bar[int]
would be missing.This is caused by how generics can invert dependencies. We process packages based on import order, but with generics, package Y could import X and therefore is scanned after X, however Y could create instances in X, thus inverting the dependencies because X now depends on instance information defined in Y.
Fix
To fix this problem I moved all the processing of unprocessed instances until after all packages were scanned. The processing is allowed to recheck packages multiple times until every instance added before and added during processing has been processed (e.g. If
Bar[T]
usedBaz[T]
thenFoo[int]
being processed would addBar[int]
that would need to be processed to getBaz[int]
and so on).I also fixed some spelling and some odd spacing in the code.
This is related to #1013 and #1270