Skip to content

Commit

Permalink
Retry constraint.replace after constraint.updateEntry
Browse files Browse the repository at this point in the history
Checking `isSub` on the resulting bounds may have introduced new constraints,
which might allow us to replace the type parameter entirely.
  • Loading branch information
EugeneFlesselle committed Apr 17, 2024
1 parent 63810dc commit 8daccbd
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions compiler/src/dotty/tools/dotc/core/ConstraintHandling.scala
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,19 @@ trait ConstraintHandling {
false
else
val bound = legalBound(param, rawBound, isUpper)

// If the narrowed bounds are equal and not recursive,
// we can remove `param` from the constraint.
def replaceIfEqualBounds: Boolean =
val TypeBounds(lo, hi) = constraint.nonParamBounds(param)
val equalBounds = (if isUpper then lo else hi) eq bound
if equalBounds && !bound.existsPart(_ eq param, StopAt.Static) then
constraint = constraint.replace(param, bound)
true
else false

val oldBounds @ TypeBounds(lo, hi) = constraint.nonParamBounds(param)
val equalBounds = (if isUpper then lo else hi) eq bound
if equalBounds && !bound.existsPart(_ eq param, StopAt.Static) then
// The narrowed bounds are equal and not recursive,
// so we can remove `param` from the constraint.
constraint = constraint.replace(param, bound)
true
else
replaceIfEqualBounds || {
// Narrow one of the bounds of type parameter `param`
// If `isUpper` is true, ensure that `param <: `bound`, otherwise ensure
// that `param >: bound`.
Expand All @@ -328,8 +333,13 @@ trait ConstraintHandling {
|| {
constraint = c1
val TypeBounds(lo, hi) = constraint.entry(param): @unchecked
isSub(lo, hi)
isSub(lo, hi) && {
// isSub may have introduced new constraints
replaceIfEqualBounds
true
}
}
}
end addOneBound

protected def addBoundTransitively(param: TypeParamRef, rawBound: Type, isUpper: Boolean)(using Context): Boolean =
Expand Down

0 comments on commit 8daccbd

Please sign in to comment.