diff --git a/compiler/src/dotty/tools/dotc/core/TypeErrors.scala b/compiler/src/dotty/tools/dotc/core/TypeErrors.scala index f1575b538e22..8dcd1d170235 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeErrors.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeErrors.scala @@ -22,7 +22,11 @@ abstract class TypeError(using creationContext: Context) extends Exception(""): * This is expensive and only useful for debugging purposes. */ def computeStackTrace: Boolean = - ctx.debug || (cyclicErrors != noPrinter && this.isInstanceOf[CyclicReference] && !(ctx.mode is Mode.CheckCyclic)) + ctx.debug + || (cyclicErrors != noPrinter && this.isInstanceOf[CyclicReference] && !(ctx.mode is Mode.CheckCyclic)) + || ctx.settings.YdebugTypeError.value + || ctx.settings.YdebugError.value + || ctx.settings.YdebugUnpickling.value override def fillInStackTrace(): Throwable = if computeStackTrace then super.fillInStackTrace().nn @@ -188,3 +192,14 @@ object CyclicReference: ex end CyclicReference +class UnpicklingError(denot: Denotation, where: String, cause: Throwable)(using Context) extends TypeError: + override def toMessage(using Context): Message = + val debugUnpickling = cause match + case cause: UnpicklingError => "" + case _ => + if ctx.settings.YdebugUnpickling.value then + cause.getStackTrace().nn.mkString("\n ", "\n ", "") + else "\n\nRun with -Ydebug-unpickling to see full stack trace." + em"""Could not read definition $denot$where. Caused by the following exception: + |$cause$debugUnpickling""" +end UnpicklingError diff --git a/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala b/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala index 4e34fc9ab448..9de82c57be2a 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala @@ -128,12 +128,7 @@ class TreeUnpickler(reader: TastyReader, def where = val f = denot.symbol.associatedFile if f == null then "" else s" in $f" - if ctx.settings.YdebugUnpickling.value then throw ex - else throw TypeError( - em"""Could not read definition of $denot$where - |An exception was encountered: - | $ex - |Run with -Ydebug-unpickling to see full stack trace.""") + throw UnpicklingError(denot, where, ex) treeAtAddr(currentAddr) = try atPhaseBeforeTransforms {