From 956923acd32766c63cd0eab0788d1811481d7e30 Mon Sep 17 00:00:00 2001 From: som-snytt Date: Wed, 20 Apr 2016 07:28:54 -0700 Subject: SI-9735 REPL prefer standard escapes for code text (#5086) When constructing code text for compilation, the REPL should prefer standard escape sequences, in case unicode escapes are disabled. --- src/repl/scala/tools/nsc/interpreter/ReplStrings.scala | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/repl/scala/tools/nsc/interpreter/ReplStrings.scala b/src/repl/scala/tools/nsc/interpreter/ReplStrings.scala index 1664546cab..bf7508cb4e 100644 --- a/src/repl/scala/tools/nsc/interpreter/ReplStrings.scala +++ b/src/repl/scala/tools/nsc/interpreter/ReplStrings.scala @@ -11,13 +11,21 @@ import scala.reflect.internal.Chars trait ReplStrings { /** Convert a string into code that can recreate the string. * This requires replacing all special characters by escape - * codes. It does not add the surrounding " marks. */ + * codes. It does not add the surrounding " marks. + */ def string2code(str: String): String = { val res = new StringBuilder for (c <- str) c match { - case '"' | '\'' | '\\' => res += '\\' ; res += c - case _ if c.isControl => res ++= Chars.char2uescape(c) - case _ => res += c + case '"' => res ++= """\"""" + case '\'' => res ++= """\'""" + case '\\' => res ++= """\\""" + case '\b' => res ++= """\b""" + case '\t' => res ++= """\t""" + case '\n' => res ++= """\n""" + case '\f' => res ++= """\f""" + case '\r' => res ++= """\r""" + case _ if c.isControl => res ++= Chars.char2uescape(c) + case _ => res += c } res.toString } -- cgit v1.2.3 From 684c314d882dcfd26558c9972cfd5acabc9f051f Mon Sep 17 00:00:00 2001 From: som-snytt Date: Wed, 20 Apr 2016 07:48:57 -0700 Subject: SI-9734 Narrow type when import REPL history (#5084) Under `-Yrepl-class-based`, imports from historical `$read` instances must be singleton-typed so that path-dependent types remain so. --- src/repl/scala/tools/nsc/interpreter/Imports.scala | 2 +- test/files/run/repl-paste-6.check | 17 ++++++++++++++++ test/files/run/repl-paste-6.scala | 23 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100755 test/files/run/repl-paste-6.check create mode 100644 test/files/run/repl-paste-6.scala (limited to 'src') diff --git a/src/repl/scala/tools/nsc/interpreter/Imports.scala b/src/repl/scala/tools/nsc/interpreter/Imports.scala index 5742c1d0d8..f04e2e808c 100644 --- a/src/repl/scala/tools/nsc/interpreter/Imports.scala +++ b/src/repl/scala/tools/nsc/interpreter/Imports.scala @@ -183,7 +183,7 @@ trait Imports { case _ => val valName = req.lineRep.packageName + req.lineRep.readName if (!tempValLines.contains(req.lineRep.lineId)) { - code.append(s"val $valName = $objName\n") + code.append(s"val $valName: ${objName}.type = $objName\n") tempValLines += req.lineRep.lineId } code.append(s"import $valName${req.accessPath}.`$imv`;\n") diff --git a/test/files/run/repl-paste-6.check b/test/files/run/repl-paste-6.check new file mode 100755 index 0000000000..efcea9274f --- /dev/null +++ b/test/files/run/repl-paste-6.check @@ -0,0 +1,17 @@ + +scala> :paste < EOF +// Entering paste mode (EOF to finish) + +case class C(i: Int) +val c = C(42) +EOF + +// Exiting paste mode, now interpreting. + +defined class C +c: C = C(42) + +scala> val d: C = c // shew +d: C = C(42) + +scala> :quit diff --git a/test/files/run/repl-paste-6.scala b/test/files/run/repl-paste-6.scala new file mode 100644 index 0000000000..8040d24f8a --- /dev/null +++ b/test/files/run/repl-paste-6.scala @@ -0,0 +1,23 @@ + +import scala.tools.partest.ReplTest +import scala.tools.nsc.Settings + + +/* + * Add // show to witness: + * val $line3$read: $line3.$read.INSTANCE.type = $line3.$read.INSTANCE; + */ +object Test extends ReplTest { + override def transformSettings(settings: Settings): Settings = { + settings.Yreplclassbased.value = true + settings + } + def code = + """ +:paste < EOF +case class C(i: Int) +val c = C(42) +EOF +val d: C = c // shew + """ +} -- cgit v1.2.3