Skip to content

Commit 3cbef4d

Browse files
committed
Repl truncation copes with null
[Cherry-picked b95cd75][modified]
1 parent 6078710 commit 3cbef4d

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,16 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
7070
// In order to figure out if it did get truncated, we invoke it twice - once with the `maxElements` that we
7171
// want to print, and once without a limit. If the first is shorter, truncation did occur.
7272
val notTruncated = stringOfMaybeTruncated(value, Int.MaxValue)
73-
val maybeTruncatedByElementCount = stringOfMaybeTruncated(value, maxElements)
74-
val maybeTruncated = truncate(maybeTruncatedByElementCount, maxCharacters)
75-
76-
// our string representation may have been truncated by element and/or character count
77-
// if so, append an info string - but only once
78-
if (notTruncated.length == maybeTruncated.length) maybeTruncated
79-
else s"$maybeTruncated ... large output truncated, print value to show all"
73+
if notTruncated == null then null else
74+
val maybeTruncated =
75+
val maybeTruncatedByElementCount = stringOfMaybeTruncated(value, maxElements)
76+
truncate(maybeTruncatedByElementCount, maxCharacters)
77+
78+
// our string representation may have been truncated by element and/or character count
79+
// if so, append an info string - but only once
80+
if notTruncated.length == maybeTruncated.length then maybeTruncated
81+
else s"$maybeTruncated ... large output truncated, print value to show all"
82+
end if
8083
}
8184

8285
}
@@ -94,8 +97,9 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
9497
"replStringOf should only be called on values creating using `classLoader()`, but `classLoader()` has not been called so far")
9598
val maxPrintElements = ctx.settings.VreplMaxPrintElements.valueIn(ctx.settingsState)
9699
val maxPrintCharacters = ctx.settings.VreplMaxPrintCharacters.valueIn(ctx.settingsState)
97-
val res = myReplStringOf(value, maxPrintElements, maxPrintCharacters)
98-
if res == null then "null // non-null reference has null-valued toString" else res
100+
Option(value)
101+
.flatMap(v => Option(myReplStringOf(v, maxPrintElements, maxPrintCharacters)))
102+
.getOrElse("null // non-null reference has null-valued toString")
99103

100104
/** Load the value of the symbol using reflection.
101105
*

compiler/test/dotty/tools/repl/ReplCompilerTests.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,14 @@ class ReplCompilerTests extends ReplTest:
353353
@Test def `i13097 expect template after colon` = contextually:
354354
assert(ParseResult.isIncomplete("class C:"))
355355

356+
@Test def `i17333 print null result of toString`: Unit =
357+
initially:
358+
run("val tpolecat = new Object { override def toString(): String = null }")
359+
.andThen:
360+
assertEquals("val tpolecat: Object = null // non-null reference has null-valued toString", lines().head)
361+
362+
end ReplCompilerTests
363+
356364
object ReplCompilerTests:
357365

358366
private val pattern = Pattern.compile("\\r[\\n]?|\\n");

0 commit comments

Comments
 (0)