Skip to content
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
16 changes: 0 additions & 16 deletions compiler/src/dotty/tools/dotc/ast/tpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -978,22 +978,6 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
}
}

/** A traverser that passes the enclosing class or method as an argument
* to the traverse method.
*/
abstract class EnclosingMethodTraverser extends TreeAccumulator[Symbol] {
def traverse(enclMeth: Symbol, tree: Tree)(implicit ctx: Context): Unit
def apply(enclMeth: Symbol, tree: Tree)(implicit ctx: Context) = {
tree match {
case _: DefTree if tree.symbol.exists =>
traverse(tree.symbol.enclosingMethod, tree)
case _ =>
traverse(enclMeth, tree)
}
enclMeth
}
}

/** A key to be used in a context property that tracks enclosing inlined calls */
private val InlinedCalls = new Property.Key[List[Tree]]

Expand Down
23 changes: 13 additions & 10 deletions compiler/src/dotty/tools/dotc/transform/CapturedVars.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,30 @@ class CapturedVars extends MiniPhase with IdentityDenotTransformer { thisTransfo
myRefInfo
}

private class CollectCaptured(implicit ctx: Context) extends EnclosingMethodTraverser {
private class CollectCaptured extends TreeTraverser {
private val captured = mutable.HashSet[Symbol]()
def traverse(enclMeth: Symbol, tree: Tree)(implicit ctx: Context) = tree match {
def traverse(tree: Tree)(implicit ctx: Context) = tree match {
case id: Ident =>
val sym = id.symbol
if (sym.is(Mutable, butNot = Method) && sym.owner.isTerm && sym.enclosingMethod != enclMeth) {
ctx.log(i"capturing $sym in ${sym.enclosingMethod}, referenced from $enclMeth")
captured += sym
if (sym.is(Mutable, butNot = Method) && sym.owner.isTerm) {
val enclMeth = ctx.owner.enclosingMethod
if (sym.enclosingMethod != enclMeth) {
ctx.log(i"capturing $sym in ${sym.enclosingMethod}, referenced from $enclMeth")
captured += sym
}
}
case _ =>
foldOver(enclMeth, tree)
traverseChildren(tree)
}
def runOver(tree: Tree): collection.Set[Symbol] = {
apply(NoSymbol, tree)
def runOver(tree: Tree)(implicit ctx: Context): collection.Set[Symbol] = {
traverse(tree)
captured
}
}

override def prepareForUnit(tree: Tree)(implicit ctx: Context) = {
val captured = (new CollectCaptured)(ctx.withPhase(thisTransform))
.runOver(ctx.compilationUnit.tpdTree)
val captured = (new CollectCaptured)
.runOver(ctx.compilationUnit.tpdTree)(ctx.withPhase(thisTransform))
new Transform(captured)
}

Expand Down
17 changes: 11 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/LambdaLift.scala
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,20 @@ class LambdaLift extends MiniPhase with IdentityDenotTransformer { thisTransform
if (callee.enclosingClass != caller.enclosingClass) calledFromInner += callee
}

private class CollectDependencies extends EnclosingMethodTraverser {
def traverse(enclosure: Symbol, tree: Tree)(implicit ctx: Context) = try { //debug
private class CollectDependencies extends TreeTraverser {
def traverse(tree: Tree)(implicit ctx: Context) = try { //debug
val sym = tree.symbol

def enclosure = ctx.owner.enclosingMethod.symbol

def narrowTo(thisClass: ClassSymbol) = {
val enclClass = enclosure.enclosingClass
narrowLiftedOwner(enclosure,
val enclMethod = enclosure
val enclClass = enclMethod.enclosingClass
narrowLiftedOwner(enclMethod,
if (enclClass.isContainedIn(thisClass)) thisClass
else enclClass) // unknown this reference, play it safe and assume the narrowest possible owner
}

tree match {
case tree: Ident =>
if (isLocal(sym)) {
Expand Down Expand Up @@ -290,7 +295,7 @@ class LambdaLift extends MiniPhase with IdentityDenotTransformer { thisTransform
liftedDefs(tree.symbol.owner) = new mutable.ListBuffer
case _ =>
}
foldOver(enclosure, tree)
traverseChildren(tree)
} catch { //debug
case ex: Exception =>
println(i"$ex while traversing $tree")
Expand Down Expand Up @@ -394,7 +399,7 @@ class LambdaLift extends MiniPhase with IdentityDenotTransformer { thisTransform
}

private def init(implicit ctx: Context) = {
(new CollectDependencies).traverse(NoSymbol, ctx.compilationUnit.tpdTree)
(new CollectDependencies).traverse(ctx.compilationUnit.tpdTree)
computeFreeVars()
computeLiftedOwners()
generateProxies()(ctx.withPhase(thisTransform.next))
Expand Down