Skip to content

Commit a06284b

Browse files
authored
Stop catching Throwable/IIOBE/NPE in the compiler, outside of stack overflow handling (#25832)
Part of #25799 ## How much have you relied on LLM-based tools in this contribution? Not at all ## How was the solution tested? Covered by existing tests (this is a refactoring)
1 parent cad895d commit a06284b

52 files changed

Lines changed: 106 additions & 179 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/src/dotty/tools/backend/jvm/BCodeHelpers.scala

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -662,15 +662,13 @@ trait BCodeHelpers(val backendUtils: BackendUtils)(using ctx: Context) extends B
662662
import scala.tools.asm.util.CheckClassAdapter
663663
def wrap(body: => Unit): Unit = {
664664
try body
665-
catch {
666-
case ex: Throwable =>
667-
report.error(
668-
em"""|compiler bug: created invalid generic signature for $sym in ${sym.denot.owner.showFullName}
669-
|signature: $sig
670-
|if this is reproducible, please report bug at https://github.com/scala/scala3/issues
671-
""", sym.sourcePos)
672-
throw ex
673-
}
665+
catch case ex: Exception =>
666+
report.error(
667+
em"""|compiler bug: created invalid generic signature for $sym in ${sym.denot.owner.showFullName}
668+
|signature: $sig
669+
|if this is reproducible, please report bug at https://github.com/scala/scala3/issues
670+
""", sym.sourcePos)
671+
throw ex
674672
}
675673

676674
wrap {

compiler/src/dotty/tools/backend/jvm/ClassfileWriters.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class ClassfileWriters(frontendAccess: PostProcessorFrontendAccess)(using ctx: C
238238
catch {
239239
case ex: ClosedByInterruptException =>
240240
try Files.deleteIfExists(path) // don't leave a empty of half-written classfile around after an interrupt
241-
catch { case _: Throwable => () }
241+
catch { case _: java.io.IOException => () }
242242
throw ex
243243
}
244244
os.close()

compiler/src/dotty/tools/backend/jvm/CodeGen.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ class CodeGen(val backendUtils: BackendUtils, val primitives: ScalaPrimitives, v
8585
registerGeneratedClass(mainClassNode, isArtifact = false)
8686
registerGeneratedClass(mirrorClassNode, isArtifact = true)
8787
catch
88-
case ex: InterruptedException => throw ex
89-
case ex: CompilationUnit.SuspendException => throw ex
90-
case ex: Throwable =>
91-
if !ex.isInstanceOf[TypeError] then ex.printStackTrace()
88+
case ex: TypeError =>
9289
report.error(s"Error while emitting ${unit.source}\n${ex.getMessage}", cd.sourcePos)
9390

9491

compiler/src/dotty/tools/backend/jvm/GeneratedClassHandler.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ import dotty.tools.dotc.core.Contexts.*
1010
import dotty.tools.io.AbstractFile
1111
import dotty.tools.dotc.profile.ThreadPoolFactory
1212

13-
import scala.util.control.NonFatal
1413
import dotty.tools.dotc.core.Phases
1514
import dotty.tools.dotc.core.Decorators.em
16-
import dotty.tools.dotc.core.Types.IdentityTypeMap.mapCtx
1715
import dotty.tools.dotc.report
1816

1917
import scala.compiletime.uninitialized
@@ -155,10 +153,10 @@ private[jvm] object GeneratedClassHandler {
155153
unitInPostProcess.task.value.get.get
156154
catch
157155
case _: ClosedByInterruptException => throw new InterruptedException()
158-
case NonFatal(t) =>
159-
t.printStackTrace()
156+
case e: Exception =>
157+
e.printStackTrace()
160158
given Context = ctx
161-
report.error(em"unable to write ${unitInPostProcess.sourceFile} $t")
159+
report.error(em"unable to write ${unitInPostProcess.sourceFile} $e")
162160
}
163161
}
164162
}

compiler/src/dotty/tools/backend/jvm/GenericSignatureVisitor.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import scala.tools.asm.{ClassReader, Type, Handle }
66
import scala.tools.asm.tree.*
77

88
import scala.collection.mutable
9-
import scala.util.control.{NoStackTrace, NonFatal}
9+
import scala.util.control.NoStackTrace
1010
import scala.annotation.*
1111
import scala.jdk.CollectionConverters.*
1212
import BTypes.InternalName
@@ -47,7 +47,7 @@ abstract class GenericSignatureVisitor(nestedOnly: Boolean) {
4747

4848
@inline def safely(f: => Unit): Unit = try f catch {
4949
case Aborted =>
50-
case NonFatal(e) => raiseError(s"Exception thrown during signature parsing", sig, Some(e))
50+
case e: Exception => raiseError(s"Exception thrown during signature parsing", sig, Some(e))
5151
}
5252

5353
private def current = {

compiler/src/dotty/tools/backend/jvm/PostProcessor.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class PostProcessor(val frontendAccess: PostProcessorFrontendAccess,
4949
case e: java.lang.RuntimeException if e.getMessage != null && e.getMessage.contains("too large!") =>
5050
report.error(em"Could not write class $internalName because it exceeds JVM code size limits. ${e.getMessage}")
5151
null
52-
case ex: Throwable =>
52+
case ex: Exception =>
5353
if frontendAccess.compilerSettings.debug then ex.printStackTrace()
5454
report.error(em"Error while emitting $internalName\n${ex.getMessage}")
5555
null

compiler/src/dotty/tools/debug/ExpressionCompilerBridge.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dotty.tools.debug
22

33
import java.nio.file.Path
4-
import scala.util.control.NonFatal
54
import dotty.tools.dotc.core.Contexts.Context
65
import dotty.tools.dotc.Driver
76

@@ -29,6 +28,6 @@ class ExpressionCompilerBridge:
2928
driver.process(args, reporter)
3029
!reporter.hasErrors
3130
catch
32-
case NonFatal(cause) =>
31+
case cause: Exception =>
3332
cause.printStackTrace()
3433
throw cause

compiler/src/dotty/tools/dotc/Driver.scala

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import reporting.*
1111
import core.Decorators.*
1212
import util.chaining.*
1313

14-
import scala.util.control.NonFatal
1514
import fromtasty.{TASTYCompiler, TastyFileUtil}
1615

1716
/** Run the Dotty compiler.
@@ -39,13 +38,13 @@ class Driver {
3938
catch
4039
case ex: FatalError =>
4140
report.error(ex.getMessage) // signals that we should fail compilation.
42-
case ex: Throwable if ctx.usedBestEffortTasty =>
41+
case ex: Exception if ctx.usedBestEffortTasty =>
4342
report.bestEffortError(ex, "Some best-effort tasty files were not able to be read.")
4443
throw ex
4544
case ex: TypeError if !runOrNull.enrichedErrorMessage =>
4645
println(runOrNull.enrichErrorMessage(s"${ex.toMessage} while compiling ${files.map(_.path).mkString(", ")}"))
4746
throw ex
48-
case ex: Throwable if !runOrNull.enrichedErrorMessage =>
47+
case ex: Exception if !runOrNull.enrichedErrorMessage =>
4948
println(runOrNull.enrichErrorMessage(s"Exception while compiling ${files.map(_.path).mkString(", ")}"))
5049
throw ex
5150
ctx.reporter
@@ -215,10 +214,6 @@ class Driver {
215214
}
216215

217216
def main(args: Array[String]): Unit = {
218-
// Preload scala.util.control.NonFatal. Otherwise, when trying to catch a StackOverflowError,
219-
// we may try to load it but fail with another StackOverflowError and lose the original exception,
220-
// see <https://groups.google.com/forum/#!topic/scala-user/kte6nak-zPM>.
221-
val _ = NonFatal
222217
sys.exit(if (process(args).hasErrors) 1 else 0)
223218
}
224219
}

compiler/src/dotty/tools/dotc/Run.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import java.io.{BufferedWriter, OutputStreamWriter}
3131
import java.nio.charset.StandardCharsets
3232

3333
import scala.collection.mutable, mutable.ListBuffer
34-
import scala.util.control.NonFatal
3534
import scala.io.Codec
3635

3736
import Run.Progress
@@ -323,7 +322,7 @@ extends ImplicitRunInfo, ConstraintRunInfo, cc.CaptureRunInfo {
323322

324323
def compile(files: List[AbstractFile]): Unit =
325324
try compileSources(files.map(runContext.getSource(_)))
326-
catch case NonFatal(ex) if !this.enrichedErrorMessage =>
325+
catch case ex: Exception if !this.enrichedErrorMessage =>
327326
val files1 = if units.isEmpty then files else units.map(_.source.file)
328327
report.echo(this.enrichErrorMessage(s"exception occurred while compiling ${files1.map(_.path)}"))
329328
throw ex

compiler/src/dotty/tools/dotc/classpath/DirectoryClassPath.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import PlainFile.toPlainFile
1414

1515
import scala.jdk.CollectionConverters.*
1616
import scala.collection.immutable.ArraySeq
17-
import scala.util.control.NonFatal
1817

1918
/**
2019
* A trait allowing to look for classpath entries in directories. It provides common logic for
@@ -134,7 +133,7 @@ object JrtClassPath {
134133
val ctSym = Paths.get(Properties.javaHome).resolve("lib").resolve("ct.sym")
135134
if (Files.notExists(ctSym)) None
136135
else Some(new CtSymClassPath(ctSym, v.toInt))
137-
catch case NonFatal(_) => None
136+
catch case _: Exception => None
138137
case _ =>
139138
try Some(new JrtClassPath(FileSystems.getFileSystem(URI.create("jrt:/"))))
140139
catch case _: ProviderNotFoundException | _: FileSystemNotFoundException => None

0 commit comments

Comments
 (0)