From b26142740751d13d3d131e9505029af2afe3d4fb Mon Sep 17 00:00:00 2001 From: Anselm von Wangenheim Date: Tue, 10 Aug 2021 00:46:58 +0200 Subject: [PATCH] Emit unicode instead of octal literals fixes #13183 Both in -Xprint and in macros, there were octal literals generated. This commit replaces that by unicode. --- compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala | 3 +-- .../src/scala/quoted/runtime/impl/printers/SourceCode.scala | 2 +- tests/run-macros/i13183.check | 5 +++++ tests/run-macros/i13183/Macro_1.scala | 5 +++++ tests/run-macros/i13183/Test_2.scala | 6 ++++++ tests/run/i13183.check | 1 + tests/run/i13183.scala | 2 ++ 7 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 tests/run-macros/i13183.check create mode 100644 tests/run-macros/i13183/Macro_1.scala create mode 100644 tests/run-macros/i13183/Test_2.scala create mode 100644 tests/run/i13183.check create mode 100644 tests/run/i13183.scala diff --git a/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala b/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala index 7009a55d3aeb..34b5428d89d4 100644 --- a/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala @@ -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 @@ -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) f"\u${ch.toInt}%04x" else String.valueOf(ch) } def toText(const: Constant): Text = const.tag match { diff --git a/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala b/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala index e3f0f282e524..8677ee886c02 100644 --- a/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala +++ b/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala @@ -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) f"\u${ch.toInt}%04x" else String.valueOf(ch) } private def escapedString(str: String): String = str flatMap escapedChar diff --git a/tests/run-macros/i13183.check b/tests/run-macros/i13183.check new file mode 100644 index 000000000000..49dac665435e --- /dev/null +++ b/tests/run-macros/i13183.check @@ -0,0 +1,5 @@ +"\u001b" +"\u0000\u0001\u0003" +"ABC" +"\u0080\u0081ēææ" +"\t\n\ršŸ‘‹šŸ‘ŒšŸ„³" diff --git a/tests/run-macros/i13183/Macro_1.scala b/tests/run-macros/i13183/Macro_1.scala new file mode 100644 index 000000000000..4400a4497cb8 --- /dev/null +++ b/tests/run-macros/i13183/Macro_1.scala @@ -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) diff --git a/tests/run-macros/i13183/Test_2.scala b/tests/run-macros/i13183/Test_2.scala new file mode 100644 index 000000000000..ae5897eb95b0 --- /dev/null +++ b/tests/run-macros/i13183/Test_2.scala @@ -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šŸ‘‹šŸ‘ŒšŸ„³" diff --git a/tests/run/i13183.check b/tests/run/i13183.check new file mode 100644 index 000000000000..2dc88a12f2d4 --- /dev/null +++ b/tests/run/i13183.check @@ -0,0 +1 @@ +"\u001b" diff --git a/tests/run/i13183.scala b/tests/run/i13183.scala new file mode 100644 index 000000000000..128f42b162f1 --- /dev/null +++ b/tests/run/i13183.scala @@ -0,0 +1,2 @@ +@main def Test(): Unit = + println(scala.compiletime.codeOf("\u001b"))