Skip to content

Commit

Permalink
SI-6549 Improve escaping in REPL codegen.
Browse files Browse the repository at this point in the history
 - Escape the LHS of an assign when printing results
   - e.g. X("").foo = bar
 - Escape val names
   - e.g. val `"` = 0`
  • Loading branch information
retronym committed Dec 3, 2012
1 parent fd57069 commit 90c87fc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/compiler/scala/tools/nsc/interpreter/MemberHandlers.scala
Expand Up @@ -120,7 +120,7 @@ trait MemberHandlers {
if (replProps.vids) """" + " @ " + "%%8x".format(System.identityHashCode(%s)) + " """.trim.format(req fullPath name)
else ""

""" + "%s%s: %s = " + %s""".format(prettyName, vidString, string2code(req typeOf name), resultString)
""" + "%s%s: %s = " + %s""".format(string2code(prettyName), vidString, string2code(req typeOf name), resultString)
}
}
}
Expand All @@ -147,8 +147,7 @@ trait MemberHandlers {
override def resultExtractionCode(req: Request) = {
val lhsType = string2code(req lookupTypeOf name)
val res = string2code(req fullPath name)

""" + "%s: %s = " + %s + "\n" """.format(lhs, lhsType, res) + "\n"
""" + "%s: %s = " + %s + "\n" """.format(string2code(lhs.toString), lhsType, res) + "\n"
}
}

Expand Down
32 changes: 32 additions & 0 deletions test/files/run/t6549.check
@@ -0,0 +1,32 @@
Type in expressions to have them evaluated.
Type :help for more information.

scala>

scala> case class `X"`(var xxx: Any)
defined class X$u0022

scala> val m = Map(("": Any) -> `X"`("\""), ('s: Any) -> `X"`("\""))
m: scala.collection.immutable.Map[Any,X"] = Map("" -> X"("), 's -> X"("))

scala> m("")
res0: X" = X"(")

scala> m("").xxx
res1: Any = "

scala> m("").xxx = 0
m("").xxx: Any = 0

scala> m("").xxx = "\""
m("").xxx: Any = "

scala> m('s).xxx = 's
m(scala.Symbol("s")).xxx: Any = 's

scala> val `"` = 0
": Int = 0

scala>

scala>
22 changes: 22 additions & 0 deletions test/files/run/t6549.scala
@@ -0,0 +1,22 @@
import scala.tools.partest.ReplTest

// Check that the fragments of code generated in
// in the REPL correctly escape values added to
// literal strings.
//
// Before, we saw:
// scala> m("").x = 77
// <console>:10: error: ')' expected but string literal found.
// + "m("").x: Int = " + `$ires8` + "\n"
object Test extends ReplTest {
def code = """
|case class `X"`(var xxx: Any)
|val m = Map(("": Any) -> `X"`("\""), ('s: Any) -> `X"`("\""))
|m("")
|m("").xxx
|m("").xxx = 0
|m("").xxx = "\""
|m('s).xxx = 's
|val `"` = 0
""".stripMargin
}

0 comments on commit 90c87fc

Please sign in to comment.