Skip to content

Commit 2659361

Browse files
Fix #11555: In REPL, render string values inside quotes
See also scala/scala#8885
1 parent 4bf2f04 commit 2659361

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

compiler/src/dotty/tools/repl/Rendering.scala

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,24 +106,25 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None) {
106106
*
107107
* Calling this method evaluates the expression using reflection
108108
*/
109-
private def valueOf(sym: Symbol)(using Context): Option[String] = {
109+
private def valueOf(sym: Symbol)(using Context): Option[String] =
110+
if !sym.is(Flags.Method) && sym.info == defn.UnitType
111+
then return None
112+
110113
val objectName = sym.owner.fullName.encode.toString.stripSuffix("$")
111114
val resObj: Class[?] = Class.forName(objectName, true, classLoader())
112-
val value =
113-
resObj
114-
.getDeclaredMethods.find(_.getName == sym.name.encode.toString)
115-
.map(_.invoke(null))
116-
val string = value.map(replStringOf(_))
117-
if (!sym.is(Flags.Method) && sym.info == defn.UnitType)
118-
None
119-
else
120-
string.map { s =>
121-
if (s.startsWith(str.REPL_SESSION_LINE))
122-
s.drop(str.REPL_SESSION_LINE.length).dropWhile(c => c.isDigit || c == '$')
123-
else
124-
s
125-
}
126-
}
115+
116+
resObj.getDeclaredMethods.find(_.getName == sym.name.encode.toString).map { method =>
117+
val value = method.invoke(null)
118+
val resultType = method.getReturnType()
119+
val rawStringRepr = replStringOf(value)
120+
val stringRepr =
121+
if rawStringRepr.startsWith(str.REPL_SESSION_LINE)
122+
then rawStringRepr.drop(str.REPL_SESSION_LINE.length).dropWhile(c => c.isDigit || c == '$')
123+
else rawStringRepr
124+
125+
if resultType == classOf[String] then s""""$stringRepr"""" else stringRepr
126+
}
127+
end valueOf
127128

128129
/** Formats errors using the `messageRenderer` */
129130
def formatError(dia: Diagnostic)(implicit state: State): Diagnostic =

0 commit comments

Comments
 (0)