Skip to content

Fix #2502: improve warning message of exhaustivity check #2504

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 25, 2017
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 @@ -789,7 +789,12 @@ object messages {
|It would fail on: $uncovered"""


val explanation = ""
val explanation =
hl"""|There are several ways to make the match exhaustive:
| - Add missing cases as shown in the warning
| - If an extractor always return 'Some(...)', write 'Some[X]' for its return type
| - Add a 'case _ => ...' at the end to match all remaining cases
|"""
}

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

/** Display spaces */
def show(s: Space): String = {

/** does the companion object of the given symbol have custom unapply */
def hasCustomUnapply(sym: Symbol): Boolean = {
val companion = sym.companionModule
companion.findMember(nme.unapply, NoPrefix, excluded = Synthetic).exists ||
companion.findMember(nme.unapplySeq, NoPrefix, excluded = Synthetic).exists
}

def doShow(s: Space, mergeList: Boolean = false): String = s match {
case Empty => ""
case Typ(c: ConstantType, _) => c.value.show
Expand All @@ -654,11 +662,9 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
if (mergeList) "_*" else "_: List"
else if (scalaConsType.isRef(sym))
if (mergeList) "_" else "List(_)"
else if (tp.classSymbol.is(CaseClass))
else if (tp.classSymbol.is(CaseClass) && !hasCustomUnapply(tp.classSymbol))
// use constructor syntax for case class
showType(tp) + signature(tp).map(_ => "_").mkString("(", ", ", ")")
else if (signature(tp).nonEmpty)
tp.classSymbol.name + signature(tp).map(_ => "_").mkString("(", ", ", ")")
else if (decomposed) "_: " + showType(tp)
else "_"
case Kon(tp, params) =>
Expand Down
1 change: 1 addition & 0 deletions tests/patmat/i2502.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5: Pattern Match Exhaustivity: _: BTypes.ClassBType
17 changes: 17 additions & 0 deletions tests/patmat/i2502.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
abstract class BTypes {
trait BType

sealed trait RefBType extends BType {
def classOrArrayType: String = this match {
case ClassBType(internalName) => internalName
case a: ArrayBType => ""
}
}

final class ClassBType(val internalName: String) extends RefBType
class ArrayBType extends RefBType

object ClassBType {
def unapply(x: ClassBType): Option[String] = None
}
}
1 change: 1 addition & 0 deletions tests/patmat/i2502b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5: Pattern Match Exhaustivity: _: BTypes.ClassBType
17 changes: 17 additions & 0 deletions tests/patmat/i2502b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
abstract class BTypes {
trait BType

sealed trait RefBType extends BType {
def classOrArrayType: String = this match {
case ClassBType(internalName) => internalName
case a: ArrayBType => ""
}
}

final case class ClassBType(val internalName: String) extends RefBType
class ArrayBType extends RefBType

object ClassBType {
def unapply(x: RefBType): Option[String] = None
}
}
2 changes: 1 addition & 1 deletion tests/patmat/t4691.check
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15: Pattern Match Exhaustivity: NodeType2(_)
15: Pattern Match Exhaustivity: _: NodeType2