Skip to content

Commit 3e1fcc3

Browse files
Fix #11555: In REPL, render string values inside quotes
See also scala/scala#8885
1 parent e08fd59 commit 3e1fcc3

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

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

+17-16
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,25 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None) {
9595
*
9696
* Calling this method evaluates the expression using reflection
9797
*/
98-
private def valueOf(sym: Symbol)(using Context): Option[String] = {
98+
private def valueOf(sym: Symbol)(using Context): Option[String] =
99+
if !sym.is(Flags.Method) && sym.info == defn.UnitType
100+
then return None
101+
99102
val objectName = sym.owner.fullName.encode.toString.stripSuffix("$")
100103
val resObj: Class[?] = Class.forName(objectName, true, classLoader())
101-
val value =
102-
resObj
103-
.getDeclaredMethods.find(_.getName == sym.name.encode.toString)
104-
.map(_.invoke(null))
105-
val string = value.map(replStringOf(_).trim)
106-
if (!sym.is(Flags.Method) && sym.info == defn.UnitType)
107-
None
108-
else
109-
string.map { s =>
110-
if (s.startsWith(str.REPL_SESSION_LINE))
111-
s.drop(str.REPL_SESSION_LINE.length).dropWhile(c => c.isDigit || c == '$')
112-
else
113-
s
114-
}
115-
}
104+
105+
resObj.getDeclaredMethods.find(_.getName == sym.name.encode.toString).map { method =>
106+
val value = method.invoke(null)
107+
val resultType = method.getReturnType()
108+
val rawStringRepr = replStringOf(value).trim
109+
val stringRepr =
110+
if rawStringRepr.startsWith(str.REPL_SESSION_LINE)
111+
then rawStringRepr.drop(str.REPL_SESSION_LINE.length).dropWhile(c => c.isDigit || c == '$')
112+
else rawStringRepr
113+
114+
if resultType == classOf[String] then s""""$stringRepr"""" else stringRepr
115+
}
116+
end valueOf
116117

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

0 commit comments

Comments
 (0)