Skip to content

Commit 6a094ab

Browse files
Merge pull request #4644 from dotty-staging/fix-decompiler-escape-chars
Escape chars when printing strings in decompiler
2 parents 8d2d20d + 4d01607 commit 6a094ab

File tree

4 files changed

+21
-24
lines changed

4 files changed

+21
-24
lines changed

compiler/src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ object StdNames {
688688
class ScalaTermNames extends ScalaNames[TermName] {
689689
protected implicit def fromString(s: String): TermName = termName(s)
690690

691-
@switch def syntheticParamName(i: Int): TermName = i match {
691+
def syntheticParamName(i: Int): TermName = (i: @switch) match {
692692
case 0 => x_0
693693
case 1 => x_1
694694
case 2 => x_2
@@ -702,7 +702,7 @@ object StdNames {
702702
case _ => termName("x$" + i)
703703
}
704704

705-
@switch def productAccessorName(j: Int): TermName = j match {
705+
def productAccessorName(j: Int): TermName = (j: @switch) match {
706706
case 1 => nme._1
707707
case 2 => nme._2
708708
case 3 => nme._3

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
443443

444444
def toText(denot: Denotation): Text = toText(denot.symbol) ~ "/D"
445445

446-
@switch private def escapedChar(ch: Char): String = ch match {
446+
private def escapedChar(ch: Char): String = (ch: @switch) match {
447447
case '\b' => "\\b"
448448
case '\t' => "\\t"
449449
case '\n' => "\\n"

compiler/src/dotty/tools/dotc/quoted/Toolbox.scala

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
package dotty.tools.dotc.quoted
22

33
import dotty.tools.dotc.ast.tpd
4-
import dotty.tools.dotc.core.Contexts.Context
5-
import dotty.tools.dotc.core.Constants._
6-
import dotty.tools.dotc.core.quoted.PickledQuotes
7-
import dotty.tools.dotc.printing.RefinedPrinter
84

95
import scala.quoted.Expr
10-
import scala.runtime.BoxedUnit
116
import scala.quoted.Exprs.{LiftedExpr, TastyTreeExpr}
127
import scala.runtime.quoted._
138

@@ -32,20 +27,7 @@ object Toolbox {
3227
new QuoteDriver().run(expr, runSettings)
3328
}
3429

35-
def show(expr: Expr[T]): String = expr match {
36-
case expr: LiftedExpr[T] =>
37-
expr.value match {
38-
case value: Class[_] => s"classOf[${value.getCanonicalName}]"
39-
case value if value == BoxedUnit.UNIT => "()"
40-
case value =>
41-
implicit val ctx = new QuoteDriver().initCtx
42-
if (showSettings.compilerArgs.contains("-color:never"))
43-
ctx.settings.color.update("never")
44-
val printer = new RefinedPrinter(ctx)
45-
printer.toText(Literal(Constant(value))).mkString(Int.MaxValue, false)
46-
}
47-
case _ => new QuoteDriver().show(expr, showSettings)
48-
}
30+
def show(expr: Expr[T]): String = new QuoteDriver().show(expr, showSettings)
4931

5032
}
5133

library/src/scala/tasty/util/ShowSourceCode.scala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package scala.tasty
22
package util
33

4+
import scala.annotation.switch
5+
46
class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty0) {
57
import tasty._
68

@@ -591,8 +593,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
591593
case Constant.Long(v) => this += v += "L"
592594
case Constant.Float(v) => this += v
593595
case Constant.Double(v) => this += v
594-
case Constant.Char(v) => this += '\'' += v.toString += '\'' // TODO escape char
595-
case Constant.String(v) => this += '"' += v.toString += '"' // TODO escape string
596+
case Constant.Char(v) => this += '\'' += escapedChar(v) += '\''
597+
case Constant.String(v) => this += '"' += escapedString(v) += '"'
596598
}
597599

598600
def printTypeOrBoundsTree(tpt: TypeOrBoundsTree): Buffer = tpt match {
@@ -770,6 +772,19 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
770772
def +=(x: Char): this.type = { sb.append(x); this }
771773
def +=(x: String): this.type = { sb.append(x); this }
772774

775+
private def escapedChar(ch: Char): String = (ch: @switch) match {
776+
case '\b' => "\\b"
777+
case '\t' => "\\t"
778+
case '\n' => "\\n"
779+
case '\f' => "\\f"
780+
case '\r' => "\\r"
781+
case '"' => "\\\""
782+
case '\'' => "\\\'"
783+
case '\\' => "\\\\"
784+
case _ => if (ch.isControl) "\\0" + Integer.toOctalString(ch) else String.valueOf(ch)
785+
}
786+
787+
private def escapedString(str: String): String = str flatMap escapedChar
773788
}
774789

775790

0 commit comments

Comments
 (0)