Skip to content

Commit cfcc624

Browse files
Special case .toString for null and empty String
1 parent 88b62e8 commit cfcc624

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,29 @@ private[repl] class Rendering(compiler: ReplCompiler,
4343
myClassLoader
4444
}
4545

46-
/** Load the value of the symbol using reflection
46+
/** Load the value of the symbol using reflection.
4747
*
4848
* Calling this method evaluates the expression using reflection
4949
*/
5050
private[this] def valueOf(sym: Symbol)(implicit ctx: Context): Option[String] = {
5151
val defn = ctx.definitions
52-
val objectName = sym.owner.fullName.encode.toString.dropRight(1) // gotta drop the '$'
52+
val objectName = sym.owner.fullName.encode.toString.stripSuffix("$")
5353
val resObj: Class[_] = Class.forName(objectName, true, classLoader())
54-
val res =
54+
val value =
5555
resObj
56-
.getDeclaredMethods.find(_.getName == sym.name.toString).get
57-
.invoke(null).toString
58-
56+
.getDeclaredMethods.find(_.getName == sym.name.encode.toString).get
57+
.invoke(null)
58+
val string = value match {
59+
case null => "null" // Calling .toString on null => NPE
60+
case "" => "\"\"" // Sepcial cased for empty string, following scalac
61+
case x => x.toString
62+
}
5963
if (!sym.is(Flags.Method) && sym.info == defn.UnitType)
6064
None
61-
else if (res.startsWith(str.REPL_SESSION_LINE))
62-
Some(res.drop(str.REPL_SESSION_LINE.length).dropWhile(c => c.isDigit || c == '$'))
65+
else if (string.startsWith(str.REPL_SESSION_LINE))
66+
Some(string.drop(str.REPL_SESSION_LINE.length).dropWhile(c => c.isDigit || c == '$'))
6367
else
64-
Some(res)
68+
Some(string)
6569
}
6670

6771
/** Render method definition result */

0 commit comments

Comments
 (0)