Skip to content

Commit

Permalink
print null result of replStringOf as null
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha committed Mar 1, 2024
1 parent 302aaca commit 390d956
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
4 changes: 1 addition & 3 deletions compiler/src/dotty/tools/repl/Rendering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
val maxPrintElements = ctx.settings.VreplMaxPrintElements.valueIn(ctx.settingsState)
val maxPrintCharacters = ctx.settings.VreplMaxPrintCharacters.valueIn(ctx.settingsState)
// stringOf returns null if value.toString returns null. Show some text as a fallback.
def toIdentityString(value: Object): String =
s"${value.getClass.getName}@${System.identityHashCode(value).toHexString}"
def fallback = s"""${toIdentityString(value)} // return value of "${sym.name}.toString" is null"""
def fallback = s"""null // result of "${sym.name}.toString" is null"""
if value == null then "null" else
myReplStringOf(value, maxPrintElements, maxPrintCharacters) match
case null => fallback
Expand Down
8 changes: 4 additions & 4 deletions compiler/test/dotty/tools/repl/ReplCompilerTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -436,17 +436,17 @@ class ReplCompilerTests extends ReplTest:
run("val tpolecat = new Object { override def toString(): String = null }")
.andThen:
val last = lines().last
assertTrue(last, last.startsWith("val tpolecat: Object = anon"))
assertTrue(last, last.endsWith("""// return value of "tpolecat.toString" is null"""))
assertTrue(last, last.startsWith("val tpolecat: Object = null"))
assertTrue(last, last.endsWith("""// result of "tpolecat.toString" is null"""))

@Test def `i17333 print toplevel object with null toString`: Unit =
initially:
run("object tpolecat { override def toString(): String = null }")
.andThen:
run("tpolecat")
val last = lines().last
assertTrue(last, last.startsWith("val res0: tpolecat.type = tpolecat"))
assertTrue(last, last.endsWith("""// return value of "res0.toString" is null"""))
assertTrue(last, last.startsWith("val res0: tpolecat.type = null"))
assertTrue(last, last.endsWith("""// result of "res0.toString" is null"""))

object ReplCompilerTests:

Expand Down

0 comments on commit 390d956

Please sign in to comment.