Skip to content

Commit

Permalink
Merge pull request #10302 from som-snytt/issue/12726-CME-unit-checks
Browse files Browse the repository at this point in the history
Avoid exception in post-typer checks
  • Loading branch information
lrytz committed Mar 16, 2023
2 parents 34e01b4 + d9ec261 commit 36da1b3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
17 changes: 8 additions & 9 deletions src/compiler/scala/tools/nsc/CompilationUnits.scala
Expand Up @@ -14,8 +14,7 @@ package scala.tools.nsc

import scala.annotation.nowarn
import scala.reflect.internal.util.{FreshNameCreator, NoSourceFile, SourceFile}
import scala.collection.mutable
import scala.collection.mutable.{LinkedHashSet, ListBuffer}
import scala.collection.mutable, mutable.ArrayDeque

trait CompilationUnits { global: Global =>

Expand Down Expand Up @@ -127,9 +126,9 @@ trait CompilationUnits { global: Global =>
val transformed = new mutable.AnyRefMap[Tree, Tree]

/** things to check at end of compilation unit */
val toCheck = new ListBuffer[CompilationUnit.ToCheck]
private[nsc] def addPostUnitCheck(check: CompilationUnit.ToCheckAfterUnit): Unit = toCheck += check
private[nsc] def addPostTyperCheck(check: CompilationUnit.ToCheckAfterTyper): Unit = toCheck += check
val toCheck = ArrayDeque.empty[CompilationUnit.ToCheck]
private[nsc] def addPostUnitCheck(check: CompilationUnit.ToCheckAfterUnit): Unit = toCheck.append(check)
private[nsc] def addPostTyperCheck(check: CompilationUnit.ToCheckAfterTyper): Unit = toCheck.append(check)

/** The features that were already checked for this unit */
var checkedFeatures = Set[Symbol]()
Expand All @@ -144,7 +143,7 @@ trait CompilationUnits { global: Global =>
def targetPos: Position = NoPosition

/** For sbt compatibility (https://github.com/scala/scala/pull/4588) */
val icode: LinkedHashSet[icodes.IClass] = new LinkedHashSet
val icode: mutable.LinkedHashSet[icodes.IClass] = new mutable.LinkedHashSet

/** Is this about a .java source file? */
val isJava: Boolean = source.isJava
Expand All @@ -153,8 +152,8 @@ trait CompilationUnits { global: Global =>
}

object CompilationUnit {
sealed trait ToCheck
trait ToCheckAfterUnit extends ToCheck { def apply(): Unit }
trait ToCheckAfterTyper extends ToCheck { def apply(): Unit }
sealed trait ToCheck { def apply(): Unit }
trait ToCheckAfterUnit extends ToCheck
trait ToCheckAfterTyper extends ToCheck
}
}
19 changes: 11 additions & 8 deletions src/compiler/scala/tools/nsc/typechecker/Analyzer.scala
Expand Up @@ -13,7 +13,7 @@
package scala.tools.nsc
package typechecker

import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ArrayDeque

/** Defines the sub-components for the namer, packageobjects, and typer phases.
*/
Expand Down Expand Up @@ -100,7 +100,8 @@ trait Analyzer extends AnyRef
// Lacking a better fix, we clear it here (before the phase is created, meaning for each
// compiler run). This is good enough for the resident compiler, which was the most affected.
undoLog.clear()
private val toCheckAfterTyper = new ListBuffer[CompilationUnit.ToCheckAfterTyper]
private val toCheckAfterTyper = ArrayDeque.empty[CompilationUnit.ToCheckAfterTyper]
def addCheckAfterTyper(check: CompilationUnit.ToCheckAfterTyper): Unit = toCheckAfterTyper.append(check)
override def run(): Unit = {
val start = if (settings.areStatisticsEnabled) statistics.startTimer(statistics.typerNanos) else null
global.echoPhaseSummary(this)
Expand All @@ -111,8 +112,8 @@ trait Analyzer extends AnyRef
undoLog.clear()
}
finishComputeParamAlias()
for (work <- toCheckAfterTyper) work()
toCheckAfterTyper.clear()
try while (toCheckAfterTyper.nonEmpty) toCheckAfterTyper.removeHead().apply()
finally toCheckAfterTyper.clearAndShrink()
// defensive measure in case the bookkeeping in deferred macro expansion is buggy
clearDelayed()
if (settings.areStatisticsEnabled) statistics.stopTimer(statistics.typerNanos, start)
Expand All @@ -123,9 +124,11 @@ trait Analyzer extends AnyRef
unit.body = typer.typed(unit.body)
// interactive typed may finish by throwing a `TyperResult`
if (!settings.Youtline.value) {
unit.toCheck foreach {
case now: CompilationUnit.ToCheckAfterUnit => now()
case later: CompilationUnit.ToCheckAfterTyper => toCheckAfterTyper += later
while (unit.toCheck.nonEmpty) {
unit.toCheck.removeHead() match {
case now: CompilationUnit.ToCheckAfterUnit => now()
case later: CompilationUnit.ToCheckAfterTyper => addCheckAfterTyper(later)
}
}
if (!settings.isScaladoc && settings.warnUnusedImport)
warnUnusedImports(unit)
Expand All @@ -135,7 +138,7 @@ trait Analyzer extends AnyRef
}
finally {
runReporting.reportSuspendedMessages(unit)
unit.toCheck.clear()
unit.toCheck.clearAndShrink()
}
}
}
Expand Down

0 comments on commit 36da1b3

Please sign in to comment.