Skip to content

Commit 952eb76

Browse files
committed
Compute trees from sources in compilation unit used for entering symbols
Don't parse a new tree, since this generates a new set of symbols which are hard to correlate with the old ones.
1 parent 9349cd6 commit 952eb76

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,26 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
189189
/** Enter top-level definitions of classes and objects contain in Scala source file `file`.
190190
* The newly added symbols replace any previously entered symbols.
191191
*/
192-
def enterRoots(file: AbstractFile)(implicit ctx: Context): Unit =
192+
def enterRoots(file: AbstractFile)(implicit ctx: Context): Option[CompilationUnit] =
193193
if (!files.contains(file) && !lateFiles.contains(file)) {
194194
lateFiles += file
195195
val unit = new CompilationUnit(getSource(file.path))
196196
enterRoots(unit)(runContext.fresh.setCompilationUnit(unit))
197+
Some(unit)
197198
}
199+
else None
198200

199201
private def enterRoots(unit: CompilationUnit)(implicit ctx: Context): Unit = {
200202
unit.untpdTree = new Parser(unit.source).parse()
201203
ctx.typer.lateEnter(unit.untpdTree)
202204
}
203205

206+
def typedTree(unit: CompilationUnit)(implicit ctx: Context) = {
207+
def typeCheck(implicit ctx: Context) = ctx.typer.typedExpr(unit.untpdTree)
208+
if (unit.tpdTree.isEmpty) unit.tpdTree = typeCheck(runContext.fresh.setCompilationUnit(unit))
209+
unit.tpdTree
210+
}
211+
204212
private sealed trait PrintedTree
205213
private /*final*/ case class SomePrintedTree(phase: String, tree: String) extends PrintedTree
206214
private object NoPrintedTree extends PrintedTree

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -398,19 +398,15 @@ class SourcefileLoader(val srcfile: AbstractFile) extends SymbolLoader {
398398
def description(implicit ctx: Context) = "source file " + srcfile.toString
399399
override def sourceFileOrNull = srcfile
400400
def doComplete(root: SymDenotation)(implicit ctx: Context): Unit = {
401-
ctx.run.enterRoots(srcfile)
402-
if (ctx.settings.YretainTrees.value) {
403-
val (classRoot, moduleRoot) = rootDenots(root.asClass)
404-
classRoot.classSymbol.treeOrProvider = treeProvider
405-
moduleRoot.classSymbol.treeOrProvider = treeProvider
406-
}
407-
}
408-
object treeProvider extends tpd.TreeProvider {
409-
def computeTrees(implicit ctx: Context): List[tpd.Tree] = {
410-
var units = new CompilationUnit(ctx.run.getSource(srcfile.path)) :: Nil
411-
for (phase <- ctx.allPhases; if phase.isTyper)
412-
units = phase.runOn(units)
413-
units.map(_.tpdTree)
414-
}
401+
for (unit <- ctx.run.enterRoots(srcfile))
402+
if (ctx.settings.YretainTrees.value) {
403+
val (classRoot, moduleRoot) = rootDenots(root.asClass)
404+
val treeProvider = new tpd.TreeProvider {
405+
def computeTrees(implicit ctx: Context): List[tpd.Tree] =
406+
ctx.run.typedTree(unit) :: Nil
407+
}
408+
classRoot.classSymbol.treeOrProvider = treeProvider
409+
moduleRoot.classSymbol.treeOrProvider = treeProvider
410+
}
415411
}
416412
}

0 commit comments

Comments
 (0)