Skip to content

Commit db12e72

Browse files
authored
Merge pull request scala#6935 from som-snytt/issue/repl-throwing-print
Trim stack trace in REPL print
2 parents 40d0f04 + 35f4e4c commit db12e72

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/repl/scala/tools/nsc/interpreter/IMain.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -587,11 +587,12 @@ class IMain(val settings: Settings, parentClassLoaderOverride: Option[ClassLoade
587587
val unwrapped = rootCause(t)
588588

589589
// Example input: $line3.$read$$iw$$iw$
590-
val classNameRegex = (lineRegex + ".*").r
591-
def isWrapperInit(x: StackTraceElement) = cond(x.getClassName) {
592-
case classNameRegex() if x.getMethodName == nme.CONSTRUCTOR.decoded => true
590+
val classNameRegex = s"$lineRegex.*".r
591+
def isWrapperCode(x: StackTraceElement) = cond(x.getClassName) {
592+
case classNameRegex() =>
593+
x.getMethodName == nme.CONSTRUCTOR.decoded || x.getMethodName == printName
593594
}
594-
val stackTrace = unwrapped stackTracePrefixString (!isWrapperInit(_))
595+
val stackTrace = unwrapped.stackTracePrefixString(!isWrapperCode(_))
595596

596597
withLastExceptionLock[String]({
597598
directBind[Throwable]("lastException", unwrapped)(StdReplTags.tagOfThrowable, classTag[Throwable])

src/repl/scala/tools/nsc/interpreter/Naming.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ object Naming {
3333
cleaned map (ch => if (ch.isWhitespace || ch == ESC) ch else if (ch < 32) '?' else ch)
3434
}
3535

36+
// Uncompiled regex pattern to detect `line` package and members
37+
// `read`, `eval`, `print`, for purposes of filtering output and stack traces.
38+
//
3639
// The two name forms this is catching are the two sides of this assignment:
3740
//
3841
// $line3.$read.$iw.$iw.Bippy =
3942
// $line3.$read$$iw$$iw$Bippy@4a6a00ca
40-
lazy val lineRegex = {
43+
//
44+
lazy val lineRegex: String = {
4145
val sn = sessionNames
4246
val members = List(sn.read, sn.eval, sn.print) map Regex.quote mkString("(?:", "|", ")")
4347
Regex.quote(sn.line) + """\d+[./]""" + members + """[$.]"""

test/files/run/repl-trim-stack-trace.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ java.lang.Exception
2323
at .f(<console>:1)
2424
... ??? elided
2525

26+
scala> null.asInstanceOf
27+
java.lang.NullPointerException
28+
at .$print$lzycompute(<synthetic>:9)
29+
... ??? elided
30+
2631
scala> :quit

0 commit comments

Comments
 (0)