Skip to content

Commit f1b53d3

Browse files
committed
REPL format stack trace prefix of exception
1 parent 33e706f commit f1b53d3

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import dotc.core.Denotations.Denotation
1111
import dotc.core.Flags
1212
import dotc.core.Flags._
1313
import dotc.core.Symbols.{Symbol, defn}
14-
import dotc.core.StdNames.str
14+
import dotc.core.StdNames.{nme, str}
1515
import dotc.core.NameOps._
1616
import dotc.printing.ReplPrinter
1717
import dotc.reporting.{MessageRendering, Message, Diagnostic}
@@ -115,32 +115,33 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None) {
115115
/** Render value definition result */
116116
def renderVal(d: Denotation)(using Context): Option[Diagnostic] =
117117
val dcl = d.symbol.showUser
118-
118+
def msg(s: String) = infoDiagnostic(s, d)
119119
try
120-
if (d.symbol.is(Flags.Lazy)) Some(infoDiagnostic(dcl, d))
121-
else valueOf(d.symbol).map(value => infoDiagnostic(s"$dcl = $value", d))
122-
catch case ex: InvocationTargetException => Some(infoDiagnostic(renderError(ex), d))
120+
if (d.symbol.is(Flags.Lazy)) Some(msg(dcl))
121+
else valueOf(d.symbol).map(value => msg(s"$dcl = $value"))
122+
catch case e: InvocationTargetException => Some(msg(renderError(e, d)))
123123
end renderVal
124124

125125
/** Force module initialization in the absence of members. */
126126
def forceModule(sym: Symbol)(using Context): Seq[Diagnostic] =
127127
def load() =
128128
val objectName = sym.fullName.encode.toString
129-
val resObj: Class[?] = Class.forName(objectName, true, classLoader())
129+
Class.forName(objectName, true, classLoader())
130130
Nil
131-
try load() catch case e: ExceptionInInitializerError => List(infoDiagnostic(renderError(e), sym.denot))
131+
try load() catch case e: ExceptionInInitializerError => List(infoDiagnostic(renderError(e, sym.denot), sym.denot))
132132

133133
/** Render the stack trace of the underlying exception. */
134-
private def renderError(ex: InvocationTargetException | ExceptionInInitializerError): String = {
135-
val cause = ex.getCause match {
136-
case ex: ExceptionInInitializerError => ex.getCause
137-
case ex => ex
138-
}
139-
val sw = new StringWriter()
140-
val pw = new PrintWriter(sw)
141-
cause.printStackTrace(pw)
142-
sw.toString
143-
}
134+
private def renderError(ite: InvocationTargetException | ExceptionInInitializerError, d: Denotation)(using Context): String =
135+
import dotty.tools.dotc.util.StackTraceOps._
136+
val cause = ite.getCause match
137+
case e: ExceptionInInitializerError => e.getCause
138+
case e => e
139+
def isWrapperCode(ste: StackTraceElement) =
140+
ste.getClassName == d.symbol.owner.name.show
141+
&& (ste.getMethodName == nme.STATIC_CONSTRUCTOR.show || ste.getMethodName == nme.CONSTRUCTOR.show)
142+
143+
cause.formatStackTracePrefix(!isWrapperCode(_))
144+
end renderError
144145

145146
private def infoDiagnostic(msg: String, d: Denotation)(using Context): Diagnostic =
146147
new Diagnostic.Info(msg, d.symbol.sourcePos)

0 commit comments

Comments
 (0)