Skip to content

Commit 741eaec

Browse files
committed
Avoid pickling '(x) when x is a captured parameter
1 parent 0eb0423 commit 741eaec

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

@@ -433,7 +438,7 @@ class ReifyQuotes extends MacroTransformWithImplicits {
433438
}
434439

435440
/** Returns true if this tree will be captured by `makeLambda` */
436-
private def isCaptured(tree: RefTree)(implicit ctx: Context): Boolean = {
441+
private def isCaptured(tree: RefTree, level: Int)(implicit ctx: Context): Boolean = {
437442
// Check phase consistency and presence of capturer
438443
level == 1 && !tree.symbol.is(Inline) && levelOf.get(tree.symbol).contains(1) &&
439444
capturers.contains(tree.symbol)
@@ -473,7 +478,7 @@ class ReifyQuotes extends MacroTransformWithImplicits {
473478
splice(ref(splicedType).select(tpnme.UNARY_~))
474479
case tree: Select if tree.symbol.isSplice =>
475480
splice(tree)
476-
case tree: RefTree if isCaptured(tree) =>
481+
case tree: RefTree if isCaptured(tree, level) =>
477482
val capturer = capturers(tree.symbol)
478483
splice(capturer(tree).select(if (tree.isTerm) nme.UNARY_~ else tpnme.UNARY_~))
479484
case Block(stats, _) =>

0 commit comments

Comments
 (0)