Skip to content

Commit f2fc99b

Browse files
committed
Remove SourceImportTree
We keep only `SourceTree`, that we had before, but it now accepts any `tpd.Tree`. After we bootstrap, this should become `tpd.Import | tpd.NameTree`.
1 parent 59275a8 commit f2fc99b

File tree

3 files changed

+45
-51
lines changed

3 files changed

+45
-51
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ object Interactive {
279279
* source code.
280280
*/
281281
def namedTrees(trees: List[SourceTree], include: Include.Set, sym: Symbol)
282-
(implicit ctx: Context): List[SourceNamedTree] =
282+
(implicit ctx: Context): List[SourceTree] =
283283
if (!sym.exists)
284284
Nil
285285
else
@@ -288,7 +288,7 @@ object Interactive {
288288
/** Find named trees with a non-empty position whose name contains `nameSubstring` in `trees`.
289289
*/
290290
def namedTrees(trees: List[SourceTree], nameSubstring: String)
291-
(implicit ctx: Context): List[SourceNamedTree] = {
291+
(implicit ctx: Context): List[SourceTree] = {
292292
val predicate: NameTree => Boolean = _.name.toString.contains(nameSubstring)
293293
namedTrees(trees, 0, predicate)
294294
}
@@ -298,10 +298,10 @@ object Interactive {
298298
* @param includeReferences If true, include references and not just definitions
299299
*/
300300
def namedTrees(trees: List[SourceTree], include: Include.Set, treePredicate: NameTree => Boolean)
301-
(implicit ctx: Context): List[SourceNamedTree] = safely {
301+
(implicit ctx: Context): List[SourceTree] = safely {
302302
val includeReferences = (include & Include.references) != 0
303303
val includeImports = (include & Include.imports) != 0
304-
val buf = new mutable.ListBuffer[SourceNamedTree]
304+
val buf = new mutable.ListBuffer[SourceTree]
305305

306306
def traverser(source: SourceFile) = {
307307
new untpd.TreeTraverser {
@@ -319,7 +319,7 @@ object Interactive {
319319
&& !tree.pos.isZeroExtent
320320
&& (includeReferences || isDefinition(tree))
321321
&& treePredicate(tree))
322-
buf += SourceNamedTree(tree, source)
322+
buf += SourceTree(tree, source)
323323
traverseChildren(tree)
324324
case tree: untpd.Inlined =>
325325
traverse(tree.call)
@@ -347,7 +347,7 @@ object Interactive {
347347
includes: Include.Set,
348348
symbol: Symbol,
349349
predicate: NameTree => Boolean = util.common.alwaysTrue
350-
)(implicit ctx: Context): List[SourceNamedTree] = {
350+
)(implicit ctx: Context): List[SourceTree] = {
351351
val linkedSym = symbol.linkedClass
352352
val includeDeclaration = (includes & Include.definitions) != 0
353353
val includeLinkedClass = (includes & Include.linkedClass) != 0
@@ -450,7 +450,7 @@ object Interactive {
450450
* @param driver The driver responsible for `path`.
451451
* @return The definitions for the symbol at the end of `path`.
452452
*/
453-
def findDefinitions(path: List[Tree], pos: SourcePosition, driver: InteractiveDriver)(implicit ctx: Context): List[SourceNamedTree] = {
453+
def findDefinitions(path: List[Tree], pos: SourcePosition, driver: InteractiveDriver)(implicit ctx: Context): List[SourceTree] = {
454454
enclosingSourceSymbols(path, pos).flatMap { sym =>
455455
val enclTree = enclosingTree(path)
456456

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ class InteractiveDriver(val settings: List[String]) extends Driver {
253253
case PackageDef(_, stats) =>
254254
stats.foreach(addTrees)
255255
case imp: Import =>
256-
trees += SourceImportTree(imp, source)
256+
trees += SourceTree(imp, source)
257257
case tree: TypeDef =>
258-
trees += SourceNamedTree(tree, source)
258+
trees += SourceTree(tree, source)
259259
case _ =>
260260
}
261261
addTrees(topTree)

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

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,44 @@ import core._, core.Decorators.{sourcePos => _}
99
import Contexts._, NameOps._, Symbols._, StdNames._
1010
import util._, util.Positions._
1111

12-
/** A `tree` coming from `source` */
13-
sealed trait SourceTree {
14-
15-
/** The underlying tree. */
16-
def tree: tpd.Tree
17-
18-
/** The source from which `tree` comes. */
19-
def source: SourceFile
12+
/**
13+
* A `tree` coming from `source`
14+
*
15+
* `tree` can be either an `Import` or a `NameTree`.
16+
*/
17+
case class SourceTree(tree: tpd.Tree /** really: tpd.Import | tpd.NameTree */, source: SourceFile) {
2018

2119
/** The position of `tree` */
2220
final def pos(implicit ctx: Context): SourcePosition = source.atPos(tree.pos)
23-
}
24-
25-
/** An import coming from `source` */
26-
case class SourceImportTree(tree: tpd.Import, source: SourceFile) extends SourceTree
27-
28-
/** A typechecked `tree` coming from `source` */
29-
case class SourceNamedTree(tree: tpd.NameTree, source: SourceFile) extends SourceTree {
3021

3122
/** The position of the name in `tree` */
32-
def namePos(implicit ctx: Context): SourcePosition = {
33-
// FIXME: Merge with NameTree#namePos ?
34-
val treePos = tree.pos
35-
if (treePos.isZeroExtent || tree.name.toTermName == nme.ERROR)
36-
NoSourcePosition
37-
else {
38-
// Constructors are named `<init>` in the trees, but `this` in the source.
39-
val nameLength = tree.name match {
40-
case nme.CONSTRUCTOR => nme.this_.toString.length
41-
case other => other.stripModuleClassSuffix.show.toString.length
42-
}
43-
val position = {
44-
// FIXME: This is incorrect in some cases, like with backquoted identifiers,
45-
// see https://github.com/lampepfl/dotty/pull/1634#issuecomment-257079436
46-
val (start, end) =
47-
if (!treePos.isSynthetic)
48-
(treePos.point, treePos.point + nameLength)
49-
else
50-
// If we don't have a point, we need to find it
51-
(treePos.end - nameLength, treePos.end)
52-
Position(start, end, start)
23+
def namePos(implicit ctx: Context): SourcePosition = tree match {
24+
case tree: tpd.NameTree =>
25+
// FIXME: Merge with NameTree#namePos ?
26+
val treePos = tree.pos
27+
if (treePos.isZeroExtent || tree.name.toTermName == nme.ERROR)
28+
NoSourcePosition
29+
else {
30+
// Constructors are named `<init>` in the trees, but `this` in the source.
31+
val nameLength = tree.name match {
32+
case nme.CONSTRUCTOR => nme.this_.toString.length
33+
case other => other.stripModuleClassSuffix.show.toString.length
34+
}
35+
val position = {
36+
// FIXME: This is incorrect in some cases, like with backquoted identifiers,
37+
// see https://github.com/lampepfl/dotty/pull/1634#issuecomment-257079436
38+
val (start, end) =
39+
if (!treePos.isSynthetic)
40+
(treePos.point, treePos.point + nameLength)
41+
else
42+
// If we don't have a point, we need to find it
43+
(treePos.end - nameLength, treePos.end)
44+
Position(start, end, start)
45+
}
46+
source.atPos(position)
5347
}
54-
source.atPos(position)
55-
}
48+
case _ =>
49+
NoSourcePosition
5650
}
5751
}
5852

@@ -63,19 +57,19 @@ object SourceTree {
6357
Nil
6458
else {
6559
import ast.Trees._
66-
def sourceTreeOfClass(tree: tpd.Tree): Option[SourceNamedTree] = tree match {
60+
def sourceTreeOfClass(tree: tpd.Tree): Option[SourceTree] = tree match {
6761
case PackageDef(_, stats) =>
6862
stats.flatMap(sourceTreeOfClass).headOption
6963
case tree: tpd.TypeDef if tree.symbol == sym =>
7064
val sourceFile = new SourceFile(sym.sourceFile, Codec.UTF8)
71-
Some(SourceNamedTree(tree, sourceFile))
65+
Some(SourceTree(tree, sourceFile))
7266
case _ =>
7367
None
7468
}
7569

76-
def sourceImports(tree: tpd.Tree, sourceFile: SourceFile): List[SourceImportTree] = tree match {
70+
def sourceImports(tree: tpd.Tree, sourceFile: SourceFile): List[SourceTree] = tree match {
7771
case PackageDef(_, stats) => stats.flatMap(sourceImports(_, sourceFile))
78-
case imp: tpd.Import => SourceImportTree(imp, sourceFile) :: Nil
72+
case imp: tpd.Import => SourceTree(imp, sourceFile) :: Nil
7973
case _ => Nil
8074
}
8175

0 commit comments

Comments
 (0)