Skip to content

Fix #12337: Enable exhaustivity check for case classes with checkable components #12377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/backend/jvm/BCodeBodyBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
resKind
}

def genPrimitiveOp(tree: Apply, expectedType: BType): BType = tree match {
def genPrimitiveOp(tree: Apply, expectedType: BType): BType = (tree: @unchecked) match {
case Apply(fun @ DesugaredSelect(receiver, _), _) =>
val sym = tree.symbol

Expand Down Expand Up @@ -610,7 +610,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
}
}

def genTypeApply(t: TypeApply): BType = t match {
def genTypeApply(t: TypeApply): BType = (t: @unchecked) match {
case TypeApply(fun@DesugaredSelect(obj, _), targs) =>

val sym = fun.symbol
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/backend/jvm/BCodeSyncAndTry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ trait BCodeSyncAndTry extends BCodeBodyBuilder {
*/
abstract class SyncAndTryBuilder(cunit: CompilationUnit) extends PlainBodyBuilder(cunit) {

def genSynchronized(tree: Apply, expectedType: BType): BType = tree match {
def genSynchronized(tree: Apply, expectedType: BType): BType = (tree: @unchecked) match {
case Apply(TypeApply(fun, _), args) =>
val monitor = locals.makeLocal(ObjectReference, "monitor", defn.ObjectType, tree.span)
val monCleanup = new asm.Label
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class StringInterpolatorOpt extends MiniPhase {
(sym.name == nme.f && sym.eq(defn.StringContext_f)) ||
(sym.name == nme.s && sym.eq(defn.StringContext_s))
if (isInterpolatedMethod)
tree match {
(tree: @unchecked) match {
case StringContextIntrinsic(strs: List[Literal], elems: List[Tree]) =>
val stri = strs.iterator
val elemi = elems.iterator
Expand Down
8 changes: 5 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -791,16 +791,18 @@ class SpaceEngine(using Context) extends SpaceLogic {
def isCheckable(tp: Type): Boolean =
!tp.hasAnnotation(defn.UncheckedAnnot) && {
val tpw = tp.widen.dealias
val classSym = tpw.classSymbol
ctx.settings.YcheckAllPatmat.value ||
tpw.typeSymbol.is(Sealed) ||
classSym.is(Sealed) ||
tpw.isInstanceOf[OrType] ||
(tpw.isInstanceOf[AndType] && {
val and = tpw.asInstanceOf[AndType]
isCheckable(and.tp1) || isCheckable(and.tp2)
}) ||
tpw.isRef(defn.BooleanClass) ||
tpw.typeSymbol.isAllOf(JavaEnumTrait) ||
(defn.isTupleType(tpw) && tpw.argInfos.exists(isCheckable(_)))
classSym.isAllOf(JavaEnumTrait) ||
(defn.isProductSubType(tpw) && classSym.is(Case)
&& productSelectorTypes(tpw, sel.srcPos).exists(isCheckable(_)))
}

val res = isCheckable(sel.tpe)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ trait Applications extends Compatibility {
* { val xs = es; e' = e' + args }
*/
def typedOpAssign(using Context): Tree = {
val (lhs1, name, rhss) = tree match
val (lhs1, name, rhss) = (tree: @unchecked) match
case Apply(Select(lhs, name), rhss) => (typedExpr(lhs), name, rhss)
case Apply(untpd.TypedSplice(Select(lhs1, name)), rhss) => (lhs1, name, rhss)
val liftedDefs = new mutable.ListBuffer[Tree]
Expand Down
1 change: 1 addition & 0 deletions tests/neg-custom-args/isInstanceOf/enum-approx2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ class Test {
def eval(e: Fun[Int, Int]) = e match {
case Fun(x: Fun[Int, Double]) => ??? // error
case Fun(x: Exp[Int => String]) => ??? // error
case _ =>
}
}
2 changes: 2 additions & 0 deletions tests/patmat/i12337.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
8: Pattern Match Exhaustivity: Foo(Inactive)
17: Pattern Match Exhaustivity: Foo(Status.Active(_))
26 changes: 26 additions & 0 deletions tests/patmat/i12337.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
sealed trait Status
object Status {
case class Active(since: Int) extends Status
case object Inactive extends Status
Copy link
Contributor

@kevin-lee kevin-lee May 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can one of these case object be case class instead to make sure it works for case class as well?

e.g.)

sealed trait Status
object Status {
  case class Active(since: Int) extends Status
  case object Inactive extends Status
}

case class Foo(status: Status)
def bar(foo: Foo): Unit = foo match {
  case Foo(Status.Active(2000)) =>
    println("active since 2000")
}
// Expected:
// warning: match may not be exhaustive.
// It would fail on the following inputs: Foo(Active((x: Int forSome x not in 2000))), Foo(Inactive)
// def bar(foo: Foo): Unit = foo match {

Copy link
Contributor

@kevin-lee kevin-lee May 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or probably adding separate cases for case class and case object might be better?

e.g.)

sealed trait Status
object Status {
  case class Active(since: Int) extends Status
  case object Inactive extends Status
}

case class Foo(status: Status)
def bar(foo: Foo): Unit = foo match {
  case Foo(Status.Active(since)) =>
    println(s"active since $since")
}
// Expected:
// warning: match may not be exhaustive.
// It would fail on the following input: Foo(Inactive)
// def bar(foo: Foo): Unit = foo match {

def baz(foo: Foo): Unit = foo match {
  case Foo(Status.Active(2000)) =>
    println("active since 2000")
  case Foo(Status.Inactive) =>
    println("inactive")
}
// Expected:
// warning: match may not be exhaustive.
// It would fail on the following input: Foo(Active((x: Int forSome x not in 2000)))
// def baz(foo: Foo): Unit = foo match {

}

case class Foo(status: Status)
def bar(foo: Foo): Unit = foo match {
case Foo(Status.Active(since)) =>
println(s"active since $since")
}
// Expected:
// warning: match may not be exhaustive.
// It would fail on the following input: Foo(Inactive)
// def bar(foo: Foo): Unit = foo match {

def baz(foo: Foo): Unit = foo match {
case Foo(Status.Active(2000)) =>
println("active since 2000")
case Foo(Status.Inactive) =>
println("inactive")
}
// Expected:
// warning: match may not be exhaustive.
// It would fail on the following input: Foo(Active((x: Int forSome x not in 2000)))
// def baz(foo: Foo): Unit = foo match {