Skip to content

Emit unicode instead of octal literals #13273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) f"\u${ch.toInt}%04x" 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) f"\u${ch.toInt}%04x" 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"))