Skip to content

Commit e820624

Browse files
committed
address review feedback
1 parent 62d73ea commit e820624

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,9 @@ object messages {
791791

792792
val explanation =
793793
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
794+
| - Add missing cases as shown in the warning
795+
| - If an extractor always return Some(...), write Some[X] for its return type
796+
| - Add a case _ => ... at the end to match all remaining cases
797797
|"""
798798
}
799799

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, Flags.Synthetic).exists ||
649+
companion.findMember(nme.unapplySeq, NoPrefix, Flags.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 (sym.is(Flags.Case) && 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/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+
}

0 commit comments

Comments
 (0)