Skip to content

Fix #4227: handle singleton types in exhaustivity check #4233

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 3 commits into from
Apr 3, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ object messages {
val msg =
hl"""|match may not be exhaustive.
|
|It would fail on: $uncovered"""
|It would fail on pattern case: $uncovered"""


val explanation =
Expand Down
9 changes: 6 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -400,21 +400,21 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {

val sig =
if (isSyntheticScala2Unapply(unappSym) && caseAccessors.length == argLen)
caseAccessors.map(_.info.asSeenFrom(mt.paramInfos.head, caseClass).widen)
caseAccessors.map(_.info.asSeenFrom(mt.paramInfos.head, caseClass).widenExpr)
else if (mt.finalResultType.isRef(defn.BooleanClass))
List()
else {
val isUnapplySeq = unappSym.name == nme.unapplySeq
if (isProductMatch(mt.finalResultType, argLen) && !isUnapplySeq) {
productSelectors(mt.finalResultType).take(argLen)
.map(_.info.asSeenFrom(mt.finalResultType, mt.resultType.classSymbol).widen)
.map(_.info.asSeenFrom(mt.finalResultType, mt.resultType.classSymbol).widenExpr)
}
else {
val resTp = mt.finalResultType.select(nme.get).finalResultType.widen
if (isUnapplySeq) scalaListType.appliedTo(resTp.argTypes.head) :: Nil
else if (argLen == 0) Nil
else if (isProductMatch(resTp, argLen))
productSelectors(resTp).map(_.info.asSeenFrom(resTp, resTp.classSymbol).widen)
productSelectors(resTp).map(_.info.asSeenFrom(resTp, resTp.classSymbol).widenExpr)
else resTp :: Nil
}
}
Expand Down Expand Up @@ -443,6 +443,8 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
Typ(ConstantType(Constant(true)), true),
Typ(ConstantType(Constant(false)), true)
)
case tp if tp.isRef(defn.UnitClass) =>
Typ(ConstantType(Constant(())), true) :: Nil
case tp if tp.classSymbol.is(Enum) =>
children.map(sym => Typ(sym.termRef, true))
case tp =>
Expand Down Expand Up @@ -708,6 +710,7 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
canDecompose(and.tp1) || canDecompose(and.tp2)
}) ||
tp.isRef(defn.BooleanClass) ||
tp.isRef(defn.UnitClass) ||
tp.classSymbol.is(allOf(Enum, Sealed)) // Enum value doesn't have Sealed flag

debug.println(s"decomposable: ${tp.show} = $res")
Expand Down
6 changes: 6 additions & 0 deletions tests/patmat/i4227.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object Test {
def foo(x: Option[1]) = x match {
case Some(1) =>
case None =>
}
}
16 changes: 16 additions & 0 deletions tests/patmat/i4227b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
sealed abstract class Maybe[A]
final case class Just[A](a: A) extends Maybe[A]
class Empty[A] extends Maybe[A]
object Empty {
def apply[A](): Maybe[A] = new Empty[A]
def unapply[A](e: Empty[A]): Some[Unit] = Some(())
}

object Test {
val a: Maybe[Int] = Just(2)
def main(args: Array[String]): Unit = a match {
case Just(_) =>
// case Empty(_) => // ok
case Empty(()) => // match may not be exhaustive. It would fail on: Empty(_)
}
}