Skip to content

Commit 908c1c9

Browse files
committed
Fix Symbol#isDefinedInCurrentRun
And add a test to make sure it's working. The rest required a change to the testing infrastructure: files in compilation groups should be sorted alphabetically, otherwise we cannot know in advance if the error will happen in A1.scala or A2.scala
1 parent 6d5c226 commit 908c1c9

File tree

11 files changed

+64
-28
lines changed

11 files changed

+64
-28
lines changed

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

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ import Types._
1010
import Scopes._
1111
import typer.{FrontEnd, Typer, ImportInfo, RefChecks}
1212
import Decorators._
13-
import io.PlainFile
13+
import io.{AbstractFile, PlainFile}
1414
import scala.io.Codec
15-
import util._
15+
import util.{Set => _, _}
1616
import reporting.Reporter
1717
import transform.TreeChecker
1818
import rewrite.Rewrites
1919
import java.io.{BufferedWriter, OutputStreamWriter}
2020
import printing.XprintMode
21+
import typer.ImplicitRunInfo
2122

2223
import scala.annotation.tailrec
2324
import dotty.tools.io.VirtualFile
2425
import scala.util.control.NonFatal
2526

2627
/** A compiler run. Exports various methods to compile source files */
2728
class Run(comp: Compiler, ictx: Context) {
28-
29-
var units: List[CompilationUnit] = _
29+
import Run._
3030

3131
/** Produces the following contexts, from outermost to innermost
3232
*
@@ -78,7 +78,7 @@ class Run(comp: Compiler, ictx: Context) {
7878
compileSources(sources)
7979
} catch {
8080
case NonFatal(ex) =>
81-
ctx.echo(i"exception occurred while compiling $units%, %")
81+
ctx.echo(i"exception occurred while compiling ${ctx.runInfo.units}%, %")
8282
throw ex
8383
}
8484

@@ -90,17 +90,17 @@ class Run(comp: Compiler, ictx: Context) {
9090
*/
9191
def compileSources(sources: List[SourceFile]) =
9292
if (sources forall (_.exists)) {
93-
units = sources map (new CompilationUnit(_))
93+
ctx.runInfo.units = sources map (new CompilationUnit(_))
9494
compileUnits()
9595
}
9696

9797
def compileUnits(us: List[CompilationUnit]): Unit = {
98-
units = us
98+
ctx.runInfo.units = us
9999
compileUnits()
100100
}
101101

102102
def compileUnits(us: List[CompilationUnit], ctx: Context): Unit = {
103-
units = us
103+
ctx.runInfo.units = us
104104
compileUnits()(ctx)
105105
}
106106

@@ -122,16 +122,16 @@ class Run(comp: Compiler, ictx: Context) {
122122
if (phase.isRunnable)
123123
Stats.trackTime(s"$phase ms ") {
124124
val start = System.currentTimeMillis
125-
units = phase.runOn(units)
125+
ctx.runInfo.units = phase.runOn(ctx.runInfo.units)
126126
if (ctx.settings.Xprint.value.containsPhase(phase)) {
127-
for (unit <- units) {
127+
for (unit <- ctx.runInfo.units) {
128128
lastPrintedTree =
129129
printTree(lastPrintedTree)(ctx.fresh.setPhase(phase.next).setCompilationUnit(unit))
130130
}
131131
}
132132
ctx.informTime(s"$phase ", start)
133133
Stats.record(s"total trees at end of $phase", ast.Trees.ntrees)
134-
for (unit <- units)
134+
for (unit <- ctx.runInfo.units)
135135
Stats.record(s"retained typed trees at end of $phase", unit.tpdTree.treeSize)
136136
}
137137
}
@@ -191,3 +191,35 @@ class Run(comp: Compiler, ictx: Context) {
191191
r
192192
}
193193
}
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+
}
224+
}
225+
}

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

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

44
import Contexts._
5+
import Run.RunInfo
56
import config.Printers.typr
67

