Skip to content

Commit 7ccdd40

Browse files
committed
simplify monitor
1 parent 7fc4341 commit 7ccdd40

File tree

4 files changed

+44
-45
lines changed

4 files changed

+44
-45
lines changed

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

+13-11
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,8 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
171171

172172
private var _progress: Progress | Null = null // Set if progress reporting is enabled
173173

174-
/** Only safe to call if progress is being tracked. */
175174
private inline def trackProgress(using Context)(inline op: Context ?=> Progress => Unit): Unit =
176-
val local = _progress
177-
if local != null then
178-
op(using ctx)(local)
175+
foldProgress(())(op)
179176

180177
private inline def foldProgress[T](using Context)(inline default: T)(inline op: Context ?=> Progress => T): T =
181178
val local = _progress
@@ -184,11 +181,11 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
184181
else
185182
default
186183

187-
def didEnterUnit()(using Context): Boolean =
188-
foldProgress(true /* should progress by default */)(_.tryEnterUnit(ctx.compilationUnit))
184+
def didEnterUnit(unit: CompilationUnit)(using Context): Boolean =
185+
foldProgress(true /* should progress by default */)(_.tryEnterUnit(unit))
189186

190-
def didEnterFinal()(using Context): Boolean =
191-
foldProgress(true /* should progress by default */)(p => !p.checkCancellation())
187+
def canProgress()(using Context): Boolean =
188+
foldProgress(true /* not cancelled by default */)(p => !p.checkCancellation())
192189

