Skip to content

Commit 62d73ea

Browse files
committed
fix #2502: improve warning message of exhaustivity check
1 parent 34b98f3 commit 62d73ea

File tree

5 files changed

+26
-3
lines changed

5 files changed

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

791791

792-
val explanation = ""
792+
val explanation =
793+
hl"""|There are several ways to make the match exhaustive:
794+
| - add missing cases as shown in the warning
795+
| - change the return type of irrefutable extractors as `Some[T]` if exist
796+
| - add a wildcard `_` case at the end
797+
|"""
793798
}
794799

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
657657
else if (tp.classSymbol.is(CaseClass))
658658
// use constructor syntax for case class
659659
showType(tp) + signature(tp).map(_ => "_").mkString("(", ", ", ")")
660-
else if (signature(tp).nonEmpty)
660+
else if (sym.is(Flags.Case) && signature(tp).nonEmpty)
661661
tp.classSymbol.name + signature(tp).map(_ => "_").mkString("(", ", ", ")")
662662
else if (decomposed) "_: " + showType(tp)
663663
else "_"

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/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)