Skip to content

Commit 440f68d

Browse files
committed
Repl truncation copes with null
1 parent e0ac98b commit 440f68d

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

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

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

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

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

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

+9
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,15 @@ class ReplCompilerTests extends ReplTest:
347347
assertEquals("java.lang.AssertionError: assertion failed", all.head)
348348
}
349349

350+
@Test def `i17333 print null result of toString`: Unit =
351+
initially:
352+
run("val tpolecat = new Object { override def toString(): String = null }")
353+
.andThen:
354+
assertEquals("val tpolecat: Object = null // non-null reference has null-valued toString", lines().head)
355+
356+
end ReplCompilerTests
357+
358+
350359
object ReplCompilerTests:
351360

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

0 commit comments

Comments
 (0)