Skip to content

Create single argument for each capture #4148

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
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
31 changes: 18 additions & 13 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -402,16 +402,21 @@ class ReifyQuotes extends MacroTransformWithImplicits {
def body(arg: Tree)(implicit ctx: Context): Tree = {
var i = 0
transformWithCapturer(tree)(
(captured: mutable.ListBuffer[Tree]) => (tree: RefTree) => {
val argTpe =
if (tree.isTerm) defn.QuotedExprType.appliedTo(tree.tpe.widen)
else defn.QuotedTypeType.appliedTo(defn.AnyType)
val selectArg = arg.select(nme.apply).appliedTo(Literal(Constant(i))).asInstance(argTpe)
val capturedArg = SyntheticValDef(UniqueName.fresh(tree.name.toTermName).toTermName, selectArg)
i += 1
embedded += tree
captured += capturedArg
ref(capturedArg.symbol)
(captured: mutable.Map[Symbol, Tree]) => {
(tree: RefTree) => {
def newCapture = {
val argTpe =
if (tree.isTerm) defn.QuotedExprType.appliedTo(tree.tpe.widen)
else defn.QuotedTypeType.appliedTo(defn.AnyType)
val selectArg = arg.select(nme.apply).appliedTo(Literal(Constant(i))).asInstance(argTpe)
val capturedArg = SyntheticValDef(UniqueName.fresh(tree.name.toTermName).toTermName, selectArg)
i += 1
embedded += tree
captured.put(tree.symbol, capturedArg)
capturedArg
}
ref(captured.getOrElseUpdate(tree.symbol, newCapture).symbol)
}
}
)
}
Expand All @@ -423,13 +428,13 @@ class ReifyQuotes extends MacroTransformWithImplicits {
}

private def transformWithCapturer(tree: Tree)(
capturer: mutable.ListBuffer[Tree] => RefTree => Tree)(implicit ctx: Context): Tree = {
val captured = new mutable.ListBuffer[Tree]
capturer: mutable.Map[Symbol, Tree] => RefTree => Tree)(implicit ctx: Context): Tree = {
val captured = mutable.LinkedHashMap.empty[Symbol, Tree]
val captured2 = capturer(captured)
outer.enteredSyms.foreach(s => capturers.put(s, captured2))
val tree2 = transform(tree)
capturers --= outer.enteredSyms
seq(captured.result(), tree2)
seq(captured.result().valuesIterator.toList, tree2)
}

/** Returns true if this tree will be captured by `makeLambda` */
Expand Down
5 changes: 5 additions & 0 deletions tests/run-with-compiler/quote-two-captured-ref.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1
{
val x: Int = 1
println(x.+(x))
}
18 changes: 18 additions & 0 deletions tests/run-with-compiler/quote-two-captured-ref.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import quoted._
import dotty.tools.dotc.quoted.Toolbox._

object Test {
def main(args: Array[String]): Unit = {
val q = '{
val x = 1
println(~{
println(1)
val a = '(x)
val b = '(x)
'{ ~a + ~b }
})
}

println(q.show)
}
}