@@ -738,7 +738,9 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
738738 return recur(AndType (tp11, tp121), tp2) && recur(AndType (tp11, tp122), tp2)
739739 case _ =>
740740 }
741- either(recur(tp11, tp2), recur(tp12, tp2))
741+ val tp1norm = simplifyAndTypeWithFallback(tp11, tp12, tp1)
742+ if (tp1 ne tp1norm) recur(tp1norm, tp2)
743+ else either(recur(tp11, tp2), recur(tp12, tp2))
742744 case tp1 : MatchType =>
743745 def compareMatch = tp2 match {
744746 case tp2 : MatchType =>
@@ -1641,6 +1643,18 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
16411643 NoType
16421644 }
16431645
1646+ private [this ] def andTypeGen (tp1 : Type , tp2 : Type , op : (Type , Type ) => Type ,
1647+ original : (Type , Type ) => Type = _ & _, isErased : Boolean = ctx.erasedTypes): Type = trace(s " glb( ${tp1.show}, ${tp2.show}) " , subtyping, show = true ) {
1648+ val t1 = distributeAnd(tp1, tp2)
1649+ if (t1.exists) t1
1650+ else {
1651+ val t2 = distributeAnd(tp2, tp1)
1652+ if (t2.exists) t2
1653+ else if (isErased) erasedGlb(tp1, tp2, isJava = false )
1654+ else liftIfHK(tp1, tp2, op, original)
1655+ }
1656+ }
1657+
16441658 /** Form a normalized conjunction of two types.
16451659 * Note: For certain types, `&` is distributed inside the type. This holds for
16461660 * all types which are not value types (e.g. TypeBounds, ClassInfo,
@@ -1659,16 +1673,11 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
16591673 *
16601674 * In these cases, a MergeError is thrown.
16611675 */
1662- final def andType (tp1 : Type , tp2 : Type , isErased : Boolean = ctx.erasedTypes): Type = trace(s " glb( ${tp1.show}, ${tp2.show}) " , subtyping, show = true ) {
1663- val t1 = distributeAnd(tp1, tp2)
1664- if (t1.exists) t1
1665- else {
1666- val t2 = distributeAnd(tp2, tp1)
1667- if (t2.exists) t2
1668- else if (isErased) erasedGlb(tp1, tp2, isJava = false )
1669- else liftIfHK(tp1, tp2, AndType (_, _), _ & _)
1670- }
1671- }
1676+ final def andType (tp1 : Type , tp2 : Type , isErased : Boolean = ctx.erasedTypes): Type =
1677+ andTypeGen(tp1, tp2, AndType (_, _), isErased = isErased)
1678+
1679+ final def simplifyAndTypeWithFallback (tp1 : Type , tp2 : Type , fallback : Type ): Type =
1680+ andTypeGen(tp1, tp2, (_, _) => fallback)
16721681
16731682 /** Form a normalized conjunction of two types.
16741683 * Note: For certain types, `|` is distributed inside the type. This holds for
0 commit comments