Fix #7512: Normalize type arguments before instantiation #14259
Merged
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 PR fixes a long-lasting issue with type normalization that led to unnecessary exponential compilation time. I recently wrote some match type benchmarks and managed to reproduce this issue in a more controlled environment, which lead to this PR.
To summarize the issue, which is hard to observe without looking at the compiler internal, here is a simple example that exercises the exponential behavior:
The issue with this code is we try to normalize
B + 1
first too early, and then too late. We normalize type at creating, but at that pointB
is abstract andB + 1
cannot be normalized. Then, we normalize during subtyping, but that's a bit late. Here are the queries that will be sent to type comparer forGEQ[5, 1]
:Which results in a quadratic number of additions being computer... The solution is to normalize types when doing substitution, so that each addition is computed once and for all at each set of the recursion.
Fixes #7512