Skip to content

Commit b7be521

Browse files
authored
Merge pull request #3224 from dotty-staging/remove-EnclosingMethodTraverser
Remove unnecessary EnclosingMethodTraverser
2 parents b570a62 + 990c3aa commit b7be521

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -978,22 +978,6 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
978978
}
979979
}
980980

981-
/** A traverser that passes the enclosing class or method as an argument
982-
* to the traverse method.
983-
*/
984-
abstract class EnclosingMethodTraverser extends TreeAccumulator[Symbol] {
985-
def traverse(enclMeth: Symbol, tree: Tree)(implicit ctx: Context): Unit
986-
def apply(enclMeth: Symbol, tree: Tree)(implicit ctx: Context) = {
987-
tree match {
988-
case _: DefTree if tree.symbol.exists =>
989-
traverse(tree.symbol.enclosingMethod, tree)
990-
case _ =>
991-
traverse(enclMeth, tree)
992-
}
993-
enclMeth
994-
}
995-
}
996-
997981
/** A key to be used in a context property that tracks enclosing inlined calls */
998982
private val InlinedCalls = new Property.Key[List[Tree]]
999983

compiler/src/dotty/tools/dotc/transform/CapturedVars.scala

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,30 @@ class CapturedVars extends MiniPhase with IdentityDenotTransformer { thisTransfo
5252
myRefInfo
5353
}
5454

55-
private class CollectCaptured(implicit ctx: Context) extends EnclosingMethodTraverser {
55+
private class CollectCaptured extends TreeTraverser {
5656
private val captured = mutable.HashSet[Symbol]()
57-
def traverse(enclMeth: Symbol, tree: Tree)(implicit ctx: Context) = tree match {
57+
def traverse(tree: Tree)(implicit ctx: Context) = tree match {
5858
case id: Ident =>
5959
val sym = id.symbol
60-
if (sym.is(Mutable, butNot = Method) && sym.owner.isTerm && sym.enclosingMethod != enclMeth) {
61-
ctx.log(i"capturing $sym in ${sym.enclosingMethod}, referenced from $enclMeth")
62-
captured += sym
60+
if (sym.is(Mutable, butNot = Method) && sym.owner.isTerm) {
61+
val enclMeth = ctx.owner.enclosingMethod
62+
if (sym.enclosingMethod != enclMeth) {
63+
ctx.log(i"capturing $sym in ${sym.enclosingMethod}, referenced from $enclMeth")
64+
captured += sym
65+
}
6366
}
6467
case _ =>
65-
foldOver(enclMeth, tree)
68+
traverseChildren(tree)
6669
}
67-
def runOver(tree: Tree): collection.Set[Symbol] = {
68-
apply(NoSymbol, tree)
70+
def runOver(tree: Tree)(implicit ctx: Context): collection.Set[Symbol] = {
71+
traverse(tree)
6972
captured
7073
}
7174
}
7275

7376
override def prepareForUnit(tree: Tree)(implicit ctx: Context) = {
74-
val captured = (new CollectCaptured)(ctx.withPhase(thisTransform))
75-
.runOver(ctx.compilationUnit.tpdTree)
77+
val captured = (new CollectCaptured)
78+
.runOver(ctx.compilationUnit.tpdTree)(ctx.withPhase(thisTransform))
7679
new Transform(captured)
7780
}
7881

compiler/src/dotty/tools/dotc/transform/LambdaLift.scala

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,20 @@ class LambdaLift extends MiniPhase with IdentityDenotTransformer { thisTransform
234234
if (callee.enclosingClass != caller.enclosingClass) calledFromInner += callee
235235
}
236236

237-
private class CollectDependencies extends EnclosingMethodTraverser {
238-
def traverse(enclosure: Symbol, tree: Tree)(implicit ctx: Context) = try { //debug
237+
private class CollectDependencies extends TreeTraverser {
238+
def traverse(tree: Tree)(implicit ctx: Context) = try { //debug
239239
val sym = tree.symbol
240+
241+
def enclosure = ctx.owner.enclosingMethod.symbol
242+
240243
def narrowTo(thisClass: ClassSymbol) = {
241-
val enclClass = enclosure.enclosingClass
242-
narrowLiftedOwner(enclosure,
244+
val enclMethod = enclosure
245+
val enclClass = enclMethod.enclosingClass
246+
narrowLiftedOwner(enclMethod,
243247
if (enclClass.isContainedIn(thisClass)) thisClass
244248
else enclClass) // unknown this reference, play it safe and assume the narrowest possible owner
245249
}
250+
246251
tree match {
247252
case tree: Ident =>
248253
if (isLocal(sym)) {
@@ -290,7 +295,7 @@ class LambdaLift extends MiniPhase with IdentityDenotTransformer { thisTransform
290295
liftedDefs(tree.symbol.owner) = new mutable.ListBuffer
291296
case _ =>
292297
}
293-
foldOver(enclosure, tree)
298+
traverseChildren(tree)
294299
} catch { //debug
295300
case ex: Exception =>
296301
println(i"$ex while traversing $tree")
@@ -394,7 +399,7 @@ class LambdaLift extends MiniPhase with IdentityDenotTransformer { thisTransform
394399
}
395400

396401
private def init(implicit ctx: Context) = {
397-
(new CollectDependencies).traverse(NoSymbol, ctx.compilationUnit.tpdTree)
402+
(new CollectDependencies).traverse(ctx.compilationUnit.tpdTree)
398403
computeFreeVars()
399404
computeLiftedOwners()
400405
generateProxies()(ctx.withPhase(thisTransform.next))

0 commit comments

Comments
 (0)