Skip to content

[generics] Fixed collecting instances from inverted dependencies #1384

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

Merged
merged 1 commit into from
Aug 1, 2025

Conversation

grantnelson-wf
Copy link
Collaborator

@grantnelson-wf grantnelson-wf commented Jul 31, 2025

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 call Foo[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 of Foo[T] was the instantiation Bar[T] and Bar[T] isn't instantiated anywhere else, then we'd have to process Foo[int] to discover the instance Bar[int]. Since Foo[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] used Baz[T] then Foo[int] being processed would add Bar[int] that would need to be processed to get Baz[int] and so on).

I also fixed some spelling and some odd spacing in the code.

This is related to #1013 and #1270

@grantnelson-wf grantnelson-wf self-assigned this Jul 31, 2025
@grantnelson-wf grantnelson-wf marked this pull request as ready for review July 31, 2025 19:14
@grantnelson-wf grantnelson-wf merged commit a87964c into gopherjs:master Aug 1, 2025
10 checks passed
@grantnelson-wf grantnelson-wf deleted the collectImplicitInstances branch August 1, 2025 20:42
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.

2 participants