From 27ae72074e4e068691a4b5a036cc6a973c2d0a25 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Sat, 24 Sep 2022 17:25:18 +0100 Subject: [PATCH] Change Show Tuple2 instance to generic tuple I tried to show a Tuple3 and it fell back to Show[Product] (which then calls toString). So let's use our generic tuples - don't even need a macro! --- compiler/src/dotty/tools/dotc/printing/Formatting.scala | 6 +++--- compiler/test/dotty/tools/dotc/StringFormatterTest.scala | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/printing/Formatting.scala b/compiler/src/dotty/tools/dotc/printing/Formatting.scala index 348390d9c7e2..f85845517d8c 100644 --- a/compiler/src/dotty/tools/dotc/printing/Formatting.scala +++ b/compiler/src/dotty/tools/dotc/printing/Formatting.scala @@ -58,9 +58,9 @@ object Formatting { def show(x: Seq[X]) = new CtxShow: def run(using Context) = x.map(show1) - given [A: Show, B: Show]: Show[(A, B)] with - def show(x: (A, B)) = new CtxShow: - def run(using Context) = (show1(x._1), show1(x._2)) + given [H: Show, T <: Tuple: Show]: Show[H *: T] with + def show(x: H *: T) = new CtxShow: + def run(using Context) = show1(x.head) *: Show[T].show(x.tail).ctxShow.asInstanceOf[Tuple] given [X: Show]: Show[X | Null] with def show(x: X | Null) = if x == null then "null" else Show[X].show(x.nn) diff --git a/compiler/test/dotty/tools/dotc/StringFormatterTest.scala b/compiler/test/dotty/tools/dotc/StringFormatterTest.scala index 7df64ad5bf3f..a5b5902b1f2e 100644 --- a/compiler/test/dotty/tools/dotc/StringFormatterTest.scala +++ b/compiler/test/dotty/tools/dotc/StringFormatterTest.scala @@ -22,6 +22,7 @@ class StringFormatterTest extends AbstractStringFormatterTest: @Test def flagsSeq = check(", final", i"${Seq(JavaStatic, Final)}%, %") @Test def flagsTup = check("(,final)", i"${(JavaStatic, Final)}") @Test def seqOfTup2 = check("(final,given), (private,lazy)", i"${Seq((Final, Given), (Private, Lazy))}%, %") + @Test def seqOfTup3 = check("(Foo,given, (right is approximated))", i"${Seq((Foo, Given, TypeComparer.ApproxState.None.addHigh))}%, %") class StorePrinter extends Printer: var string: String = "" @@ -76,6 +77,11 @@ class ExStringFormatterTest extends AbstractStringFormatterTest: |where: Foo is a type | Foo² is a type |""".stripMargin, ex"${(Foo, Foo)}") + @Test def seqOfTup3Amb = check("""[(Foo,Foo²,type Err)] + | + |where: Foo is a type + | Foo² is a type + |""".stripMargin, ex"${Seq((Foo, Foo, Err))}") end ExStringFormatterTest abstract class AbstractStringFormatterTest extends DottyTest: