Skip to content

Use foreachSubTree instead of new TreeTraverser #17303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -963,29 +963,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