Fix rebuilding types with circular references (#5623). #5637
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 problem reported in #5623 using the observation that if we are re-building a type that already exists in the type pool, we should just return that type.
This makes type rebuilding more efficient, and it also prevents the type builder from getting itself into infinite recursion (as reported in this issue).
In fixing this, I found a couple of other bugs in the type builder:
When rebuilding an Array type, we were not re-building the element type. This caused stale type references in the rebuilt type.
This bug had not been caught by the test, because the test itself had a bug in it: the test was rebuilding types on top of the same ID (the ID counter was never incremented).
Initially, the bug in the test caused a failure with the new logic in the builder because we now return types from the pool directly, which causes a failure when two incompatible types are registered under the same ID.
Fixing that issue in the test exposed another bug in the rebuilder: we were not re-building the element type for Array types. This was causing a stale type reference inside Array types which was later caught by the type removal logic in the test.