@@ -492,23 +492,35 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
492
492
case AndType (tp21, tp22) => constrainRHSVars(tp21) && constrainRHSVars(tp22)
493
493
case _ => true
494
494
495
- widenOK
496
- || joinOK
497
- || (tp1.isSoft || constrainRHSVars(tp2)) && recur(tp11, tp2) && recur(tp12, tp2)
498
- || containsAnd(tp1)
499
- && ! joined
500
- && {
501
- joined = true
502
- try inFrozenGadt(recur(tp1.join, tp2))
503
- finally joined = false
504
- }
505
- // An & on the left side loses information. We compensate by also trying the join.
506
- // This is less ad-hoc than it looks since we produce joins in type inference,
507
- // and then need to check that they are indeed supertypes of the original types
508
- // under -Ycheck. Test case is i7965.scala.
509
- // On the other hand, we could get a combinatorial explosion by applying such joins
510
- // recursively, so we do it only once. See i14870.scala as a test case, which would
511
- // loop for a very long time without the recursion brake.
495
+ def hardenTypeVars (tp2 : Type ): Unit = tp2.dealiasKeepRefiningAnnots match
496
+ case tvar : TypeVar if constraint.contains(tvar.origin) =>
497
+ tvar.widenUnions = false
498
+ case tp2 : TypeParamRef if constraint.contains(tp2) =>
499
+ hardenTypeVars(constraint.typeVarOfParam(tp2))
500
+ case tp2 : AndOrType =>
501
+ hardenTypeVars(tp2.tp1)
502
+ hardenTypeVars(tp2.tp2)
503
+ case _ =>
504
+
505
+ val res = widenOK
506
+ || joinOK
507
+ || (tp1.isSoft || constrainRHSVars(tp2)) && recur(tp11, tp2) && recur(tp12, tp2)
508
+ || containsAnd(tp1)
509
+ && ! joined
510
+ && {
511
+ joined = true
512
+ try inFrozenGadt(recur(tp1.join, tp2))
513
+ finally joined = false
514
+ }
515
+ // An & on the left side loses information. We compensate by also trying the join.
516
+ // This is less ad-hoc than it looks since we produce joins in type inference,
517
+ // and then need to check that they are indeed supertypes of the original types
518
+ // under -Ycheck. Test case is i7965.scala.
519
+ // On the other hand, we could get a combinatorial explosion by applying such joins
520
+ // recursively, so we do it only once. See i14870.scala as a test case, which would
521
+ // loop for a very long time without the recursion brake.
522
+ if res then hardenTypeVars(tp2)
523
+ res
512
524
513
525
case tp1 : MatchType =>
514
526
val reduced = tp1.reduced
@@ -2851,8 +2863,8 @@ object TypeComparer {
2851
2863
def subtypeCheckInProgress (using Context ): Boolean =
2852
2864
comparing(_.subtypeCheckInProgress)
2853
2865
2854
- def instanceType (param : TypeParamRef , fromBelow : Boolean )(using Context ): Type =
2855
- comparing(_.instanceType(param, fromBelow))
2866
+ def instanceType (param : TypeParamRef , fromBelow : Boolean , widenUnions : Boolean )(using Context ): Type =
2867
+ comparing(_.instanceType(param, fromBelow, widenUnions ))
2856
2868
2857
2869
def approximation (param : TypeParamRef , fromBelow : Boolean )(using Context ): Type =
2858
2870
comparing(_.approximation(param, fromBelow))
@@ -2872,8 +2884,8 @@ object TypeComparer {
2872
2884
def addToConstraint (tl : TypeLambda , tvars : List [TypeVar ])(using Context ): Boolean =
2873
2885
comparing(_.addToConstraint(tl, tvars))
2874
2886
2875
- def widenInferred (inst : Type , bound : Type )(using Context ): Type =
2876
- comparing(_.widenInferred(inst, bound))
2887
+ def widenInferred (inst : Type , bound : Type , widenUnions : Boolean )(using Context ): Type =
2888
+ comparing(_.widenInferred(inst, bound, widenUnions ))
2877
2889
2878
2890
def dropTransparentTraits (tp : Type , bound : Type )(using Context ): Type =
2879
2891
comparing(_.dropTransparentTraits(tp, bound))
0 commit comments