-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Correct owner chains when mixing byname implicits and inlining #5767
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -730,6 +730,27 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo { | |
if (from == to) tree else loop(from, Nil, to :: Nil) | ||
} | ||
|
||
/** | ||
* Set the owner of every definition in this tree which is not itself contained in this | ||
* tree to be `newowner` | ||
*/ | ||
def changeNonLocalOwners(newowner: Symbol)(implicit ctx: Context): Tree = { | ||
val localOwners = mutable.HashSet[Symbol]() | ||
val traverser = new TreeTraverser { | ||
|
||
def traverse(tree: Tree)(implicit ctx: Context): Unit = { | ||
tree match { | ||
case _: DefTree => if(tree.symbol ne NoSymbol) localOwners += tree.symbol | ||
case _ => traverseChildren(tree) | ||
} | ||
} | ||
} | ||
traverser.traverse(tree) | ||
val nonLocalOwners = localOwners.filter(sym => !localOwners.contains(sym.owner)).toList.map(_.owner) | ||
val newOwners = nonLocalOwners.map(_ => newowner) | ||
new TreeTypeMap(oldOwners = nonLocalOwners, newOwners = newOwners).apply(tree) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be simplified further? def changeNonLocalOwners(newOwner: Symbol)(implicit ctx: Context): Tree = {
val localOwnerAcc = new TreeAccumulator[List[Symbol]] {
def apply(ss: List[Symbol], tree: Tree)(implicit ctx: Context) = tree match {
case tree: DefTree =>
if (tree.symbol.exists) tree.symbol :: ss
else ss
case _ =>
foldOver(ss, tree)
}
}
}
val nonLocalOwners = localOwnerAcc(Nil, tree).map(_.owner).distinct
val newOwners = nonLocalOwners.map(_ => newOwner)
new TreeTypeMap(oldOwners = nonLocalOwners, newOwners = newOwners).apply(tree)
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the idea of using an accumulator is good, but why the detour via lists? Let's use an accumulator over an immutable set instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated: def changeNonLocalOwners(newOwner: Symbol)(implicit ctx: Context): Tree = {
val ownerAcc = new TreeAccumulator[Set[Symbol]] {
def apply(ss: Set[Symbol], tree: Tree)(implicit ctx: Context) = tree match {
case tree: DefTree =>
if (tree.symbol.exists) ss + tree.symbol.owner
else ss
case _ =>
foldOver(ss, tree)
}
}
}
val owners = ownerAcc(Set.empty, tree)
val newOwners = List.fill(owners.size)(newOwner)
new TreeTypeMap(oldOwners = owners.toList, newOwners = newOwners).apply(tree)
} |
||
|
||
/** After phase `trans`, set the owner of every definition in this tree that was formerly | ||
* owner by `from` to `to`. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
object Test1 { | ||
trait Foo { def next: Foo } | ||
|
||
inline implicit def foo(implicit loop: => Foo): Foo = new Foo { def next = loop } | ||
|
||
def summon(implicit f: Foo) = () | ||
summon | ||
} | ||
|
||
object Test2 { | ||
trait Foo { def next: Bar } | ||
trait Bar { def next: Foo } | ||
|
||
implicit def foo(implicit loop: => Bar): Foo = new Foo { def next = loop } | ||
inline implicit def bar(implicit loop: Foo): Bar = new Bar { def next = loop } | ||
|
||
def summon(implicit f: Foo) = () | ||
summon | ||
} | ||
|
||
object Test3 { | ||
trait Foo { def next: Bar } | ||
trait Bar { def next: Baz } | ||
trait Baz { def next: Foo } | ||
|
||
implicit def foo(implicit loop: Bar): Foo = new Foo { def next = loop } | ||
inline implicit def bar(implicit loop: Baz): Bar = new Bar { def next = loop } | ||
inline implicit def baz(implicit loop: => Foo): Baz = new Baz { def next = loop } | ||
|
||
def summon(implicit f: Foo) = () | ||
summon | ||
} |
Uh oh!
There was an error while loading. Please reload this page.