@@ -18,19 +18,22 @@ import config.Printers.{constr, typr}
1818 * By comparison: Constraint handlers are parts of type comparers and can use their functionality.
1919 * Constraint handlers update the current constraint as a side effect.
2020 */
21- trait ConstraintHandling {
22- import Constraint . _
21+ trait ConstraintHandling [ VarRef <: TypeVarRef . Of [ VarRef , RefBinder ], RefBinder <: TypeVarRefBinder . Of [ VarRef , RefBinder ]] {
22+ protected final type OwnConstraint = Constraint [ VarRef , RefBinder ]
2323
24- def constr_println (msg : => String ): Unit = constr.println(msg)
25- def typr_println (msg : => String ): Unit = typr.println(msg)
24+ protected def isVarRef (r : Type ): Boolean = r.isInstanceOf [TypeVarRef ]
25+ protected def isRefBinder (r : Type ): Boolean = r.isInstanceOf [TypeVarRefBinder ]
26+
27+ protected def constr_println (msg : => String ): Unit = constr.println(msg)
28+ protected def typr_println (msg : => String ): Unit = typr.println(msg)
2629
2730 implicit def ctx : Context
2831
2932 protected def isSubType (tp1 : Type , tp2 : Type ): Boolean
3033 protected def isSameType (tp1 : Type , tp2 : Type ): Boolean
3134
32- protected def constraint : Constraint
33- protected def constraint_= (c : Constraint ): Unit
35+ protected def constraint : OwnConstraint
36+ protected def constraint_= (c : OwnConstraint ): Unit
3437
3538 private [this ] var addConstraintInvocations = 0
3639
@@ -59,9 +62,10 @@ trait ConstraintHandling {
5962 * is done only in a temporary way for contexts that may be retracted
6063 * without also retracting the type var as a whole.
6164 */
62- def instType (tvar : TypeVar ): Type = constraint.entry(tvar.origin) match {
65+ def instType (tvar : TypeVarHandle [ VarRef ] ): Type = constraint.entry(tvar.origin) match {
6366 case _ : TypeBounds => NoType
64- case tp : VarRef =>
67+ case tp_ if isVarRef(tp_) =>
68+ val tp = tp_.asInstanceOf [VarRef ]
6569 var tvar1 = constraint.typeVarOfParam(tp)
6670 if (tvar1.exists) tvar1 else tp
6771 case tp => tp
@@ -284,9 +288,9 @@ trait ConstraintHandling {
284288 case tp : SingletonType => true
285289 case AndType (tp1, tp2) => isMultiSingleton(tp1) | isMultiSingleton(tp2)
286290 case OrType (tp1, tp2) => isMultiSingleton(tp1) & isMultiSingleton(tp2)
287- case tp : TypeRef if ! constraint.contains(tp : VarRef ) => isMultiSingleton(tp.info.hiBound)
291+ case tp_ if isVarRef(tp_) => isMultiSingleton(bounds(tp_.asInstanceOf [VarRef ]).hi)
292+ case tp : TypeRef => isMultiSingleton(tp.info.hiBound)
288293 case tp : TypeVar => isMultiSingleton(tp.underlying)
289- case tp : VarRef => isMultiSingleton(bounds(tp).hi)
290294 case _ => false
291295 }
292296 def isOrType (tp : Type ): Boolean = tp.dealias match {
@@ -327,7 +331,7 @@ trait ConstraintHandling {
327331 * Both `c1` and `c2` are required to derive from constraint `pre`, possibly
328332 * narrowing it with further bounds.
329333 */
330- protected final def subsumes (c1 : Constraint , c2 : Constraint , pre : Constraint ): Boolean =
334+ protected final def subsumes (c1 : OwnConstraint , c2 : OwnConstraint , pre : OwnConstraint ): Boolean =
331335 if (c2 eq pre) true
332336 else if (c1 eq pre) false
333337 else {
@@ -355,7 +359,7 @@ trait ConstraintHandling {
355359 * and propagate all bounds.
356360 * @param tvars See Constraint#add
357361 */
358- def addToConstraint (tl : Binder , tvars : List [TypeVar ]): Boolean =
362+ def addToConstraint (tl : RefBinder , tvars : List [TypeVarHandle [ VarRef ] ]): Boolean =
359363 checkPropagated(i " initialized $tl" ) {
360364 constraint = constraint.add(tl, tvars)
361365 tl.boundRefs.forall { param =>
@@ -427,7 +431,7 @@ trait ConstraintHandling {
427431 }
428432 else tp
429433
430- def addParamBound (bound : TypeParamRef ) =
434+ def addParamBound (bound : VarRef ) =
431435 constraint.entry(param) match {
432436 case _ : TypeBounds =>
433437 if (fromBelow) addLess(bound, param) else addLess(param, bound)
@@ -480,9 +484,10 @@ trait ConstraintHandling {
480484 val p2 = prune(bound.tp2)
481485 if (p1.exists && p2.exists) bound.derivedOrType(p1, p2)
482486 else NoType
483- case bound : TypeVar if constraint contains bound.origin =>
487+ case bound : TypeVar if constraint contains bound.origin. asInstanceOf [ VarRef ] =>
484488 prune(bound.underlying)
485- case bound : TypeParamRef =>
489+ case bound_ : TypeParamRef =>
490+ val bound = bound_.asInstanceOf [VarRef ]
486491 constraint.entry(bound) match {
487492 case NoType => pruneLambdaParams(bound)
488493 case _ : TypeBounds =>
@@ -497,7 +502,8 @@ trait ConstraintHandling {
497502 }
498503
499504 try bound match {
500- case bound : TypeParamRef if constraint contains bound =>
505+ case bound_ : TypeParamRef if constraint contains bound_.asInstanceOf [VarRef ] =>
506+ val bound = bound_.asInstanceOf [VarRef ]
501507 addParamBound(bound)
502508 case _ =>
503509 val pbound = prune(bound)
@@ -509,7 +515,7 @@ trait ConstraintHandling {
509515 }
510516
511517 /** Instantiate `param` to `tp` if the constraint stays satisfiable */
512- protected def tryInstantiate (param : TypeParamRef , tp : Type ): Boolean = {
518+ protected def tryInstantiate (param : VarRef , tp : Type ): Boolean = {
513519 val saved = constraint
514520 constraint =
515521 if (addConstraint(param, tp, fromBelow = true ) &&
0 commit comments