Skip to content

Commit

Permalink
Emit unicode instead of octal literals
Browse files Browse the repository at this point in the history
fixes #13183
Both in -Xprint and in macros, there were octal literals generated. This commit replaces that by unicode.
  • Loading branch information
ansvonwa committed Aug 9, 2021
1 parent 38b983c commit 86e17a1
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 3 deletions.
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import typer.Implicits._
import typer.ImportInfo
import Variances.varianceSign
import util.SourcePosition
import java.lang.Integer.toOctalString
import scala.util.control.NonFatal
import scala.annotation.switch

Expand Down Expand Up @@ -525,7 +524,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
case '"' => "\\\""
case '\'' => "\\\'"
case '\\' => "\\\\"
case _ => if (ch.isControl) "\\0" + toOctalString(ch) else String.valueOf(ch)
case _ => if (ch.isControl) "\\u%04x".format(ch.toInt) else String.valueOf(ch)
}

def toText(const: Constant): Text = const.tag match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ object SourceCode {
case '"' => "\\\""
case '\'' => "\\\'"
case '\\' => "\\\\"
case _ => if (ch.isControl) "\\0" + Integer.toOctalString(ch) else String.valueOf(ch)
case _ => if (ch.isControl) "\\u%04x".format(ch.toInt) else String.valueOf(ch)
}

private def escapedString(str: String): String = str flatMap escapedChar
Expand Down
5 changes: 5 additions & 0 deletions tests/run-macros/i13183.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"\u001b"
"\u0000\u0001\u0003"
"ABC"
"\u0080\u0081翿"
"\t\n\r👋👌🥳"
5 changes: 5 additions & 0 deletions tests/run-macros/i13183/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import scala.quoted.*

object Macro_1:
inline def stringLiteral(inline s: String): String = ${showExpr('s)}
def showExpr(s: Expr[?])(using Quotes): Expr[String] = Expr(s.show.toString)
6 changes: 6 additions & 0 deletions tests/run-macros/i13183/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@main def Test =
println(Macro_1.stringLiteral("\u001b")) // "\u001b"
println(Macro_1.stringLiteral("\u0000\u0001\u0003")) // "\u0000\u0001\u0003"
println(Macro_1.stringLiteral("A\u0042C")) // "ABC"
println(Macro_1.stringLiteral("\u0080\u0081\u7fff")) // "\u0080\u0081翿"
println(Macro_1.stringLiteral("\t\n\r👋👌🥳")) // "\t\n\r👋👌🥳"
1 change: 1 addition & 0 deletions tests/run/i13183.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"\u001b"
2 changes: 2 additions & 0 deletions tests/run/i13183.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@main def Test(): Unit =
println(scala.compiletime.codeOf("\u001b"))

0 comments on commit 86e17a1

Please sign in to comment.