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
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)
}
}