diff --git a/compiler/src/dotty/tools/dotc/core/StdNames.scala b/compiler/src/dotty/tools/dotc/core/StdNames.scala index 1b0bac514871..ff7a3abed486 100644 --- a/compiler/src/dotty/tools/dotc/core/StdNames.scala +++ b/compiler/src/dotty/tools/dotc/core/StdNames.scala @@ -688,7 +688,7 @@ object StdNames { class ScalaTermNames extends ScalaNames[TermName] { protected implicit def fromString(s: String): TermName = termName(s) - @switch def syntheticParamName(i: Int): TermName = i match { + def syntheticParamName(i: Int): TermName = (i: @switch) match { case 0 => x_0 case 1 => x_1 case 2 => x_2 @@ -702,7 +702,7 @@ object StdNames { case _ => termName("x$" + i) } - @switch def productAccessorName(j: Int): TermName = j match { + def productAccessorName(j: Int): TermName = (j: @switch) match { case 1 => nme._1 case 2 => nme._2 case 3 => nme._3 diff --git a/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala b/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala index f3153592a73c..79c00e0f9f92 100644 --- a/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala @@ -443,7 +443,7 @@ class PlainPrinter(_ctx: Context) extends Printer { def toText(denot: Denotation): Text = toText(denot.symbol) ~ "/D" - @switch private def escapedChar(ch: Char): String = ch match { + private def escapedChar(ch: Char): String = (ch: @switch) match { case '\b' => "\\b" case '\t' => "\\t" case '\n' => "\\n" diff --git a/compiler/src/dotty/tools/dotc/quoted/Toolbox.scala b/compiler/src/dotty/tools/dotc/quoted/Toolbox.scala index 3a4fe5736362..1d369ed19057 100644 --- a/compiler/src/dotty/tools/dotc/quoted/Toolbox.scala +++ b/compiler/src/dotty/tools/dotc/quoted/Toolbox.scala @@ -1,13 +1,8 @@ package dotty.tools.dotc.quoted import dotty.tools.dotc.ast.tpd -import dotty.tools.dotc.core.Contexts.Context -import dotty.tools.dotc.core.Constants._ -import dotty.tools.dotc.core.quoted.PickledQuotes -import dotty.tools.dotc.printing.RefinedPrinter import scala.quoted.Expr -import scala.runtime.BoxedUnit import scala.quoted.Exprs.{LiftedExpr, TastyTreeExpr} import scala.runtime.quoted._ @@ -32,20 +27,7 @@ object Toolbox { new QuoteDriver().run(expr, runSettings) } - def show(expr: Expr[T]): String = expr match { - case expr: LiftedExpr[T] => - expr.value match { - case value: Class[_] => s"classOf[${value.getCanonicalName}]" - case value if value == BoxedUnit.UNIT => "()" - case value => - implicit val ctx = new QuoteDriver().initCtx - if (showSettings.compilerArgs.contains("-color:never")) - ctx.settings.color.update("never") - val printer = new RefinedPrinter(ctx) - printer.toText(Literal(Constant(value))).mkString(Int.MaxValue, false) - } - case _ => new QuoteDriver().show(expr, showSettings) - } + def show(expr: Expr[T]): String = new QuoteDriver().show(expr, showSettings) } diff --git a/library/src/scala/tasty/util/ShowSourceCode.scala b/library/src/scala/tasty/util/ShowSourceCode.scala index c4b18859deec..c5028409f022 100644 --- a/library/src/scala/tasty/util/ShowSourceCode.scala +++ b/library/src/scala/tasty/util/ShowSourceCode.scala @@ -1,6 +1,8 @@ package scala.tasty package util +import scala.annotation.switch + class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty0) { import tasty._ @@ -591,8 +593,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty case Constant.Long(v) => this += v += "L" case Constant.Float(v) => this += v case Constant.Double(v) => this += v - case Constant.Char(v) => this += '\'' += v.toString += '\'' // TODO escape char - case Constant.String(v) => this += '"' += v.toString += '"' // TODO escape string + case Constant.Char(v) => this += '\'' += escapedChar(v) += '\'' + case Constant.String(v) => this += '"' += escapedString(v) += '"' } def printTypeOrBoundsTree(tpt: TypeOrBoundsTree): Buffer = tpt match { @@ -770,6 +772,19 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty def +=(x: Char): this.type = { sb.append(x); this } def +=(x: String): this.type = { sb.append(x); this } + private def escapedChar(ch: Char): String = (ch: @switch) match { + case '\b' => "\\b" + case '\t' => "\\t" + case '\n' => "\\n" + case '\f' => "\\f" + case '\r' => "\\r" + case '"' => "\\\"" + case '\'' => "\\\'" + case '\\' => "\\\\" + case _ => if (ch.isControl) "\\0" + Integer.toOctalString(ch) else String.valueOf(ch) + } + + private def escapedString(str: String): String = str flatMap escapedChar }