Skip to content

Commit fef5807

Browse files
committed
simplify monitor
[Cherry-picked 7ccdd40][modified]
1 parent cf60f88 commit fef5807

File tree

4 files changed

+41
-42
lines changed

4 files changed

+41
-42
lines changed

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

Lines changed: 13 additions & 11 deletions
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 =>
@@ -350,7 +347,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
350347
if (!ctx.reporter.hasErrors)
351348
Rewrites.writeBack()
352349
suppressions.runFinished(hasErrors = ctx.reporter.hasErrors)
353-
while (finalizeActions.nonEmpty && didEnterFinal()) {
350+
while (finalizeActions.nonEmpty && canProgress()) {
354351
val action = finalizeActions.remove(0)
355352
action()
356353
}
@@ -571,8 +568,13 @@ object Run {
571568
extension (run: Run | Null)
572569

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

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

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ object Phases {
329329
val buf = List.newBuilder[CompilationUnit]
330330
for unit <- units do
331331
given unitCtx: Context = runCtx.fresh.setPhase(this.start).setCompilationUnit(unit).withRootImports
332-
if ctx.run.enterUnit() then
332+
if ctx.run.enterUnit(unit) then
333333
try run
334334
catch case ex: Throwable if !ctx.run.enrichedErrorMessage =>
335335
println(ctx.run.enrichErrorMessage(s"unhandled exception while running $phaseName on $unit"))
@@ -446,29 +446,26 @@ object Phases {
446446
final def iterator: Iterator[Phase] =
447447
Iterator.iterate(this)(_.next) takeWhile (_.hasNext)
448448

449-
/** run the body as one iteration of a (sub)phase (see Run.Progress), Enrich crash messages */
449+
/** Cancellable region, if not cancelled, run the body in the context of the current compilation unit.
450+
* Enrich crash messages.
451+
*/
450452
final def monitor(doing: String)(body: Context ?=> Unit)(using Context): Boolean =
451-
if ctx.run.enterUnit() then
453+
val unit = ctx.compilationUnit
454+
if ctx.run.enterUnit(unit) then
452455
try {body; true}
453-
catch
454-
case NonFatal(ex) if !ctx.run.enrichedErrorMessage =>
455-
report.echo(ctx.run.enrichErrorMessage(s"exception occurred while $doing ${ctx.compilationUnit}"))
456-
throw ex
456+
catch case NonFatal(ex) if !ctx.run.enrichedErrorMessage =>
457+
report.echo(ctx.run.enrichErrorMessage(s"exception occurred while $doing $unit"))
458+
throw ex
457459
finally ctx.run.advanceUnit()
458460
else
459461
false
460462

461-
/** run the body as one iteration of a (sub)phase (see Run.Progress), Enrich crash messages */
462-
final def monitorOpt[T](doing: String)(body: Context ?=> Option[T])(using Context): Option[T] =
463-
if ctx.run.enterUnit() then
464-
try body
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
469-
finally ctx.run.advanceUnit()
463+
/** Do not run if compile progress has been cancelled */
464+
final def cancellable(body: Context ?=> Unit)(using Context): Boolean =
465+
if ctx.run.enterRegion() then
466+
{body; true}
470467
else
471-
None
468+
false
472469

473470
override def toString: String = phaseName
474471
}

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

Lines changed: 6 additions & 6 deletions
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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ 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-
Semantic.checkClasses(classes)(using checkCtx)
49+
Semantic.checkClasses(classes)(using checkCtx)
50+
}
5251

53-
unitContexts0.map(_.compilationUnit)
52+
units0
53+
end runOn
5454

5555
def run(using Context): Unit = unsupported("run")
5656

0 commit comments

Comments
 (0)