@@ -329,9 +329,14 @@ class ReifyQuotes extends MacroTransformWithImplicits {
329
329
if (isType) ref(defn.typeQuoteMethod).appliedToType(body1.tpe.widen)
330
330
else ref(defn.quoteMethod).appliedToType(body1.tpe.widen).appliedTo(body1)
331
331
}
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)
335
340
}
336
341
}
337
342
@@ -438,7 +443,7 @@ class ReifyQuotes extends MacroTransformWithImplicits {
438
443
}
439
444
440
445
/** 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 = {
442
447
// Check phase consistency and presence of capturer
443
448
level == 1 && ! tree.symbol.is(Inline ) && levelOf.get(tree.symbol).contains(1 ) &&
444
449
capturers.contains(tree.symbol)
@@ -478,7 +483,7 @@ class ReifyQuotes extends MacroTransformWithImplicits {
478
483
splice(ref(splicedType).select(tpnme.UNARY_~ ))
479
484
case tree : Select if tree.symbol.isSplice =>
480
485
splice(tree)
481
- case tree : RefTree if isCaptured(tree) =>
486
+ case tree : RefTree if isCaptured(tree, level ) =>
482
487
val capturer = capturers(tree.symbol)
483
488
splice(capturer(tree).select(if (tree.isTerm) nme.UNARY_~ else tpnme.UNARY_~ ))
484
489
case Block (stats, _) =>
0 commit comments