Skip to content

Commit a601d9d

Browse files
committed
Address feedback: refine printing for unapplySeq
1 parent 8ceb525 commit a601d9d

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

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

+14-9
Original file line numberDiff line numberDiff line change
@@ -695,19 +695,21 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
695695
companion.findMember(nme.unapplySeq, NoPrefix, required = EmptyFlags, excluded = Synthetic).exists
696696
}
697697

698-
def doShow(s: Space, mergeList: Boolean = false): String = s match {
698+
def doShow(s: Space, flattenList: Boolean = false): String = s match {
699699
case Empty => ""
700700
case Typ(c: ConstantType, _) => "" + c.value.value
701-
case Typ(tp: TermRef, _) => tp.symbol.showName
701+
case Typ(tp: TermRef, _) =>
702+
if (flattenList && tp <:< scalaNilType) ""
703+
else tp.symbol.showName
702704
case Typ(tp, decomposed) =>
703705
val sym = tp.widen.classSymbol
704706

705707
if (ctx.definitions.isTupleType(tp))
706708
params(tp).map(_ => "_").mkString("(", ", ", ")")
707709
else if (scalaListType.isRef(sym))
708-
if (mergeList) "_: _*" else "_: List"
710+
if (flattenList) "_: _*" else "_: List"
709711
else if (scalaConsType.isRef(sym))
710-
if (mergeList) "_, _: _*" else "List(_, _: _*)"
712+
if (flattenList) "_, _: _*" else "List(_, _: _*)"
711713
else if (tp.classSymbol.is(Sealed) && tp.classSymbol.hasAnonymousChild)
712714
"_: " + showType(tp) + " (anonymous)"
713715
else if (tp.classSymbol.is(CaseClass) && !hasCustomUnapply(tp.classSymbol))
@@ -719,15 +721,18 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
719721
if (ctx.definitions.isTupleType(tp))
720722
"(" + params.map(doShow(_)).mkString(", ") + ")"
721723
else if (tp.isRef(scalaConsType.symbol))
722-
if (mergeList) params.map(doShow(_, mergeList)).mkString(", ")
723-
else params.map(doShow(_, true)).filter(_ != "Nil").mkString("List(", ", ", ")")
724-
else
725-
showType(sym.owner.typeRef) + params.map(doShow(_)).mkString("(", ", ", ")")
724+
if (flattenList) params.map(doShow(_, flattenList)).mkString(", ")
725+
else params.map(doShow(_, flattenList = true)).filter(!_.isEmpty).mkString("List(", ", ", ")")
726+
else {
727+
val isUnapplySeq = sym.name.eq(nme.unapplySeq)
728+
val paramsStr = params.map(doShow(_, flattenList = isUnapplySeq)).mkString("(", ", ", ")")
729+
showType(sym.owner.typeRef) + paramsStr
730+
}
726731
case Or(_) =>
727732
throw new Exception("incorrect flatten result " + s)
728733
}
729734

730-
flatten(s).map(doShow(_, false)).distinct.mkString(", ")
735+
flatten(s).map(doShow(_, flattenList = false)).distinct.mkString(", ")
731736
}
732737

733738
private def exhaustivityCheckable(sel: Tree): Boolean = {

tests/patmat/i6490.check

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8: Pattern Match Exhaustivity: Foo(Nil)
1+
8: Pattern Match Exhaustivity: Foo()

tests/patmat/t9232.check

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
13: Pattern Match Exhaustivity: Node2()
2-
17: Pattern Match Exhaustivity: Node1(Foo(List(_, _: _*))), Node1(Foo(Nil)), Node2()
3-
21: Pattern Match Exhaustivity: Node1(Foo(Nil)), Node2()
2+
17: Pattern Match Exhaustivity: Node1(Foo(_, _: _*)), Node1(Foo()), Node2()
3+
21: Pattern Match Exhaustivity: Node1(Foo()), Node2()

0 commit comments

Comments
 (0)