Skip to content

Commit 07285c0

Browse files
committed
Enable exhaustivity checks on and-types
1 parent d514638 commit 07285c0

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,12 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
325325
debug.println(s"candidates for ${tp.show} : [${children.map(_.show).mkString(", ")}]")
326326

327327
tp.dealias match {
328+
case AndType(tp1, tp2) =>
329+
intersect(Typ(tp1, false), Typ(tp2, false)) match {
330+
case Or(spaces) => spaces
331+
case Empty => Nil
332+
case space => List(space)
333+
}
328334
case OrType(tp1, tp2) => List(Typ(tp1, true), Typ(tp2, true))
329335
case _ if tp =:= ctx.definitions.BooleanType =>
330336
List(
@@ -380,6 +386,10 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
380386
val res = tp.classSymbol.is(allOf(Abstract, Sealed)) ||
381387
tp.classSymbol.is(allOf(Trait, Sealed)) ||
382388
tp.dealias.isInstanceOf[OrType] ||
389+
(tp.isInstanceOf[AndType] && {
390+
val and = tp.asInstanceOf[AndType]
391+
canDecompose(and.tp1) || canDecompose(and.tp2)
392+
}) ||
383393
tp =:= ctx.definitions.BooleanType ||
384394
tp.classSymbol.is(allOf(Enum, Sealed)) // Enum value doesn't have Sealed flag
385395

@@ -477,6 +487,10 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
477487
ctx.settings.YcheckAllPatmat.value ||
478488
tp.typeSymbol.is(Sealed) ||
479489
tp.isInstanceOf[OrType] ||
490+
(tp.isInstanceOf[AndType] && {
491+
val and = tp.asInstanceOf[AndType]
492+
isCheckable(and.tp1) || isCheckable(and.tp2)
493+
}) ||
480494
tp.typeSymbol == ctx.definitions.BooleanType.typeSymbol ||
481495
tp.typeSymbol.is(Enum) ||
482496
canDecompose(tp) ||

tests/patmat/andtype.check

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
9: Pattern Match Exhaustivity: _: B
2+
13: Pattern Match Exhaustivity: _: B
3+
17: Pattern Match Exhaustivity: _: B

tests/patmat/andtype.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
object Test {
2+
trait Marker
3+
4+
sealed trait T
5+
trait A extends T with Marker
6+
trait B extends T with Marker
7+
case object C extends T
8+
9+
def m1(s: T & Marker) = s match {
10+
case _: A => ;
11+
}
12+
13+
def m2(s: Marker & T) = s match {
14+
case _: A => ;
15+
}
16+
17+
def m3(s: (A | B) & Marker) = s match {
18+
case _: A => ;
19+
}
20+
}

0 commit comments

Comments
 (0)