Skip to content

Commit

Permalink
Use foreachSubTree instead of new TreeTraverser (#17303)
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha authored Apr 20, 2023
2 parents ab9e75d + 95d7e79 commit abf9a25
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 43 deletions.
14 changes: 4 additions & 10 deletions compiler/src/dotty/tools/dotc/ast/TreeMapWithImplicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,10 @@ class TreeMapWithImplicits extends tpd.TreeMapWithPreciseStatContexts {

private def patternScopeCtx(pattern: Tree)(using Context): Context = {
val nestedCtx = ctx.fresh.setNewScope
new TreeTraverser {
def traverse(tree: Tree)(using Context): Unit = {
tree match {
case d: DefTree if d.symbol.isOneOf(GivenOrImplicitVal) =>
nestedCtx.enter(d.symbol)
case _ =>
}
traverseChildren(tree)
}
}.traverse(pattern)
pattern.foreachSubTree {
case d: DefTree if d.symbol.isOneOf(GivenOrImplicitVal) => nestedCtx.enter(d.symbol)
case _ =>
}
nestedCtx
}

Expand Down
39 changes: 17 additions & 22 deletions compiler/src/dotty/tools/dotc/inlines/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -970,29 +970,24 @@ class Inliner(val call: tpd.Tree)(using Context):
bindingOfSym(binding.symbol) = binding
}

val countRefs = new TreeTraverser {
override def traverse(t: Tree)(using Context) = {
def updateRefCount(sym: Symbol, inc: Int) =
for (x <- refCount.get(sym)) refCount(sym) = x + inc
def updateTermRefCounts(t: Tree) =
t.typeOpt.foreachPart {
case ref: TermRef => updateRefCount(ref.symbol, 2) // can't be inlined, so make sure refCount is at least 2
case _ =>
}

t match {
case t: RefTree =>
updateRefCount(t.symbol, 1)
updateTermRefCounts(t)
case _: New | _: TypeTree =>
updateTermRefCounts(t)
case _ =>
}
traverseChildren(t)
def updateRefCount(sym: Symbol, inc: Int) =
for (x <- refCount.get(sym)) refCount(sym) = x + inc
def updateTermRefCounts(tree: Tree) =
tree.typeOpt.foreachPart {
case ref: TermRef => updateRefCount(ref.symbol, 2) // can't be inlined, so make sure refCount is at least 2
case _ =>
}
}
countRefs.traverse(tree)
for (binding <- bindings) countRefs.traverse(binding)
def countRefs(tree: Tree) =
tree.foreachSubTree {
case t: RefTree =>
updateRefCount(t.symbol, 1)
updateTermRefCounts(t)
case t @ (_: New | _: TypeTree) =>
updateTermRefCounts(t)
case _ =>
}
countRefs(tree)
for (binding <- bindings) countRefs(binding)

def retain(boundSym: Symbol) = {
refCount.get(boundSym) match {
Expand Down
4 changes: 1 addition & 3 deletions compiler/src/dotty/tools/dotc/quoted/PickledQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,7 @@ object PickledQuotes {
QuotesCache(pickled) = tree

// Make sure trees and positions are fully loaded
new TreeTraverser {
def traverse(tree: Tree)(using Context): Unit = traverseChildren(tree)
}.traverse(tree)
tree.foreachSubTree(identity)

quotePickling.println(i"**** unpickled quote\n$tree")

Expand Down
12 changes: 4 additions & 8 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1193,15 +1193,11 @@ trait Checking {
*/
def checkNoForwardDependencies(vparams: List[ValDef])(using Context): Unit = vparams match {
case vparam :: vparams1 =>
val check = new TreeTraverser {
def traverse(tree: Tree)(using Context) = tree match {
case id: Ident if vparams.exists(_.symbol == id.symbol) =>
report.error(em"illegal forward reference to method parameter", id.srcPos)
case _ =>
traverseChildren(tree)
}
vparam.tpt.foreachSubTree {
case id: Ident if vparams.exists(_.symbol == id.symbol) =>
report.error(em"illegal forward reference to method parameter", id.srcPos)
case _ =>
}
check.traverse(vparam.tpt)
checkNoForwardDependencies(vparams1)
case Nil =>
}
Expand Down

0 comments on commit abf9a25

Please sign in to comment.