Skip to content

Commit 78a44d5

Browse files
committed
TypeError: only compute the stack trace when needed
TypeError extends Exception, but the stack trace is only needed for debugging purposes, either when using -Ydebug or when investigating cyclic errors.
1 parent 49ca74b commit 78a44d5

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

compiler/src/dotty/tools/dotc/core/TypeErrors.scala

+14-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,22 @@ import Denotations._
1212
import Decorators._
1313
import reporting._
1414
import ast.untpd
15-
import config.Printers.cyclicErrors
15+
import config.Printers.{cyclicErrors, noPrinter}
16+
17+
import scala.annotation.constructorOnly
1618

1719
abstract class TypeError(using creationContext: Context) extends Exception(""):
1820

21+
/** Will the stack trace of this exception be filled in?
22+
* This is expensive and only useful for debugging purposes.
23+
*/
24+
def computeStackTrace: Boolean =
25+
ctx.debug || (cyclicErrors != noPrinter && this.isInstanceOf[CyclicReference] && !(ctx.mode is Mode.CheckCyclic))
26+
27+
override def fillInStackTrace(): Throwable =
28+
if computeStackTrace then super.fillInStackTrace().nn
29+
else this
30+
1931
/** Convert to message. This takes an additional Context, so that we
2032
* use the context when the message is first produced, i.e. when the TypeError
2133
* is handled. This makes a difference for CyclicErrors since we need to know
@@ -164,7 +176,7 @@ class CyclicReference private (val denot: SymDenotation)(using Context) extends
164176
object CyclicReference:
165177
def apply(denot: SymDenotation)(using Context): CyclicReference =
166178
val ex = new CyclicReference(denot)
167-
if !(ctx.mode is Mode.CheckCyclic) || ctx.settings.Ydebug.value then
179+
if ex.computeStackTrace then
168180
cyclicErrors.println(s"Cyclic reference involving! $denot")
169181
val sts = ex.getStackTrace.asInstanceOf[Array[StackTraceElement]]
170182
for (elem <- sts take 200)

0 commit comments

Comments
 (0)