78
trait ConstraintRunInfo { self: RunInfo =>

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ import NameOps._
1414
import Uniques._
1515
import SymDenotations._
1616
import Comments._
17+
import Run.RunInfo
1718
import util.Positions._
1819
import ast.Trees._
1920
import ast.untpd
2021
import util.{FreshNameCreator, SimpleIdentityMap, SourceFile, NoSource}
21-
import typer.{Implicits, ImplicitRunInfo, ImportInfo, Inliner, NamerContextOps, SearchHistory, TypeAssigner, Typer}
22+
import typer.{Implicits, ImportInfo, Inliner, NamerContextOps, SearchHistory, TypeAssigner, Typer}
2223
import Implicits.ContextualImplicits
2324
import config.Settings._
2425
import config.Config
@@ -675,11 +676,6 @@ object Contexts {
675676
// @sharable val theBase = new ContextBase // !!! DEBUG, so that we can use a minimal context for reporting even in code that normally cannot access a context
676677
}
677678

678-
/** Info that changes on each compiler run */
679-
class RunInfo(initctx: Context) extends ImplicitRunInfo with ConstraintRunInfo {
680-
implicit val ctx: Context = initctx
681-
}
682-
683679
class GADTMap(initBounds: SimpleIdentityMap[Symbol, TypeBounds]) extends util.DotClass {
684680
private[this] var myBounds = initBounds
685681
def setBounds(sym: Symbol, b: TypeBounds): Unit =

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -440,10 +440,11 @@ object Symbols {
440440
if (lastDenot == null) NoRunId else lastDenot.validFor.runId
441441

442442
/** Does this symbol come from a currently compiled source file? */
443-
final def isDefinedInCurrentRun(implicit ctx: Context): Boolean = {
444-
// FIXME: broken now now that symbols unpickled from TASTY have a position
445-
pos.exists && defRunId == ctx.runId
446-
}
443+
final def isDefinedInCurrentRun(implicit ctx: Context): Boolean =
444+
pos.exists && defRunId == ctx.runId && {
445+
val file = associatedFile
446+
file != null && ctx.runInfo.files.contains(file)
447+
}
447448

448449
final def isValidInCurrentRun(implicit ctx: Context): Boolean =
449450
lastDenot.validFor.runId == ctx.runId || ctx.stillValid(lastDenot)
@@ -540,7 +541,7 @@ object Symbols {
540541
* Overridden in ClassSymbol
541542
*/
542543
def associatedFile(implicit ctx: Context): AbstractFile =
543-
denot.topLevelClass.symbol.associatedFile
544+
if (lastDenot == null) null else lastDenot.topLevelClass.symbol.associatedFile
544545

545546
/** The class file from which this class was generated, null if not applicable. */
546547
final def binaryFile(implicit ctx: Context): AbstractFile = {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import core.Contexts._
66

77
class TASTYRun(comp: Compiler, ictx: Context) extends Run(comp, ictx) {
88
override def compile(classNames: List[String]) = {
9-
units = classNames.map(new TASTYCompilationUnit(_))
10-
compileUnits()
9+
val units = classNames.map(new TASTYCompilationUnit(_))
10+
compileUnits(units)
1111
}
1212
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class InteractiveDriver(settings: List[String]) extends Driver {
226226

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import util.Stats.{track, record, monitored}
99
import printing.{Showable, Printer}
1010
import printing.Texts._
1111
import Contexts._
12+
import Run.RunInfo
1213
import Types._
1314
import Flags._
1415
import TypeErasure.{erasure, hasStableErasure}

compiler/test/dotty/tools/dotc/CompilerTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ abstract class CompilerTest {
104104
else (fp, jfp)
105105
}
106106
val expErrors = expectedErrors(filePaths.toList)
107-
(filePaths, javaFilePaths, normArgs, expErrors)
107+
(filePaths.sorted, javaFilePaths.sorted, normArgs, expErrors)
108108
}
109109
if (runTest)
110110
log(s"WARNING: run tests can only be run by partest, JUnit just verifies compilation: $prefix$dirName")

compiler/test/dotty/tools/vulpix/ParallelTesting.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ trait ParallelTesting extends RunnerOrchestration { self =>
151151
) extends TestSource {
152152

153153
/** Get the files grouped by `_X` as a list of groups, files missing this
154-
* suffix will be put into the same group
154+
* suffix will be put into the same group.
155+
* Files in each group are sorted alphabetically.
155156
*
156157
* Filters out all none source files
157158
*/
@@ -170,7 +171,7 @@ trait ParallelTesting extends RunnerOrchestration { self =>
170171
.toOption
171172
.getOrElse("")
172173
}
173-
.toList.sortBy(_._1).map(_._2.filter(isSourceFile))
174+
.toList.sortBy(_._1).map(_._2.filter(isSourceFile).sorted)
174175
}
175176

176177
/** Each `Test` takes the `testSources` and performs the compilation and assertions

tests/neg/checkNoConflict/A1.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package foo
2+
class A

tests/neg/checkNoConflict/A2.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package foo
2+
class A // error: class A in package foo has already been compiled once during this run

0 commit comments

Comments
 (0)