Skip to content

Commit c208fe9

Browse files
committed
Refactor TreeInfo#defPath
Uses a TreeAccumulator, rather than ad-hoc descent through `productIterator`.
1 parent fd1214c commit c208fe9

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -421,30 +421,19 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
421421
* Pre: `sym` must have a position.
422422
*/
423423
def defPath(sym: Symbol, root: Tree)(implicit ctx: Context): List[Tree] = ctx.debugTraceIndented(s"defpath($sym with position ${sym.pos}, ${root.show})") {
424-
def show(from: Any): String = from match {
425-
case tree: Trees.Tree[_] => s"${tree.show} with attachments ${tree.allAttachments}"
426-
case x: printing.Showable => x.show
427-
case x => x.toString
428-
}
429-
430-
def search(from: Any): List[Tree] = ctx.debugTraceIndented(s"search(${show(from)})") {
431-
from match {
432-
case tree: Tree => // Dotty problem: cannot write Tree @ unchecked, this currently gives a syntax error
433-
if (definedSym(tree) == sym) tree :: Nil
434-
else if (tree.envelope.contains(sym.pos)) {
435-
val p = search(tree.productIterator)
436-
if (p.isEmpty) p else tree :: p
437-
} else Nil
438-
case xs: Iterable[_] =>
439-
search(xs.iterator)
440-
case xs: Iterator[_] =>
441-
xs.map(search).find(_.nonEmpty).getOrElse(Nil)
442-
case _ =>
443-
Nil
424+
require(sym.pos.exists)
425+
object accum extends TreeAccumulator[List[Tree]] {
426+
def apply(x: List[Tree], tree: Tree): List[Tree] = {
427+
if (tree.envelope.contains(sym.pos))
428+
if (definedSym(tree) == sym) tree :: x
429+
else {
430+
val x1 = foldOver(x, tree)
431+
if (x1 ne x) tree :: x1 else x1
432+
}
433+
else x
444434
}
445435
}
446-
require(sym.pos.exists)
447-
search(root)
436+
accum(Nil, root)
448437
}
449438

450439
/** The statement sequence that contains a definition of `sym`, or Nil

0 commit comments

Comments
 (0)