Skip to content

Commit 86c9a7e

Browse files
authored
Merge pull request scala#13273 from ansvonwa/fix-13183
Emit unicode instead of octal literals
2 parents 26a27cb + b261427 commit 86c9a7e

File tree

7 files changed

+21
-3
lines changed

7 files changed

+21
-3
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import typer.Implicits._
1212
import typer.ImportInfo
1313
import Variances.varianceSign
1414
import util.SourcePosition
15-
import java.lang.Integer.toOctalString
1615
import scala.util.control.NonFatal
1716
import scala.annotation.switch
1817

@@ -525,7 +524,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
525524
case '"' => "\\\""
526525
case '\'' => "\\\'"
527526
case '\\' => "\\\\"
528-
case _ => if (ch.isControl) "\\0" + toOctalString(ch) else String.valueOf(ch)
527+
case _ => if (ch.isControl) f"\u${ch.toInt}%04x" else String.valueOf(ch)
529528
}
530529

531530
def toText(const: Constant): Text = const.tag match {

compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ object SourceCode {
14191419
case '"' => "\\\""
14201420
case '\'' => "\\\'"
14211421
case '\\' => "\\\\"
1422-
case _ => if (ch.isControl) "\\0" + Integer.toOctalString(ch) else String.valueOf(ch)
1422+
case _ => if (ch.isControl) f"\u${ch.toInt}%04x" else String.valueOf(ch)
14231423
}
14241424

14251425
private def escapedString(str: String): String = str flatMap escapedChar

tests/run-macros/i13183.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"\u001b"
2+
"\u0000\u0001\u0003"
3+
"ABC"
4+
"\u0080\u0081翿"
5+
"\t\n\r👋👌🥳"

tests/run-macros/i13183/Macro_1.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import scala.quoted.*
2+
3+
object Macro_1:
4+
inline def stringLiteral(inline s: String): String = ${showExpr('s)}
5+
def showExpr(s: Expr[?])(using Quotes): Expr[String] = Expr(s.show.toString)

tests/run-macros/i13183/Test_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@main def Test =
2+
println(Macro_1.stringLiteral("\u001b")) // "\u001b"
3+
println(Macro_1.stringLiteral("\u0000\u0001\u0003")) // "\u0000\u0001\u0003"
4+
println(Macro_1.stringLiteral("A\u0042C")) // "ABC"
5+
println(Macro_1.stringLiteral("\u0080\u0081\u7fff")) // "\u0080\u0081翿"
6+
println(Macro_1.stringLiteral("\t\n\r👋👌🥳")) // "\t\n\r👋👌🥳"

tests/run/i13183.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"\u001b"

tests/run/i13183.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@main def Test(): Unit =
2+
println(scala.compiletime.codeOf("\u001b"))

0 commit comments

Comments
 (0)