193190
def doAdvanceUnit()(using Context): Unit =
194191
trackProgress: progress =>
@@ -351,7 +348,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
351348
if (!ctx.reporter.hasErrors)
352349
Rewrites.writeBack()
353350
suppressions.runFinished(hasErrors = ctx.reporter.hasErrors)
354-
while (finalizeActions.nonEmpty && didEnterFinal()) {
351+
while (finalizeActions.nonEmpty && canProgress()) {
355352
val action = finalizeActions.remove(0)
356353
action()
357354
}
@@ -572,8 +569,13 @@ object Run {
572569
extension (run: Run | Null)
573570

574571
/** record that the current phase has begun for the compilation unit of the current Context */
575-
def enterUnit()(using Context): Boolean =
576-
if run != null then run.didEnterUnit()
572+
def enterUnit(unit: CompilationUnit)(using Context): Boolean =
573+
if run != null then run.didEnterUnit(unit)
574+
else true // don't check cancellation if we're not tracking progress
575+
576+
/** check progress cancellation, true if not cancelled */
577+
def enterRegion()(using Context): Boolean =
578+
if run != null then run.canProgress()
577579
else true // don't check cancellation if we're not tracking progress
578580

579581
/** advance the unit count and record progress in the current phase */

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

+14-17
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ object Phases {
340340
val buf = List.newBuilder[CompilationUnit]
341341
for unit <- units do
342342
given unitCtx: Context = runCtx.fresh.setPhase(this.start).setCompilationUnit(unit).withRootImports
343-
if ctx.run.enterUnit() then
343+
if ctx.run.enterUnit(unit) then
344344
try run
345345
catch case ex: Throwable if !ctx.run.enrichedErrorMessage =>
346346
println(ctx.run.enrichErrorMessage(s"unhandled exception while running $phaseName on $unit"))
@@ -458,29 +458,26 @@ object Phases {
458458
final def iterator: Iterator[Phase] =
459459
Iterator.iterate(this)(_.next) takeWhile (_.hasNext)
460460

461-
/** run the body as one iteration of a (sub)phase (see Run.Progress), Enrich crash messages */
461+
/** Cancellable region, if not cancelled, run the body in the context of the current compilation unit.
462+
* Enrich crash messages.
463+
*/
462464
final def monitor(doing: String)(body: Context ?=> Unit)(using Context): Boolean =
463-
if ctx.run.enterUnit() then
465+
val unit = ctx.compilationUnit
466+
if ctx.run.enterUnit(unit) then
464467
try {body; true}
465-
catch
466-
case NonFatal(ex) if !ctx.run.enrichedErrorMessage =>
467-
report.echo(ctx.run.enrichErrorMessage(s"exception occurred while $doing ${ctx.compilationUnit}"))
468-
throw ex
468+
catch case NonFatal(ex) if !ctx.run.enrichedErrorMessage =>
469+
report.echo(ctx.run.enrichErrorMessage(s"exception occurred while $doing $unit"))
470+
throw ex
469471
finally ctx.run.advanceUnit()
470472
else
471473
false
472474

473-
/** run the body as one iteration of a (sub)phase (see Run.Progress), Enrich crash messages */
474-
final def monitorOpt[T](doing: String)(body: Context ?=> Option[T])(using Context): Option[T] =
475-
if ctx.run.enterUnit() then
476-
try body
477-
catch
478-
case NonFatal(ex) if !ctx.run.enrichedErrorMessage =>
479-
report.echo(ctx.run.enrichErrorMessage(s"exception occurred while $doing ${ctx.compilationUnit}"))
480-
throw ex
481-
finally ctx.run.advanceUnit()
475+
/** Do not run if compile progress has been cancelled */
476+
final def cancellable(body: Context ?=> Unit)(using Context): Boolean =
477+
if ctx.run.enterRegion() then
478+
{body; true}
482479
else
483-
None
480+
false
484481

485482
override def toString: String = phaseName
486483
}

compiler/src/dotty/tools/dotc/fromtasty/ReadTasty.scala

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import NameOps._
1212
import ast.Trees.Tree
1313
import Phases.Phase
1414

15-
1615
/** Load trees from TASTY files */
1716
class ReadTasty extends Phase {
1817

@@ -23,13 +22,14 @@ class ReadTasty extends Phase {
2322

2423
override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] =
2524
withMode(Mode.ReadPositions) {
26-
val unitContexts = units.map(unit => ctx.fresh.setCompilationUnit(unit))
27-
unitContexts.flatMap(applyPhase()(using _))
25+
val nextUnits = collection.mutable.ListBuffer.empty[CompilationUnit]
26+
val unitContexts = units.view.map(ctx.fresh.setCompilationUnit)
27+
for given Context <- unitContexts if addTasty(nextUnits += _) do ()
28+
nextUnits.toList
2829
}
2930

30-
private def applyPhase()(using Context): Option[CompilationUnit] = monitorOpt(phaseName):
31-
val unit = ctx.compilationUnit
32-
readTASTY(unit)
31+
def addTasty(fn: CompilationUnit => Unit)(using Context): Boolean = monitor(phaseName):
32+
readTASTY(ctx.compilationUnit).foreach(fn)
3333

3434
def readTASTY(unit: CompilationUnit)(using Context): Option[CompilationUnit] = unit match {
3535
case unit: TASTYCompilationUnit =>

compiler/src/dotty/tools/dotc/transform/init/Checker.scala

+11-11
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,21 @@ class Checker extends Phase:
4040
val traverser = new InitTreeTraverser()
4141
val unitContexts = units.map(unit => checkCtx.fresh.setCompilationUnit(unit))
4242

43-
val unitContexts0 =
44-
for
45-
given Context <- unitContexts
46-
if traverse(traverser)
47-
yield ctx
43+
val units0 =
44+
for given Context <- unitContexts if traverse(traverser) yield ctx.compilationUnit
4845

49-
val classes = traverser.getClasses()
46+
cancellable {
47+
val classes = traverser.getClasses()
5048

51-
if ctx.settings.YcheckInit.value then
52-
Semantic.checkClasses(classes)(using checkCtx)
49+
if ctx.settings.YcheckInit.value then
50+
Semantic.checkClasses(classes)(using checkCtx)
5351

54-
if ctx.settings.YcheckInitGlobal.value then
55-
Objects.checkClasses(classes)(using checkCtx)
52+
if ctx.settings.YcheckInitGlobal.value then
53+
Objects.checkClasses(classes)(using checkCtx)
54+
}
5655

57-
unitContexts0.map(_.compilationUnit)
56+
units0
57+
end runOn
5858

5959
def run(using Context): Unit = unsupported("run")
6060

0 commit comments

Comments
 (0)