Skip to content

Commit 3cf8530

Browse files
committed
Simplify ConstraintHandling#fullBounds
1 parent ea1815d commit 3cf8530

File tree

4 files changed

+29
-25
lines changed

4 files changed

+29
-25
lines changed

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ trait ConstraintHandling[AbstractContext] {
3434
protected def constraint: Constraint
3535
protected def constraint_=(c: Constraint): Unit
3636

37-
protected def externalize(param: TypeParamRef)(implicit ctx: Context): Type
38-
3937
private[this] var addConstraintInvocations = 0
4038

4139
/** If the constraint is frozen we cannot add new bounds to the constraint. */
@@ -71,28 +69,20 @@ trait ConstraintHandling[AbstractContext] {
7169
case tp => tp
7270
}
7371

74-
def nonParamBounds(param: TypeParamRef)(implicit ctx: Context): TypeBounds =
75-
constraint.nonParamBounds(param) match {
76-
case TypeAlias(tpr: TypeParamRef) => TypeAlias(externalize(tpr))
77-
case tb => tb
78-
}
72+
def nonParamBounds(param: TypeParamRef)(implicit actx: AbstractContext): TypeBounds = constraint.nonParamBounds(param)
7973

80-
def fullLowerBound(param: TypeParamRef)(implicit ctx: Context): Type =
81-
(nonParamBounds(param).lo /: constraint.minLower(param)) {
82-
(t, u) => t | externalize(u)
83-
}
74+
def fullLowerBound(param: TypeParamRef)(implicit actx: AbstractContext): Type =
75+
(nonParamBounds(param).lo /: constraint.minLower(param))(_ | _)
8476

85-
def fullUpperBound(param: TypeParamRef)(implicit ctx: Context): Type =
86-
(nonParamBounds(param).hi /: constraint.minUpper(param)) {
87-
(t, u) => t & externalize(u)
88-
}
77+
def fullUpperBound(param: TypeParamRef)(implicit actx: AbstractContext): Type =
78+
(nonParamBounds(param).hi /: constraint.minUpper(param))(_ & _)
8979

9080
/** Full bounds of `param`, including other lower/upper params.
9181
*
9282
* Note that underlying operations perform subtype checks - for this reason, recursing on `fullBounds`
9383
* of some param when comparing types might lead to infinite recursion. Consider `bounds` instead.
9484
*/
95-
def fullBounds(param: TypeParamRef)(implicit ctx: Context): TypeBounds =
85+
def fullBounds(param: TypeParamRef)(implicit actx: AbstractContext): TypeBounds =
9686
nonParamBounds(param).derivedTypeBounds(fullLowerBound(param), fullUpperBound(param))
9787

9888
protected def addOneBound(param: TypeParamRef, bound: Type, isUpper: Boolean)(implicit actx: AbstractContext): Boolean =

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,33 @@ final class ProperGadtConstraint private(
180180
override protected def constraint = myConstraint
181181
override protected def constraint_=(c: Constraint) = myConstraint = c
182182

183-
override protected def externalize(param: TypeParamRef)(implicit ctx: Context): Type =
184-
reverseMapping(param) match {
185-
case sym: Symbol => sym.typeRef
186-
case null => param
187-
}
188-
189183
override def isSubType(tp1: Type, tp2: Type)(implicit ctx: Context): Boolean = ctx.typeComparer.isSubType(tp1, tp2)
190184
override def isSameType(tp1: Type, tp2: Type)(implicit ctx: Context): Boolean = ctx.typeComparer.isSameType(tp1, tp2)
191185

186+
override def nonParamBounds(param: TypeParamRef)(implicit ctx: Context): TypeBounds =
187+
constraint.nonParamBounds(param) match {
188+
case TypeAlias(tpr: TypeParamRef) => TypeAlias(externalize(tpr))
189+
case tb => tb
190+
}
191+
192+
override def fullLowerBound(param: TypeParamRef)(implicit ctx: Context): Type =
193+
(nonParamBounds(param).lo /: constraint.minLower(param)) {
194+
(t, u) => t | externalize(u)
195+
}
196+
197+
override def fullUpperBound(param: TypeParamRef)(implicit ctx: Context): Type =
198+
(nonParamBounds(param).hi /: constraint.minUpper(param)) {
199+
(t, u) => t & externalize(u)
200+
}
201+
192202
// ---- Private ----------------------------------------------------------
193203

204+
private[this] def externalize(param: TypeParamRef)(implicit ctx: Context): Type =
205+
reverseMapping(param) match {
206+
case sym: Symbol => sym.typeRef
207+
case null => param
208+
}
209+
194210
private[this] def tvar(sym: Symbol)(implicit ctx: Context): TypeVar = {
195211
mapping(sym) match {
196212
case tv: TypeVar =>

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
3333
def constraint: Constraint = state.constraint
3434
def constraint_=(c: Constraint): Unit = state.constraint = c
3535

36-
override protected def externalize(param: TypeParamRef)(implicit ctx: Context): Type = param
37-
3836
private[this] var pendingSubTypes: mutable.Set[(Type, Type)] = null
3937
private[this] var recCount = 0
4038
private[this] var monitored = false

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
210210
val bounds =
211211
if (constr.contains(tp)) {
212212
val ctx0 = ctx.addMode(Mode.Printing)
213-
ctx0.typeComparer.fullBounds(tp.origin)(ctx0)
213+
ctx0.typeComparer.fullBounds(tp.origin)
214214
}
215215
else TypeBounds.empty
216216
if (bounds.isTypeAlias) toText(bounds.lo) ~ (Str("^") provided ctx.settings.YprintDebug.value)

0 commit comments

Comments
 (0)