@@ -790,9 +790,9 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
790
790
(defn.AnyType /: tps)(glb)
791
791
792
792
/** The least upper bound of two types
793
- * @note We do not admit singleton types in or-types as lubs.
793
+ * @param keepSingletons If true, do not widen singletons when forming an OrType
794
794
*/
795
- def lub (tp1 : Type , tp2 : Type ): Type = /* >|>*/ ctx.traceIndented(s " lub( ${tp1.show}, ${tp2.show}) " , subtyping, show = true ) /* <|<*/ {
795
+ def lub (tp1 : Type , tp2 : Type , keepSingletons : Boolean = false ): Type = /* >|>*/ ctx.traceIndented(s " lub( ${tp1.show}, ${tp2.show}, $keepSingletons ) " , subtyping, show = true ) /* <|<*/ {
796
796
if (tp1 eq tp2) tp1
797
797
else if (! tp1.exists) tp1
798
798
else if (! tp2.exists) tp2
@@ -805,8 +805,8 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
805
805
val t2 = mergeIfSuper(tp2, tp1)
806
806
if (t2.exists) t2
807
807
else {
808
- val tp1w = tp1.widen
809
- val tp2w = tp2.widen
808
+ val tp1w = if (keepSingletons) tp1.widenExpr else tp1.widen
809
+ val tp2w = if (keepSingletons) tp2.widenExpr else tp2.widen
810
810
if ((tp1 ne tp1w) || (tp2 ne tp2w)) lub(tp1w, tp2w)
811
811
else orType(tp1w, tp2w) // no need to check subtypes again
812
812
}
@@ -815,8 +815,8 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
815
815
}
816
816
817
817
/** The least upper bound of a list of types */
818
- final def lub (tps : List [Type ]): Type =
819
- (defn.NothingType /: tps)(lub)
818
+ final def lubList (tps : List [Type ], keepSingletons : Boolean = false ): Type =
819
+ (defn.NothingType /: tps)(lub(_, _, keepSingletons) )
820
820
821
821
/** Merge `t1` into `tp2` if t1 is a subtype of some &-summand of tp2.
822
822
*/
@@ -1207,9 +1207,9 @@ class ExplainingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
1207
1207
super .hasMatchingMember(name, tp1, tp2)
1208
1208
}
1209
1209
1210
- override def lub (tp1 : Type , tp2 : Type ) =
1211
- traceIndented(s " lub( ${show(tp1)}, ${show(tp2)}) " ) {
1212
- super .lub(tp1, tp2)
1210
+ override def lub (tp1 : Type , tp2 : Type , canWiden : Boolean = true ) =
1211
+ traceIndented(s " lub( ${show(tp1)}, ${show(tp2)}, $canWiden ) " ) {
1212
+ super .lub(tp1, tp2, canWiden )
1213
1213
}
1214
1214
1215
1215
override def glb (tp1 : Type , tp2 : Type ) =
0 commit comments