diff --git a/compiler/src/dotty/tools/repl/Rendering.scala b/compiler/src/dotty/tools/repl/Rendering.scala index a09ad8d962e1..1668857315ef 100644 --- a/compiler/src/dotty/tools/repl/Rendering.scala +++ b/compiler/src/dotty/tools/repl/Rendering.scala @@ -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 = diff --git a/compiler/test-resources/repl/i1369 b/compiler/test-resources/repl/i1369 index ced0af6aea36..971d10839f03 100644 --- a/compiler/test-resources/repl/i1369 +++ b/compiler/test-resources/repl/i1369 @@ -1,4 +1,4 @@ scala> print("foo") foo scala> "Hello" -val res0: String = Hello +val res0: String = "Hello" diff --git a/compiler/test/dotty/tools/repl/ReplCompilerTests.scala b/compiler/test/dotty/tools/repl/ReplCompilerTests.scala index bc06727f2ea4..6c0ce09c476e 100644 --- a/compiler/test/dotty/tools/repl/ReplCompilerTests.scala +++ b/compiler/test/dotty/tools/repl/ReplCompilerTests.scala @@ -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 => diff --git a/language-server/test/dotty/tools/languageserver/WorksheetTest.scala b/language-server/test/dotty/tools/languageserver/WorksheetTest.scala index d2e09060da9a..0133755df9c4 100644 --- a/language-server/test/dotty/tools/languageserver/WorksheetTest.scala +++ b/language-server/test/dotty/tools/languageserver/WorksheetTest.scala @@ -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 = { diff --git a/staging/test-resources/repl-staging/i6007 b/staging/test-resources/repl-staging/i6007 index be9d5c0f92d6..d3b03ccee26c 100644 --- a/staging/test-resources/repl-staging/i6007 +++ b/staging/test-resources/repl-staging/i6007 @@ -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)