Skip to content

Commit

Permalink
Backport "Retry constraint.replace after constraint.updateEntry" …
Browse files Browse the repository at this point in the history
…to LTS (#21111)

Backports #20399 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
WojciechMazur authored Jul 8, 2024
2 parents ffaab87 + e1aeea5 commit 7a5f48a
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions compiler/src/dotty/tools/dotc/core/ConstraintHandling.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ trait ConstraintHandling {
*/
private var myTrustBounds = true

inline def withUntrustedBounds(op: => Type): Type =
transparent inline def withUntrustedBounds(op: => Type): Type =
val saved = myTrustBounds
myTrustBounds = false
try op finally myTrustBounds = saved
Expand Down Expand Up @@ -293,34 +293,44 @@ trait ConstraintHandling {
// so we shouldn't allow them as constraints either.
false
else
val bound = legalBound(param, rawBound, isUpper)
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
// Narrow one of the bounds of type parameter `param`
// If `isUpper` is true, ensure that `param <: `bound`, otherwise ensure
// that `param >: bound`.
val narrowedBounds =
val saved = homogenizeArgs
homogenizeArgs = Config.alignArgsInAnd
try
withUntrustedBounds(
if isUpper then oldBounds.derivedTypeBounds(lo, hi & bound)
else oldBounds.derivedTypeBounds(lo | bound, hi))
finally
homogenizeArgs = saved

// Narrow one of the bounds of type parameter `param`
// If `isUpper` is true, ensure that `param <: `bound`,
// otherwise ensure that `param >: bound`.
val narrowedBounds: TypeBounds =
val bound = legalBound(param, rawBound, isUpper)
val oldBounds @ TypeBounds(lo, hi) = constraint.nonParamBounds(param)

val saved = homogenizeArgs
homogenizeArgs = Config.alignArgsInAnd
try
withUntrustedBounds(
if isUpper then oldBounds.derivedTypeBounds(lo, hi & bound)
else oldBounds.derivedTypeBounds(lo | bound, hi))
finally
homogenizeArgs = saved
end narrowedBounds

// If the narrowed bounds are equal and not recursive,
// we can remove `param` from the constraint.
def tryReplace(newBounds: TypeBounds): Boolean =
val TypeBounds(lo, hi) = newBounds
val canReplace = (lo eq hi) && !newBounds.existsPart(_ eq param, StopAt.Static)
if canReplace then constraint = constraint.replace(param, lo)
canReplace

tryReplace(narrowedBounds) || locally:
//println(i"narrow bounds for $param from $oldBounds to $narrowedBounds")
val c1 = constraint.updateEntry(param, narrowedBounds)
(c1 eq constraint)
|| {
constraint = c1
val TypeBounds(lo, hi) = constraint.entry(param): @unchecked
isSub(lo, hi)
val isSat = isSub(lo, hi)
if isSat then
// isSub may have narrowed the bounds further
tryReplace(constraint.nonParamBounds(param))
isSat
}
end addOneBound

Expand Down

0 comments on commit 7a5f48a

Please sign in to comment.