Skip to content

Commit d3c54ad

Browse files
committed
Replace Throwable catches with NonFatal
Previously, stackoverflows led to infinite loops because the catch immediately threw another stack overflow. Anyway, one should never catch Throwable.
1 parent b6fadd4 commit d3c54ad

File tree

9 files changed

+27
-17
lines changed

9 files changed

+27
-17
lines changed

src/dotty/tools/dotc/Driver.scala

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import config.CompilerCommand
44
import core.Contexts.{Context, ContextBase}
55
import util.DotClass
66
import reporting._
7+
import scala.util.control.NonFatal
78

89
abstract class Driver extends DotClass {
910

@@ -29,14 +30,11 @@ abstract class Driver extends DotClass {
2930
try {
3031
doCompile(newCompiler(), fileNames)
3132
} catch {
32-
case ex: Throwable =>
33-
ex match {
34-
case ex: FatalError =>
35-
ctx.error(ex.getMessage) // signals that we should fail compilation.
36-
ctx.typerState.reporter
37-
case _ =>
38-
throw ex // unexpected error, tell the outside world.
39-
}
33+
case ex: FatalError =>
34+
ctx.error(ex.getMessage) // signals that we should fail compilation.
35+
ctx.typerState.reporter
36+
case NonFatal(ex) =>
37+
throw(ex) // unexpected error, tell the outside world.
4038
}
4139
}
4240

src/dotty/tools/dotc/core/Denotations.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ object Denotations {
386386
case info: MethodicType =>
387387
try info.signature
388388
catch { // !!! DEBUG
389-
case ex: Throwable =>
389+
case scala.util.control.NonFatal(ex) =>
390390
println(s"cannot take signature of ${info.show}")
391391
throw ex
392392
}

src/dotty/tools/dotc/core/SymbolLoaders.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Contexts._, Symbols._, Flags._, SymDenotations._, Types._, Scopes._, util
1414
import StdNames._, NameOps._
1515
import Decorators.{StringDecorator, StringInterpolators}
1616
import pickling.ClassfileParser
17+
import scala.util.control.NonFatal
1718

1819
object SymbolLoaders {
1920
/** A marker trait for a completer that replaces the original
@@ -206,7 +207,7 @@ abstract class SymbolLoader extends LazyType {
206207
} catch {
207208
case ex: IOException =>
208209
signalError(ex)
209-
case ex: Throwable =>
210+
case NonFatal(ex) =>
210211
println(s"exception caught when loading $root: $ex")
211212
throw ex
212213
} finally {

src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import util.{Stats, DotClass, SimpleMap}
1212
import config.Config
1313
import config.Printers._
1414
import TypeErasure.{erasedLub, erasedGlb}
15+
import scala.util.control.NonFatal
1516

1617
/** Provides methods to compare types.
1718
*/
@@ -401,7 +402,7 @@ class TypeComparer(initctx: Context) extends DotClass {
401402

402403
result
403404
} catch {
404-
case ex: Throwable =>
405+
case NonFatal(ex) =>
405406
def showState = {
406407
println(disambiguated(implicit ctx => s"assertion failure for ${tp1.show} <:< ${tp2.show}, frozen = $frozenConstraint"))
407408
def explainPoly(tp: Type) = tp match {

src/dotty/tools/dotc/core/pickling/ClassfileParser.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import scala.collection.mutable.{ ListBuffer, ArrayBuffer }
1313
import scala.annotation.switch
1414
import typer.Checking.checkNonCyclic
1515
import io.AbstractFile
16+
import scala.util.control.NonFatal
1617

1718
class ClassfileParser(
1819
classfile: AbstractFile,
@@ -447,7 +448,7 @@ class ClassfileParser(
447448
else Some(Annotation.deferredResolve(attrType, argbuf.toList))
448449
} catch {
449450
case f: FatalError => throw f // don't eat fatal errors, they mean a class was not found
450-
case ex: Throwable =>
451+
case NonFatal(ex) =>
451452
// We want to be robust when annotations are unavailable, so the very least
452453
// we can do is warn the user about the exception
453454
// There was a reference to ticket 1135, but that is outdated: a reference to a class not on

src/dotty/tools/dotc/transform/TreeChecker.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import util.SourcePosition
2323
import collection.mutable
2424
import ProtoTypes._
2525
import java.lang.AssertionError
26+
import scala.util.control.NonFatal
2627

2728
/** Run by -Ycheck option after a given phase, this class retypes all syntax trees
2829
* and verifies that the type of each tree node so obtained conforms to the type found in the tree node.
@@ -58,7 +59,7 @@ class TreeChecker {
5859
val checker = new Checker(previousPhases(phasesToRun.toList)(ctx))
5960
try checker.typedExpr(ctx.compilationUnit.tpdTree)(checkingCtx)
6061
catch {
61-
case ex: Throwable =>
62+
case NonFatal(ex) =>
6263
implicit val ctx: Context = checkingCtx
6364
println(i"*** error while checking after phase ${checkingCtx.phase.prev} ***")
6465
throw ex

src/dotty/tools/dotc/transform/TreeTransform.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import dotty.tools.dotc.core.Decorators._
1616
import dotty.tools.dotc.util.DotClass
1717
import scala.annotation.tailrec
1818
import config.Printers.transforms
19+
import scala.util.control.NonFatal
1920

2021
object TreeTransforms {
2122
import tpd._
@@ -1210,6 +1211,8 @@ object TreeTransforms {
12101211
goOther(tree, info.nx.nxTransOther(cur))
12111212
}
12121213

1214+
private var crashingTree: Tree = EmptyTree
1215+
12131216
def transform(tree: Tree, info: TransformerInfo, cur: Int)(implicit ctx: Context): Tree = ctx.traceIndented(s"transforming ${tree.show} at ${ctx.phase}", transforms, show = true) {
12141217
try
12151218
if (cur < info.transformers.length) {
@@ -1223,8 +1226,11 @@ object TreeTransforms {
12231226
}
12241227
} else tree
12251228
catch {
1226-
case ex: Throwable =>
1227-
println(i"exception while transforming $tree of class ${tree.getClass} # ${tree.uniqueId}")
1229+
case NonFatal(ex) =>
1230+
if (tree ne crashingTree) {
1231+
crashingTree = tree
1232+
println(i"exception while transforming $tree of class ${tree.getClass} # ${tree.uniqueId}")
1233+
}
12281234
throw ex
12291235
}
12301236
}

src/dotty/tools/dotc/typer/FrontEnd.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import dotty.tools.dotc.parsing.JavaParsers.JavaParser
88
import parsing.Parsers.Parser
99
import config.Printers._
1010
import util.Stats._
11+
import scala.util.control.NonFatal
1112

1213
class FrontEnd extends Phase {
1314

@@ -16,7 +17,7 @@ class FrontEnd extends Phase {
1617
def monitor(doing: String)(body: => Unit)(implicit ctx: Context) =
1718
try body
1819
catch {
19-
case ex: Throwable =>
20+
case NonFatal(ex) =>
2021
println(s"exception occured while $doing ${ctx.compilationUnit}")
2122
throw ex
2223
}

src/dotty/tools/dotc/typer/ReTyper.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Decorators._
99
import typer.ProtoTypes._
1010
import ast.{tpd, untpd}
1111
import ast.Trees._
12+
import scala.util.control.NonFatal
1213

1314
/** A version of Typer that keeps all symbols defined and referenced in a
1415
* previously typed tree.
@@ -91,7 +92,7 @@ class ReTyper extends Typer {
9192
override def typedUnadapted(tree: untpd.Tree, pt: Type)(implicit ctx: Context) =
9293
try super.typedUnadapted(tree, pt)
9394
catch {
94-
case ex: Throwable =>
95+
case NonFatal(ex) =>
9596
println(i"exception while typing $tree of class ${tree.getClass} # ${tree.uniqueId}")
9697
throw ex
9798
}

0 commit comments

Comments
 (0)