Skip to content

Fix #4380: Revert optimization that breaks owners #4384

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 2 commits into from
Apr 27, 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
8 changes: 2 additions & 6 deletions compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1142,14 +1142,10 @@ class TreeUnpickler(reader: TastyReader,
val splice = splices(idx)
val reifiedArgs = args.map(arg => if (arg.isTerm) new TreeExpr(arg) else new TreeType(arg))
if (isType) {
val quotedType =
if (reifiedArgs.isEmpty) splice.asInstanceOf[quoted.Type[_]]
else splice.asInstanceOf[Seq[Any] => quoted.Type[_]](reifiedArgs)
val quotedType = splice.asInstanceOf[Seq[Any] => quoted.Type[_]](reifiedArgs)
PickledQuotes.quotedTypeToTree(quotedType)
} else {
val quotedExpr =
if (reifiedArgs.isEmpty) splice.asInstanceOf[quoted.Expr[_]]
else splice.asInstanceOf[Seq[Any] => quoted.Expr[_]](reifiedArgs)
val quotedExpr = splice.asInstanceOf[Seq[Any] => quoted.Expr[_]](reifiedArgs)
PickledQuotes.quotedExprToTree(quotedExpr)
}
}
Expand Down
40 changes: 15 additions & 25 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import dotty.tools.dotc.core.quoted._
* val x2 = ???
* ...
* ~{ ... '{ ... x1 ... x2 ...} ... }
* ~{ ... /* no references to xi */ ... }
* ...
* }
* ```
Expand All @@ -45,7 +44,6 @@ import dotty.tools.dotc.core.quoted._
* val x2 = ???
* ...
* Hole(0 | x1, x2)
* Hole(1 | )
* ...
* ]],
* List(
Expand All @@ -54,8 +52,7 @@ import dotty.tools.dotc.core.quoted._
* val x2$1 = args(1).asInstanceOf[Expr[T]] // can be asInstanceOf[Type[T]]
* ...
* { ... '{ ... x1$1.unary_~ ... x2$1.unary_~ ...} ... }
* },
* { ... /* no references to xi */ ... } // optimized to not create lambda
* }
* )
* )
* ```
Expand All @@ -69,12 +66,12 @@ import dotty.tools.dotc.core.quoted._
* ```
* to
* ```
* inline def foo[T1, ...](inline x1: X, ..., y1: Y, ....): Object = { (args: Seq[Any]) => {
* inline def foo[T1, ...](inline x1: X, ..., y1: Y, ....): Seq[Any] => Object = { (args: Seq[Any]) => {
* val T1$1 = args(0).asInstanceOf[Type[T1]]
* ...
* val x1$1 = args(..).asInstanceOf[X]
* val x1$1 = args(0).asInstanceOf[X]
* ...
* val y1$1 = args(..).asInstanceOf[Expr[Y]]
* val y1$1 = args(1).asInstanceOf[Expr[Y]]
* ...
* { ... T1$1.unary_~ ... x ... '(y1$1.unary_~) ... }
* }
Expand Down Expand Up @@ -448,8 +445,6 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
* val y$1 = args(1).asInstanceOf[Expr[Any]] // or .asInstanceOf[Type[Any]]
* { ... '{ ... x$1.unary_~ ... y$1.unary_~ ... } ... }
* }
* or if the spliced subexpression has no captures it will be transformed to
* { ... '{ ... x$1.unary_~ ... y$1.unary_~ ... } ... }
*
* See: `capture`
*
Expand All @@ -462,19 +457,6 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
* }
*/
private def makeLambda(tree: Tree)(implicit ctx: Context): Tree = {
var treeWithoutCaptures: Tree = null
def transformWithCapturer(tree: Tree)(capturer: mutable.Map[Symbol, Tree] => Tree => Tree)(implicit ctx: Context): Tree = {
val captured = mutable.LinkedHashMap.empty[Symbol, Tree]
val captured2 = capturer(captured)
outer.enteredSyms.foreach(s => capturers.put(s, captured2))
if (ctx.owner.owner.is(Macro))
outer.enteredSyms.reverse.foreach(s => captured2(ref(s)))
val tree2 = transform(tree)
capturers --= outer.enteredSyms
if (captured.isEmpty)
treeWithoutCaptures = tree2
seq(captured.result().valuesIterator.toList, tree2)
}
def body(arg: Tree)(implicit ctx: Context): Tree = {
var i = 0
transformWithCapturer(tree)(
Expand Down Expand Up @@ -502,10 +484,18 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
val lambdaOwner = ctx.owner.ownersIterator.find(o => levelOf.getOrElse(o, level) == level).get
val tpe = MethodType(defn.SeqType.appliedTo(defn.AnyType) :: Nil, tree.tpe.widen)
val meth = ctx.newSymbol(lambdaOwner, UniqueName.fresh(nme.ANON_FUN), Synthetic | Method, tpe)
val closure = Closure(meth, tss => body(tss.head.head)(ctx.withOwner(meth)).changeOwner(ctx.owner, meth))
Closure(meth, tss => body(tss.head.head)(ctx.withOwner(meth)).changeOwner(ctx.owner, meth))
}

if (treeWithoutCaptures == null || ctx.owner.is(Macro)) closure
else treeWithoutCaptures
private def transformWithCapturer(tree: Tree)(capturer: mutable.Map[Symbol, Tree] => Tree => Tree)(implicit ctx: Context): Tree = {
val captured = mutable.LinkedHashMap.empty[Symbol, Tree]
val captured2 = capturer(captured)
outer.enteredSyms.foreach(s => capturers.put(s, captured2))
if (ctx.owner.owner.is(Macro))
outer.enteredSyms.reverse.foreach(s => captured2(ref(s)))
val tree2 = transform(tree)
capturers --= outer.enteredSyms
seq(captured.result().valuesIterator.toList, tree2)
}

/** Returns true if this tree will be captured by `makeLambda` */
Expand Down
23 changes: 23 additions & 0 deletions tests/pos/i4380a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import scala.quoted._

object Test {

trait Producer[A] { self =>
def step(k: (A => Expr[Unit])): Expr[Unit]
}

trait Foo[A]
case class Bar[A, B](producer: Producer[B], nestedf: B => Expr[Unit]) extends Foo[A]

def meth[A](stream: Foo[Expr[A]]): Producer[Expr[A]] = {
stream match {
case Bar(producer, nestedf) => {
new Producer[Expr[A]] {
def step(k: Expr[A] => Expr[Unit]): Expr[Unit] = '{
val adv: Unit => Unit = { _ => ~producer.step((el) => nestedf(el))}
}
}
}
}
}
}
8 changes: 8 additions & 0 deletions tests/pos/i4380b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.quoted._

object Test {
def step(k: (String => Expr[Unit])): Expr[Unit] = '()
def meth(): Unit = '{
(i: Int) => ~step(el => '() )
}
}