@@ -738,7 +738,9 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
738
738
return recur(AndType (tp11, tp121), tp2) && recur(AndType (tp11, tp122), tp2)
739
739
case _ =>
740
740
}
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))
742
744
case tp1 : MatchType =>
743
745
def compareMatch = tp2 match {
744
746
case tp2 : MatchType =>
@@ -1641,6 +1643,18 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
1641
1643
NoType
1642
1644
}
1643
1645
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
+
1644
1658
/** Form a normalized conjunction of two types.
1645
1659
* Note: For certain types, `&` is distributed inside the type. This holds for
1646
1660
* all types which are not value types (e.g. TypeBounds, ClassInfo,
@@ -1659,16 +1673,11 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
1659
1673
*
1660
1674
* In these cases, a MergeError is thrown.
1661
1675
*/
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)
1672
1681
1673
1682
/** Form a normalized conjunction of two types.
1674
1683
* Note: For certain types, `|` is distributed inside the type. This holds for
0 commit comments