Skip to content

Commit e555ee0

Browse files
committed
Fix #12337: Enable exhaustivity check for case classes with checkable components
This aligns with Scala 2 behavior.
1 parent 782f57e commit e555ee0

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -791,16 +791,18 @@ class SpaceEngine(using Context) extends SpaceLogic {
791791
def isCheckable(tp: Type): Boolean =
792792
!tp.hasAnnotation(defn.UncheckedAnnot) && {
793793
val tpw = tp.widen.dealias
794+
val classSym = tpw.classSymbol
794795
ctx.settings.YcheckAllPatmat.value ||
795-
tpw.typeSymbol.is(Sealed) ||
796+
classSym.is(Sealed) ||
796797
tpw.isInstanceOf[OrType] ||
797798
(tpw.isInstanceOf[AndType] && {
798799
val and = tpw.asInstanceOf[AndType]
799800
isCheckable(and.tp1) || isCheckable(and.tp2)
800801
}) ||
801802
tpw.isRef(defn.BooleanClass) ||
802-
tpw.typeSymbol.isAllOf(JavaEnumTrait) ||
803-
(defn.isTupleType(tpw) && tpw.argInfos.exists(isCheckable(_)))
803+
classSym.isAllOf(JavaEnumTrait) ||
804+
(defn.isProductSubType(tpw) && classSym.is(Case)
805+
&& productSelectorTypes(tpw, sel.srcPos).exists(isCheckable(_)))
804806
}
805807

806808
val res = isCheckable(sel.tpe)

tests/patmat/i12337.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8: Pattern Match Exhaustivity: Foo(Inactive)

tests/patmat/i12337.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
sealed trait Status
2+
object Status {
3+
case object Active extends Status
4+
case object Inactive extends Status
5+
}
6+
7+
case class Foo(status: Status)
8+
def bar(user: Foo): Unit = user match {
9+
case Foo(Status.Active) =>
10+
println("active")
11+
// no compile-time warning for missing Status.Inactive case
12+
}

0 commit comments

Comments
 (0)