Skip to content

Commit 3d4275d

Browse files
committed
Streamline OrderingConstraint's newConstraint
1 parent 5bd67bb commit 3d4275d

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

compiler/src/dotty/tools/dotc/core/OrderingConstraint.scala

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@ object OrderingConstraint {
2424
/** The type of `OrderingConstraint#lowerMap`, `OrderingConstraint#upperMap` */
2525
type ParamOrdering = ArrayValuedMap[List[TypeParamRef]]
2626

27-
/** A new constraint with given maps and given set of hard typevars */
28-
private def newConstraint(
29-
boundsMap: ParamBounds, lowerMap: ParamOrdering, upperMap: ParamOrdering,
30-
hardVars: TypeVars)(using Context) : OrderingConstraint =
31-
if boundsMap.isEmpty && lowerMap.isEmpty && upperMap.isEmpty then
32-
empty
33-
else
34-
val result = new OrderingConstraint(boundsMap, lowerMap, upperMap, hardVars)
35-
if ctx.run != null then ctx.run.nn.recordConstraintSize(result, result.boundsMap.size)
36-
result
37-
3827
/** A lens for updating a single entry array in one of the three constraint maps */
3928
abstract class ConstraintLens[T <: AnyRef: ClassTag] {
4029
def entries(c: OrderingConstraint, poly: TypeLambda): Array[T] | Null
@@ -93,23 +82,23 @@ object OrderingConstraint {
9382
def entries(c: OrderingConstraint, poly: TypeLambda): Array[Type] | Null =
9483
c.boundsMap(poly)
9584
def updateEntries(c: OrderingConstraint, poly: TypeLambda, entries: Array[Type])(using Context): OrderingConstraint =
96-
newConstraint(c.boundsMap.updated(poly, entries), c.lowerMap, c.upperMap, c.hardVars)
85+
c.newConstraint(boundsMap = c.boundsMap.updated(poly, entries))
9786
def initial = NoType
9887
}
9988

10089
val lowerLens: ConstraintLens[List[TypeParamRef]] = new ConstraintLens[List[TypeParamRef]] {
10190
def entries(c: OrderingConstraint, poly: TypeLambda): Array[List[TypeParamRef]] | Null =
10291
c.lowerMap(poly)
10392
def updateEntries(c: OrderingConstraint, poly: TypeLambda, entries: Array[List[TypeParamRef]])(using Context): OrderingConstraint =
104-
newConstraint(c.boundsMap, c.lowerMap.updated(poly, entries), c.upperMap, c.hardVars)
93+
c.newConstraint(lowerMap = c.lowerMap.updated(poly, entries))
10594
def initial = Nil
10695
}
10796

10897
val upperLens: ConstraintLens[List[TypeParamRef]] = new ConstraintLens[List[TypeParamRef]] {
10998
def entries(c: OrderingConstraint, poly: TypeLambda): Array[List[TypeParamRef]] | Null =
11099
c.upperMap(poly)
111100
def updateEntries(c: OrderingConstraint, poly: TypeLambda, entries: Array[List[TypeParamRef]])(using Context): OrderingConstraint =
112-
newConstraint(c.boundsMap, c.lowerMap, c.upperMap.updated(poly, entries), c.hardVars)
101+
c.newConstraint(upperMap = c.upperMap.updated(poly, entries))
113102
def initial = Nil
114103
}
115104

@@ -148,6 +137,19 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
148137

149138
type This = OrderingConstraint
150139

140+
/** A new constraint with given maps and given set of hard typevars */
141+
def newConstraint( // !!! Dotty problem: Making newConstraint `private` causes -Ytest-pickler failure.
142+
boundsMap: ParamBounds = this.boundsMap,
143+
lowerMap: ParamOrdering = this.lowerMap,
144+
upperMap: ParamOrdering = this.upperMap,
145+
hardVars: TypeVars = this.hardVars)(using Context) : OrderingConstraint =
146+
if boundsMap.isEmpty && lowerMap.isEmpty && upperMap.isEmpty then
147+
empty
148+
else
149+
val result = new OrderingConstraint(boundsMap, lowerMap, upperMap, hardVars)
150+
if ctx.run != null then ctx.run.nn.recordConstraintSize(result, result.boundsMap.size)
151+
result
152+
151153
// ----------- Basic indices --------------------------------------------------
152154

153155
/** The number of type parameters in the given entry array */
@@ -282,7 +284,8 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
282284
val entries1 = new Array[Type](nparams * 2)
283285
poly.paramInfos.copyToArray(entries1, 0)
284286
tvars.copyToArray(entries1, nparams)
285-
newConstraint(boundsMap.updated(poly, entries1), lowerMap, upperMap, hardVars).init(poly)
287+
newConstraint(boundsMap = this.boundsMap.updated(poly, entries1))
288+
.init(poly)
286289
}
287290

288291
/** Split dependent parameters off the bounds for parameters in `poly`.
@@ -511,7 +514,7 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
511514
def swapKey[T](m: ArrayValuedMap[T]) =
512515
val info = m(from)
513516
if info == null then m else m.remove(from).updated(to, info)
514-
var current = newConstraint(swapKey(boundsMap), swapKey(lowerMap), swapKey(upperMap), hardVars)
517+
var current = newConstraint(swapKey(boundsMap), swapKey(lowerMap), swapKey(upperMap))
515518
def subst[T <: Type](x: T): T = x.subst(from, to).asInstanceOf[T]
516519
current.foreachParam {(p, i) =>
517520
current = boundsLens.map(this, current, p, i, subst)
@@ -524,7 +527,7 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
524527
def isHard(tv: TypeVar) = hardVars.contains(tv)
525528

526529
def withHard(tv: TypeVar)(using Context) =
527-
newConstraint(boundsMap, lowerMap, upperMap, hardVars + tv)
530+
newConstraint(hardVars = this.hardVars + tv)
528531

529532
def instType(tvar: TypeVar): Type = entry(tvar.origin) match
530533
case _: TypeBounds => NoType

0 commit comments

Comments
 (0)