Skip to content

Fix #11555: In REPL, render string values inside quotes #11720

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

Closed
wants to merge 2 commits into from
Closed
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
33 changes: 17 additions & 16 deletions compiler/src/dotty/tools/repl/Rendering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,25 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None) {
*
* Calling this method evaluates the expression using reflection
*/
private def valueOf(sym: Symbol)(using Context): Option[String] = {
private def valueOf(sym: Symbol)(using Context): Option[String] =
if !sym.is(Flags.Method) && sym.info == defn.UnitType
then return None

val objectName = sym.owner.fullName.encode.toString.stripSuffix("$")
val resObj: Class[?] = Class.forName(objectName, true, classLoader())
val value =
resObj
.getDeclaredMethods.find(_.getName == sym.name.encode.toString)
.map(_.invoke(null))
val string = value.map(replStringOf(_))
if (!sym.is(Flags.Method) && sym.info == defn.UnitType)
None
else
string.map { s =>
if (s.startsWith(str.REPL_SESSION_LINE))
s.drop(str.REPL_SESSION_LINE.length).dropWhile(c => c.isDigit || c == '$')
else
s
}
}

resObj.getDeclaredMethods.find(_.getName == sym.name.encode.toString).map { method =>
val value = method.invoke(null)
val resultType = method.getReturnType()
val rawStringRepr = replStringOf(value)
val stringRepr =
if rawStringRepr.startsWith(str.REPL_SESSION_LINE)
then rawStringRepr.drop(str.REPL_SESSION_LINE.length).dropWhile(c => c.isDigit || c == '$')
else rawStringRepr

if resultType == classOf[String] then s""""$stringRepr"""" else stringRepr
}
end valueOf

/** Formats errors using the `messageRenderer` */
def formatError(dia: Diagnostic)(implicit state: State): Diagnostic =
Expand Down
2 changes: 1 addition & 1 deletion compiler/test-resources/repl/i1369
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
scala> print("foo")
foo
scala> "Hello"
val res0: String = Hello
val res0: String = "Hello"
2 changes: 1 addition & 1 deletion compiler/test/dotty/tools/repl/ReplCompilerTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class ReplCompilerTests extends ReplTest {

@Test def testSingletonPrint = fromInitialState { implicit state =>
run("""val a = "hello"; val x: a.type = a""")
assertMultiLineEquals("val a: String = hello\nval x: a.type = hello", storedOutput().trim)
assertMultiLineEquals("val a: String = \"hello\"\nval x: a.type = \"hello\"", storedOutput().trim)
}

@Test def i6574 = fromInitialState { implicit state =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ class WorksheetTest {
((m1 to m2), s"1${nl}2${nl}3"))
}

@Test def patternMatching0: Unit = {
@Test def : Unit = {
ws"""${m1}1 + 2 match {
case x if x % 2 == 0 => "even"
case _ => "odd"
}${m2}""".withSource
.run(m1,
((m1 to m2), "val res0: String = odd"))
((m1 to m2), "val res0: String = \"odd\""))
}

@Test def patternMatching1: Unit = {
Expand Down
2 changes: 1 addition & 1 deletion staging/test-resources/repl-staging/i6007
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ def compiler: quoted.staging.Compiler
scala> def v(using Quotes) = '{ (if true then Some(1) else None).map(v => v+1) }
def v(using x$1: quoted.Quotes): quoted.Expr[Option[Int]]
scala> scala.quoted.staging.withQuotes(v.show)
val res0: String = (if (true) scala.Some.apply[scala.Int](1) else scala.None).map[scala.Int](((v: scala.Int) => v.+(1)))
val res0: String = "(if (true) scala.Some.apply[scala.Int](1) else scala.None).map[scala.Int](((v: scala.Int) => v.+(1)))"
scala> scala.quoted.staging.run(v)
val res1: Option[Int] = Some(2)