@@ -453,7 +453,7 @@ object Inferencing {
453
453
*
454
454
* we want to instantiate U to x.type right away. No need to wait further.
455
455
*/
456
- private def variances (tp : Type )(using Context ): VarianceMap = {
456
+ private def variances (tp : Type , pt : Type = WildcardType )(using Context ): VarianceMap = {
457
457
Stats .record(" variances" )
458
458
val constraint = ctx.typerState.constraint
459
459
@@ -486,21 +486,21 @@ object Inferencing {
486
486
def traverse (tp : Type ) = { vmap1 = accu(vmap1, tp) }
487
487
vmap.foreachBinding { (tvar, v) =>
488
488
val param = tvar.origin
489
- val e = constraint.entry(param)
490
- accu.setVariance(v)
491
- if (v >= 0 ) {
492
- traverse(e.bounds.lo)
493
- constraint.lower(param).foreach(p => traverse(constraint.typeVarOfParam(p)) )
494
- }
495
- if ( v <= 0 ) {
496
- traverse(e.bounds. hi)
497
- constraint.upper(param).foreach(p => traverse(constraint.typeVarOfParam(p)))
498
- }
489
+ constraint.entry(param) match
490
+ case TypeBounds (lo, hi) =>
491
+ accu.setVariance(v)
492
+ if v >= 0 then
493
+ traverse(lo )
494
+ constraint.lower(param).foreach(p => traverse(constraint.typeVarOfParam(p)))
495
+ if v <= 0 then
496
+ traverse(hi)
497
+ constraint.upper(param).foreach(p => traverse(constraint.typeVarOfParam(p)))
498
+ case _ =>
499
499
}
500
500
if (vmap1 eq vmap) vmap else propagate(vmap1)
501
501
}
502
502
503
- propagate(accu(SimpleIdentityMap .empty, tp))
503
+ propagate(accu(accu( SimpleIdentityMap .empty, tp), pt.finalResultType ))
504
504
}
505
505
506
506
/** Run the transformation after dealiasing but return the original type if it was a no-op. */
@@ -546,8 +546,8 @@ trait Inferencing { this: Typer =>
546
546
* @param locked the set of type variables of the current typer state that cannot be interpolated
547
547
* at the present time
548
548
* Eligible for interpolation are all type variables owned by the current typerstate
549
- * that are not in locked. Type variables occurring co- (respectively, contra-) variantly in the type
550
- * are minimized (respectvely, maximized). Non occurring type variables are minimized if they
549
+ * that are not in locked. Type variables occurring co- (respectively, contra-) variantly in the tree type
550
+ * or expected type are minimized (respectvely, maximized). Non occurring type variables are minimized if they
551
551
* have a lower bound different from Nothing, maximized otherwise. Type variables appearing
552
552
* non-variantly in the type are left untouched.
553
553
*
@@ -572,15 +572,15 @@ trait Inferencing { this: Typer =>
572
572
if ((ownedVars ne locked) && ! ownedVars.isEmpty) {
573
573
val qualifying = ownedVars -- locked
574
574
if (! qualifying.isEmpty) {
575
- typr.println(i " interpolate $tree: ${tree.tpe.widen} in $state, owned vars = ${state.ownedVars.toList}%, %, qualifying = ${qualifying.toList}%, %, previous = ${locked.toList}%, % / ${state.constraint}" )
575
+ typr.println(i " interpolate $tree: ${tree.tpe.widen} in $state, pt = $pt , owned vars = ${state.ownedVars.toList}%, %, qualifying = ${qualifying.toList}%, %, previous = ${locked.toList}%, % / ${state.constraint}" )
576
576
val resultAlreadyConstrained =
577
577
tree.isInstanceOf [Apply ] || tree.tpe.isInstanceOf [MethodOrPoly ]
578
578
if (! resultAlreadyConstrained)
579
579
constrainResult(tree.symbol, tree.tpe, pt)
580
580
// This is needed because it could establish singleton type upper bounds. See i2998.scala.
581
581
582
582
val tp = tree.tpe.widen
583
- val vs = variances(tp)
583
+ val vs = variances(tp, pt )
584
584
585
585
// Avoid interpolating variables occurring in tree's type if typerstate has unreported errors.
586
586
// Reason: The errors might reflect unsatisfiable constraints. In that
0 commit comments