Skip to content

Fix owner of Expr/Type represented with trees #5344

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
Nov 1, 2018
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
21 changes: 19 additions & 2 deletions compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object PickledQuotes {
case value: Class[_] => ref(defn.Predef_classOf).appliedToType(classToType(value))
case value => Literal(Constant(value))
}
case expr: TastyTreeExpr[Tree] @unchecked => expr.tree
case expr: TastyTreeExpr[Tree] @unchecked => healOwner(expr.tree)
case expr: FunctionAppliedTo[_, _] =>
functionAppliedTo(quotedExprToTree(expr.f), quotedExprToTree(expr.x))
}
Expand All @@ -55,7 +55,7 @@ object PickledQuotes {
def quotedTypeToTree(expr: quoted.Type[_])(implicit ctx: Context): Tree = expr match {
case expr: TastyType[_] => unpickleType(expr)
case expr: TaggedType[_] => classTagToTypeTree(expr.ct)
case expr: TreeType[Tree] @unchecked => expr.typeTree
case expr: TreeType[Tree] @unchecked => healOwner(expr.typeTree)
}

/** Unpickle the tree contained in the TastyExpr */
Expand Down Expand Up @@ -170,4 +170,21 @@ object PickledQuotes {
}
} else ctx.getClassIfDefined(clazz.getCanonicalName).typeRef
}

/** Make sure that the owner of this tree is `ctx.owner` */
private def healOwner(tree: Tree)(implicit ctx: Context): Tree = {
val getCurrentOwner = new TreeAccumulator[Option[Symbol]] {
def apply(x: Option[Symbol], tree: tpd.Tree)(implicit ctx: Context): Option[Symbol] = {
if (x.isDefined) x
else tree match {
case tree: DefTree => Some(tree.symbol.owner)
case _ => foldOver(x, tree)
}
}
}
getCurrentOwner(None, tree) match {
case Some(owner) if owner != ctx.owner => tree.changeOwner(owner, ctx.owner)
case _ => tree
}
}
}
1 change: 1 addition & 0 deletions compiler/test/dotc/run-test-pickling.blacklist
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ mixins1
outerPatternMatch
paramForwarding_separate
quote-and-splice
quote-change-owner
quote-force
quote-indexed-map-by-name
quote-sep-comp
Expand Down
1 change: 1 addition & 0 deletions tests/run/quote-change-owner.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar
8 changes: 8 additions & 0 deletions tests/run/quote-change-owner/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.quoted._
object Macros {
inline def assert2(expr: => Boolean): Unit = ~ assertImpl('(expr))
def assertImpl(expr: Expr[Boolean]) = '{
def foo(): Unit = ~expr
foo()
}
}
12 changes: 12 additions & 0 deletions tests/run/quote-change-owner/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Macros._
object Test {
def main(args: Array[String]): Unit = {
assert2 {
def bar(): Boolean = {
println("bar")
false
}
bar()
}
}
}