@@ -32,6 +32,9 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
3232 def constraint : Constraint = state.constraint
3333 def constraint_= (c : Constraint ): Unit = state.constraint = c
3434
35+ override protected def typeLub (tp1 : Type , tp2 : Type )(implicit actx : AbsentContext ): Type =
36+ lub(tp1, tp2)
37+
3538 private [this ] var pendingSubTypes : mutable.Set [(Type , Type )] = null
3639 private [this ] var recCount = 0
3740 private [this ] var monitored = false
@@ -1415,9 +1418,10 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
14151418
14161419 /** The least upper bound of two types
14171420 * @param canConstrain If true, new constraints might be added to simplify the lub.
1418- * @note We do not admit singleton types in or-types as lubs.
1421+ * @param admitSingletons We only admit singletons as parts of lubs when we must maintain necessary conditions,
1422+ * such as when inferring GADT constraints.
14191423 */
1420- def lub (tp1 : Type , tp2 : Type , canConstrain : Boolean = false ): Type = /* >|>*/ trace(s " lub( ${tp1.show}, ${tp2.show}, canConstrain= $canConstrain) " , subtyping, show = true ) /* <|<*/ {
1424+ def lub (tp1 : Type , tp2 : Type , canConstrain : Boolean = false , admitSingletons : Boolean = false ): Type = /* >|>*/ trace(s " lub( ${tp1.show}, ${tp2.show}, canConstrain= $canConstrain) " , subtyping, show = true ) /* <|<*/ {
14211425 if (tp1 eq tp2) tp1
14221426 else if (! tp1.exists) tp1
14231427 else if (! tp2.exists) tp2
@@ -1429,6 +1433,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
14291433 else {
14301434 val t2 = mergeIfSuper(tp2, tp1, canConstrain)
14311435 if (t2.exists) t2
1436+ else if (admitSingletons) orType(tp1.widenExpr, tp2.widenExpr)
14321437 else {
14331438 val tp1w = tp1.widen
14341439 val tp2w = tp2.widen
@@ -1954,8 +1959,8 @@ class ExplainingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
19541959 super .hasMatchingMember(name, tp1, tp2)
19551960 }
19561961
1957- override def lub (tp1 : Type , tp2 : Type , canConstrain : Boolean = false ): Type =
1958- traceIndented(s " lub( ${show(tp1)}, ${show(tp2)}, canConstrain= $canConstrain) " ) {
1962+ override def lub (tp1 : Type , tp2 : Type , canConstrain : Boolean = false , admitSingletons : Boolean = false ): Type =
1963+ traceIndented(s " lub( ${show(tp1)}, ${show(tp2)}, canConstrain= $canConstrain, admitSingletons= $admitSingletons ) " ) {
19591964 super .lub(tp1, tp2, canConstrain)
19601965 }
19611966
0 commit comments