Skip to content

Commit 3cc8914

Browse files
committed
Simplify ConstraintHandling#fullBounds
1 parent f53a9f8 commit 3cc8914

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
@@ -35,8 +35,6 @@ trait ConstraintHandling[AbstractContext] {
3535
protected def constraint: Constraint
3636
protected def constraint_=(c: Constraint): Unit
3737

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

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

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

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

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

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

9989
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,21 +180,37 @@ 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

192186
override protected def typeLub(tp1: Type, tp2: Type)(implicit ctx: Context): Type = {
193187
ctx.typeComparer.lub(tp1, tp2, admitSingletons = true)
194188
}
195189

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

208+
private[this] def externalize(param: TypeParamRef)(implicit ctx: Context): Type =
209+
reverseMapping(param) match {
210+
case sym: Symbol => sym.typeRef
211+
case null => param
212+
}
213+
198214
private[this] def tvar(sym: Symbol)(implicit ctx: Context): TypeVar = {
199215
mapping(sym) match {
200216
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
override protected def typeLub(tp1: Type, tp2: Type)(implicit actx: AbsentContext): Type =
3937
lub(tp1, tp2)
4038

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)