Skip to content

Commit e3ba1cb

Browse files
committed
Merge Run and RunInfo
With late source loading we need to access the current run from the current context anyway, so there's no need to separate Run and RunInfo.
1 parent 48fde8b commit e3ba1cb

File tree

8 files changed

+66
-68
lines changed

8 files changed

+66
-68
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class Compiler {
119119

120120
def reset()(implicit ctx: Context): Unit = {
121121
ctx.base.reset()
122-
ctx.runInfo.clear()
122+
if (ctx.run != null) ctx.run.reset()
123123
}
124124

125125
def newRun(implicit ctx: Context): Run = {

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

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ import dotty.tools.io.VirtualFile
2525
import scala.util.control.NonFatal
2626

2727
/** A compiler run. Exports various methods to compile source files */
28-
class Run(comp: Compiler, ictx: Context) {
29-
import Run._
28+
class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with ConstraintRunInfo {
3029

3130
/** Produces the following contexts, from outermost to innermost
3231
*
@@ -53,13 +52,42 @@ class Run(comp: Compiler, ictx: Context) {
5352
ctx.initialize()(start) // re-initialize the base context with start
5453
def addImport(ctx: Context, refFn: () => TermRef) =
5554
ctx.fresh.setImportInfo(ImportInfo.rootImport(refFn)(ctx))
56-
(start.setRunInfo(new RunInfo(start)) /: defn.RootImportFns)(addImport)
55+
(start.setRun(this) /: defn.RootImportFns)(addImport)
5756
}
5857

59-
protected[this] implicit val ctx: Context = rootContext(ictx)
58+
private[this] var myCtx = rootContext(ictx)
59+
60+
/** The context created for this run */
61+
def runContext = myCtx
62+
63+
protected[this] implicit def ctx: Context = myCtx
6064
assert(ctx.runId <= Periods.MaxPossibleRunId)
6165

62-
def getSource(fileName: String): SourceFile = {
66+
private[this] var myUnits: List[CompilationUnit] = _
67+
private[this] var myUnitsCached: List[CompilationUnit] = _
68+
private[this] var myFiles: Set[AbstractFile] = _
69+
70+
/** The compilation units currently being compiled, this may return different
71+
* results over time.
72+
*/
73+
def units: List[CompilationUnit] = myUnits
74+
75+
private def units_=(us: List[CompilationUnit]): Unit =
76+
myUnits = us
77+
78+
/** The files currently being compiled, this may return different results over time.
79+
* These files do not have to be source files since it's possible to compile
80+
* from TASTY.
81+
*/
82+
def files: Set[AbstractFile] = {
83+
if (myUnits ne myUnitsCached) {
84+
myUnitsCached = myUnits
85+
myFiles = myUnits.map(_.source.file).toSet
86+
}
87+
myFiles
88+
}
89+
90+
private def getSource(fileName: String): SourceFile = {
6391
val f = new PlainFile(io.Path(fileName))
6492
if (f.isDirectory) {
6593
ctx.error(s"expected file, received directory '$fileName'")
@@ -78,7 +106,7 @@ class Run(comp: Compiler, ictx: Context) {
78106
compileSources(sources)
79107
} catch {
80108
case NonFatal(ex) =>
81-
ctx.echo(i"exception occurred while compiling ${ctx.runInfo.units}%, %")
109+
ctx.echo(i"exception occurred while compiling $units%, %")
82110
throw ex
83111
}
84112

@@ -90,17 +118,17 @@ class Run(comp: Compiler, ictx: Context) {
90118
*/
91119
def compileSources(sources: List[SourceFile]) =
92120
if (sources forall (_.exists)) {
93-
ctx.runInfo.units = sources map (new CompilationUnit(_))
121+
units = sources map (new CompilationUnit(_))
94122
compileUnits()
95123
}
96124

97125
def compileUnits(us: List[CompilationUnit]): Unit = {
98-
ctx.runInfo.units = us
126+
units = us
99127
compileUnits()
100128
}
101129

102130
def compileUnits(us: List[CompilationUnit], ctx: Context): Unit = {
103-
ctx.runInfo.units = us
131+
units = us
104132
compileUnits()(ctx)
105133
}
106134

@@ -122,16 +150,16 @@ class Run(comp: Compiler, ictx: Context) {
122150
if (phase.isRunnable)
123151
Stats.trackTime(s"$phase ms ") {
124152
val start = System.currentTimeMillis
125-
ctx.runInfo.units = phase.runOn(ctx.runInfo.units)
153+
units = phase.runOn(units)
126154
if (ctx.settings.Xprint.value.containsPhase(phase)) {
127-
for (unit <- ctx.runInfo.units) {
155+
for (unit <- units) {
128156
lastPrintedTree =
129157
printTree(lastPrintedTree)(ctx.fresh.setPhase(phase.next).setCompilationUnit(unit))
130158
}
131159
}
132160
ctx.informTime(s"$phase ", start)
133161
Stats.record(s"total trees at end of $phase", ast.Trees.ntrees)
134-
for (unit <- ctx.runInfo.units)
162+
for (unit <- units)
135163
Stats.record(s"retained typed trees at end of $phase", unit.tpdTree.treeSize)
136164
}
137165
}
@@ -180,46 +208,19 @@ class Run(comp: Compiler, ictx: Context) {
180208
compileSources(List(new SourceFile(virtualFile, Codec.UTF8)))
181209
}
182210

183-
/** The context created for this run */
184-
def runContext = ctx
185-
186211
/** Print summary; return # of errors encountered */
187212
def printSummary(): Reporter = {
188-
ctx.runInfo.printMaxConstraint()
213+
printMaxConstraint()
189214
val r = ctx.reporter
190215
r.printSummary
191216
r
192217
}
193-
}
194-
195-
object Run {
196-
/** Info that changes on each compiler run */
197-
class RunInfo(initctx: Context) extends ImplicitRunInfo with ConstraintRunInfo {
198-
implicit val ctx: Context = initctx
199-
200-
private[this] var myUnits: List[CompilationUnit] = _
201-
private[this] var myUnitsCached: List[CompilationUnit] = _
202-
private[this] var myFiles: Set[AbstractFile] = _
203-
204-
/** The compilation units currently being compiled, this may return different
205-
* results over time.
206-
*/
207-
def units: List[CompilationUnit] = myUnits
208-
209-
private[Run] def units_=(us: List[CompilationUnit]): Unit =
210-
myUnits = us
211-
212-
213-
/** The files currently being compiled, this may return different results over time.
214-
* These files do not have to be source files since it's possible to compile
215-
* from TASTY.
216-
*/
217-
def files: Set[AbstractFile] = {
218-
if (myUnits ne myUnitsCached) {
219-
myUnitsCached = myUnits
220-
myFiles = myUnits.map(_.source.file).toSet
221-
}
222-
myFiles
223-
}
218+
219+
override def reset() = {
220+
super[ImplicitRunInfo].reset()
221+
super[ConstraintRunInfo].reset()
222+
myCtx = null
223+
myUnits = null
224+
myUnitsCached = null
224225
}
225-
}
226+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package dotty.tools.dotc
22
package core
33

44
import Contexts._
5-
import Run.RunInfo
65
import config.Printers.typr
76

8-
trait ConstraintRunInfo { self: RunInfo =>
7+
trait ConstraintRunInfo { self: Run =>
98
private[this] var maxSize = 0
109
private[this] var maxConstraint: Constraint = _
1110
def recordConstraintSize(c: Constraint, size: Int) =
@@ -15,4 +14,6 @@ trait ConstraintRunInfo { self: RunInfo =>
1514
}
1615
def printMaxConstraint()(implicit ctx: Context) =
1716
if (maxSize > 0) typr.println(s"max constraint = ${maxConstraint.show}")
17+
18+
protected def reset() = maxConstraint = null
1819
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import NameOps._
1414
import Uniques._
1515
import SymDenotations._
1616
import Comments._
17-
import Run.RunInfo
1817
import util.Positions._
1918
import ast.Trees._
2019
import ast.untpd
@@ -43,7 +42,7 @@ object Contexts {
4342
private val (settingsStateLoc, store4) = store3.newLocation[SettingsState]()
4443
private val (freshNamesLoc, store5) = store4.newLocation[FreshNameCreator](new FreshNameCreator.Default)
4544
private val (compilationUnitLoc, store6) = store5.newLocation[CompilationUnit]()
46-
private val (runInfoLoc, store7) = store6.newLocation[RunInfo]()
45+
private val (runLoc, store7) = store6.newLocation[Run]()
4746
private val initialStore = store7
4847

4948
/** A context is passed basically everywhere in dotc.
@@ -194,8 +193,8 @@ object Contexts {
194193
/** The current compilation unit */
195194
def compilationUnit: CompilationUnit = store(compilationUnitLoc)
196195

197-
/** The current compiler-run specific Info */
198-
def runInfo: RunInfo = store(runInfoLoc)
196+
/** The current compiler-run */
197+
def run: Run = store(runLoc)
199198

200199
/** The new implicit references that are introduced by this scope */
201200
protected var implicitsCache: ContextualImplicits = null
@@ -446,7 +445,7 @@ object Contexts {
446445
def setPrinterFn(printer: Context => Printer): this.type = updateStore(printerFnLoc, printer)
447446
def setSettings(settingsState: SettingsState): this.type = updateStore(settingsStateLoc, settingsState)
448447
def setCompilationUnit(compilationUnit: CompilationUnit): this.type = updateStore(compilationUnitLoc, compilationUnit)
449-
def setRunInfo(runInfo: RunInfo): this.type = updateStore(runInfoLoc, runInfo)
448+
def setRun(run: Run): this.type = updateStore(runLoc, run)
450449
def setFreshNames(freshNames: FreshNameCreator): this.type = updateStore(freshNamesLoc, freshNames)
451450

452451
def setProperty[T](key: Key[T], value: T): this.type =
@@ -506,9 +505,7 @@ object Contexts {
506505
tree = untpd.EmptyTree
507506
typeAssigner = TypeAssigner
508507
moreProperties = Map.empty
509-
store = initialStore
510-
.updated(settingsStateLoc, settings.defaultState)
511-
.updated(runInfoLoc, new RunInfo(this))
508+
store = initialStore.updated(settingsStateLoc, settings.defaultState)
512509
typeComparer = new TypeComparer(this)
513510
searchHistory = new SearchHistory(0, Map())
514511
gadt = EmptyGADTMap

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ object OrderingConstraint {
2626
private def newConstraint(boundsMap: ParamBounds, lowerMap: ParamOrdering, upperMap: ParamOrdering)(implicit ctx: Context) : OrderingConstraint = {
2727
val result = new OrderingConstraint(boundsMap, lowerMap, upperMap)
2828
if (Config.checkConstraintsNonCyclic) result.checkNonCyclic()
29-
ctx.runInfo.recordConstraintSize(result, result.boundsMap.size)
29+
ctx.run.recordConstraintSize(result, result.boundsMap.size)
3030
result
3131
}
3232

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ object Symbols {
446446
final def isDefinedInCurrentRun(implicit ctx: Context): Boolean =
447447
pos.exists && defRunId == ctx.runId && {
448448
val file = associatedFile
449-
file != null && ctx.runInfo.files.contains(file)
449+
file != null && ctx.run.files.contains(file)
450450
}
451451

452452
/** Is symbol valid in current run? */

compiler/src/dotty/tools/dotc/interactive/InteractiveDriver.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ class InteractiveDriver(settings: List[String]) extends Driver {
191191
if (!t.symbol.isCompleted) t.symbol.info = UnspecifiedErrorType
192192
t.symbol.annotations.foreach { annot =>
193193
/* In some cases annotations are are used on themself (possibly larger cycles).
194-
* This is the case with the java.lang.annotation.Target annotation, would end
195-
* in an infinite loop while cleaning. The `seen` is added to ensure that those
194+
* This is the case with the java.lang.annotation.Target annotation, would end
195+
* in an infinite loop while cleaning. The `seen` is added to ensure that those
196196
* trees are not cleand twice.
197197
* TODO: Find a less expensive way to check for those cycles.
198198
*/
@@ -226,7 +226,7 @@ class InteractiveDriver(settings: List[String]) extends Driver {
226226

227227
run.compileSources(List(source))
228228
run.printSummary()
229-
val t = ctx.runInfo.units.head.tpdTree
229+
val t = ctx.run.units.head.tpdTree
230230
cleanup(t)
231231
myOpenedTrees(uri) = topLevelClassTrees(t, source)
232232

compiler/src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import util.Stats.{track, record, monitored}
99
import printing.{Showable, Printer}
1010
import printing.Texts._
1111
import Contexts._
12-
import Run.RunInfo
1312
import Types._
1413
import Flags._
1514
import TypeErasure.{erasure, hasStableErasure}
@@ -372,7 +371,7 @@ object Implicits {
372371
import Implicits._
373372

374373
/** Info relating to implicits that is kept for one run */
375-
trait ImplicitRunInfo { self: RunInfo =>
374+
trait ImplicitRunInfo { self: Run =>
376375

377376
private val implicitScopeCache = mutable.AnyRefMap[Type, OfTypeImplicits]()
378377

@@ -502,7 +501,7 @@ trait ImplicitRunInfo { self: RunInfo =>
502501
iscope(rootTp)
503502
}
504503

505-
def clear() = {
504+
protected def reset() = {
506505
implicitScopeCache.clear()
507506
}
508507
}
@@ -1055,7 +1054,7 @@ trait Implicits { self: Typer =>
10551054
}
10561055
}
10571056

1058-
def implicitScope(tp: Type): OfTypeImplicits = ctx.runInfo.implicitScope(tp, ctx)
1057+
def implicitScope(tp: Type): OfTypeImplicits = ctx.run.implicitScope(tp, ctx)
10591058
}
10601059
}
10611060

0 commit comments

Comments
 (0)