Skip to content

Commit 129766c

Browse files
committed
Refine "allow basetype" criterion for match type reduction
Fixes scala#15319
1 parent 77bcd0d commit 129766c

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -755,21 +755,22 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
755755
fourthTry
756756
}
757757

758-
def tryBaseType(cls2: Symbol) = {
759-
val allowBaseType = !caseLambda.exists || (tp1 match {
760-
case tp: TypeRef if tp.symbol.isClass => true
761-
case AppliedType(tycon: TypeRef, _) if tycon.symbol.isClass => true
762-
case _ => false
763-
})
758+
def allowBaseTypeForMatchSelector(tp: Type): Boolean = tp.dealias match
759+
case tp: TypeRef => tp.symbol.isClass
760+
case AppliedType(tycon, _) => allowBaseTypeForMatchSelector(tycon)
761+
case tp: TypeProxy => allowBaseTypeForMatchSelector(tp.superType)
762+
case tp: AndOrType => allowBaseTypeForMatchSelector(tp.tp1) || allowBaseTypeForMatchSelector(tp.tp2)
763+
case _ => false
764+
765+
def tryBaseType(cls2: Symbol) =
764766
val base = nonExprBaseType(tp1, cls2)
765-
if (base.exists && base.ne(tp1) && allowBaseType)
766-
isSubType(base, tp2, if (tp1.isRef(cls2)) approx else approx.addLow) ||
767-
base.isInstanceOf[OrType] && fourthTry
768-
// if base is a disjunction, this might have come from a tp1 type that
769-
// expands to a match type. In this case, we should try to reduce the type
770-
// and compare the redux. This is done in fourthTry
767+
if base.exists && base.ne(tp1) && (!caseLambda.exists || allowBaseTypeForMatchSelector(tp1)) then
768+
isSubType(base, tp2, if (tp1.isRef(cls2)) approx else approx.addLow)
769+
|| base.isInstanceOf[OrType] && fourthTry
770+
// if base is a disjunction, this might have come from a tp1 type that
771+
// expands to a match type. In this case, we should try to reduce the type
772+
// and compare the redux. This is done in fourthTry
771773
else fourthTry
772-
}
773774

774775
def fourthTry: Boolean = tp1 match {
775776
case tp1: TypeRef =>

tests/pos/i15319.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type FAux[A0] = Object{type A = A0}
2+
3+
type F[t] =
4+
t match
5+
case FAux[a] => a
6+
7+
val a: F[{type A = Int}] = 10
8+
val b: F[{type A = String}] = "asd"

0 commit comments

Comments
 (0)