Skip to content

Commit c89f8fa

Browse files
Merge pull request #4149 from dotty-staging/avoid-pickling-captured-quotes
Avoid pickling '(x) when x is a captured parameter
2 parents 004f3d5 + 741eaec commit c89f8fa

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

Foo.scala

Whitespace-only changes.

compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,14 @@ class ReifyQuotes extends MacroTransformWithImplicits {
329329
if (isType) ref(defn.typeQuoteMethod).appliedToType(body1.tpe.widen)
330330
else ref(defn.quoteMethod).appliedToType(body1.tpe.widen).appliedTo(body1)
331331
}
332-
else {
333-
val (body1, splices) = nested(isQuote = true).split(body)
334-
pickledQuote(body1, splices, isType).withPos(quote.pos)
332+
else body match {
333+
case body: RefTree if isCaptured(body, level + 1) =>
334+
// Optimization: avoid the full conversion when capturing `x`
335+
// in '{ x } to '{ x$1.unary_~ } and go directly to `x$1`
336+
capturers(body.symbol)(body)
337+
case _=>
338+
val (body1, splices) = nested(isQuote = true).split(body)
339+
pickledQuote(body1, splices, isType).withPos(quote.pos)
335340
}
336341
}
337342

@@ -438,7 +443,7 @@ class ReifyQuotes extends MacroTransformWithImplicits {
438443
}
439444

440445
/** Returns true if this tree will be captured by `makeLambda` */
441-
private def isCaptured(tree: RefTree)(implicit ctx: Context): Boolean = {
446+
private def isCaptured(tree: RefTree, level: Int)(implicit ctx: Context): Boolean = {
442447
// Check phase consistency and presence of capturer
443448
level == 1 && !tree.symbol.is(Inline) && levelOf.get(tree.symbol).contains(1) &&
444449
capturers.contains(tree.symbol)
@@ -478,7 +483,7 @@ class ReifyQuotes extends MacroTransformWithImplicits {
478483
splice(ref(splicedType).select(tpnme.UNARY_~))
479484
case tree: Select if tree.symbol.isSplice =>
480485
splice(tree)
481-
case tree: RefTree if isCaptured(tree) =>
486+
case tree: RefTree if isCaptured(tree, level) =>
482487
val capturer = capturers(tree.symbol)
483488
splice(capturer(tree).select(if (tree.isTerm) nme.UNARY_~ else tpnme.UNARY_~))
484489
case Block(stats, _) =>

0 commit comments

Comments
 (0)