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 can have introduced new constraints,
which might allow us to replace the type parameter entirely.
  • Loading branch information
EugeneFlesselle committed May 13, 2024
1 parent 99c4c00 commit 3a1c2c5
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 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,18 @@ trait ConstraintHandling {
false
else
val bound = legalBound(param, rawBound, isUpper)
lazy val recBound = bound.existsPart(_ eq param, StopAt.Static)

// If the narrowed bounds are equal and not recursive,
// we can remove `param` from the constraint.
def tryReplace(lo: Type, hi: Type): Boolean =
val equalBounds = (if isUpper then lo else hi) eq bound
val canReplace = equalBounds && !recBound
if canReplace then constraint = constraint.replace(param, bound)
canReplace

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
tryReplace(lo, hi) || {
// 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 +332,12 @@ trait ConstraintHandling {
|| {
constraint = c1
val TypeBounds(lo, hi) = constraint.entry(param): @unchecked
isSub(lo, hi)
isSub(lo, hi) && {
tryReplace(lo, hi) // isSub may have introduced new constraints
true
}
}
}
end addOneBound

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

0 comments on commit 3a1c2c5

Please sign in to comment.