Skip to content

Commit 6d18c2d

Browse files
authored
Merge pull request #2504 from dotty-staging/fix-2502
Fix #2502: improve warning message of exhaustivity check
2 parents d02bf3b + 14276cd commit 6d18c2d

File tree

7 files changed

+52
-5
lines changed

7 files changed

+52
-5
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,12 @@ object messages {
794794
|It would fail on: $uncovered"""
795795

796796

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

800805
case class MatchCaseUnreachable()(implicit ctx: Context)

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,14 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
641641

642642
/** Display spaces */
643643
def show(s: Space): String = {
644+
645+
/** does the companion object of the given symbol have custom unapply */
646+
def hasCustomUnapply(sym: Symbol): Boolean = {
647+
val companion = sym.companionModule
648+
companion.findMember(nme.unapply, NoPrefix, excluded = Synthetic).exists ||
649+
companion.findMember(nme.unapplySeq, NoPrefix, excluded = Synthetic).exists
650+
}
651+
644652
def doShow(s: Space, mergeList: Boolean = false): String = s match {
645653
case Empty => ""
646654
case Typ(c: ConstantType, _) => c.value.show
@@ -654,11 +662,9 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
654662
if (mergeList) "_*" else "_: List"
655663
else if (scalaConsType.isRef(sym))
656664
if (mergeList) "_" else "List(_)"
657-
else if (tp.classSymbol.is(CaseClass))
665+
else if (tp.classSymbol.is(CaseClass) && !hasCustomUnapply(tp.classSymbol))
658666
// use constructor syntax for case class
659667
showType(tp) + signature(tp).map(_ => "_").mkString("(", ", ", ")")
660-
else if (signature(tp).nonEmpty)
661-
tp.classSymbol.name + signature(tp).map(_ => "_").mkString("(", ", ", ")")
662668
else if (decomposed) "_: " + showType(tp)
663669
else "_"
664670
case Kon(tp, params) =>

tests/patmat/i2502.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5: Pattern Match Exhaustivity: _: BTypes.ClassBType

tests/patmat/i2502.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
abstract class BTypes {
2+
trait BType
3+
4+
sealed trait RefBType extends BType {
5+
def classOrArrayType: String = this match {
6+
case ClassBType(internalName) => internalName
7+
case a: ArrayBType => ""
8+
}
9+
}
10+
11+
final class ClassBType(val internalName: String) extends RefBType
12+
class ArrayBType extends RefBType
13+
14+
object ClassBType {
15+
def unapply(x: ClassBType): Option[String] = None
16+
}
17+
}

tests/patmat/i2502b.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5: Pattern Match Exhaustivity: _: BTypes.ClassBType

tests/patmat/i2502b.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
abstract class BTypes {
2+
trait BType
3+
4+
sealed trait RefBType extends BType {
5+
def classOrArrayType: String = this match {
6+
case ClassBType(internalName) => internalName
7+
case a: ArrayBType => ""
8+
}
9+
}
10+
11+
final case class ClassBType(val internalName: String) extends RefBType
12+
class ArrayBType extends RefBType
13+
14+
object ClassBType {
15+
def unapply(x: RefBType): Option[String] = None
16+
}
17+
}

tests/patmat/t4691.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
15: Pattern Match Exhaustivity: NodeType2(_)
1+
15: Pattern Match Exhaustivity: _: NodeType2

0 commit comments

Comments
 (0)