Specify error for pseudo-cyclic type parameter bounds - #4743
Open
eernstg wants to merge 12 commits into
Open
Conversation
lrhn
reviewed
Aug 10, 2026
lrhn
approved these changes
Aug 10, 2026
| \code{X\,\,\EXTENDS\,\,FutureOr<Y>, Y\,\,\EXTENDS\,\,X} | ||
| is satisfied when all type variables are bound to a top type, | ||
| or a couple of other cases involving bottom types and \code{Object}, | ||
| which is not likely to be useful. |
Member
There was a problem hiding this comment.
Maybe not.
You can always satisfy the bounds, but if you have <X extends Y, Y extends FutureOf<X>>, you can assign Y to X, and await (x as X) to Y. You couldn't do that if you removed the bounds, and you can't just collapse them to one type variable, that wouldn't have the two different bounds.
You could likely have reduced all of the variables to a single type variable with no bound, and added the FutureOr and ? on occurrences as needed. Maybe.
But more relevantly FutureOr and ? are structural, so X extends FutureOf<X> has an infinitive expansion if one tries to be eager. That's bad.
(A cycle with no union type shouldn't be a problem if it can be detected, it's just useless.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
For background and discussion, please see #433.
This PR introduces a compile-time error for type parameters and their bounds when they form a union type based cycle. For example, not just
X extends XandY extends Z, Z extends Yare errors (as they have always been), but alsoX extends X?andY extends FutureOr<Z>, Z extends Y. That is, we make it an error to form a cycle even when union types are stripped off first. This is done because it allows us to avoid some known sources of infinite looping in the subtype and standard upper bound algorithms.Following #433 (comment), this PR also renames "union-free type derived from a type" to "union base type of a type".