Skip to content

Commit 4bc6c65

Browse files
Merge pull request #4210 from dotty-staging/optimize-simple-quote-splice
Optimize `'(~x)` to `x` to avoid pickling
2 parents 431dc90 + 5b54065 commit 4bc6c65

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import dotty.tools.dotc.core.StdNames._
1111
import dotty.tools.dotc.core.NameKinds
1212
import dotty.tools.dotc.core.Symbols._
1313
import dotty.tools.dotc.core.Types.Type
14+
import dotty.tools.dotc.core.tasty.TreePickler.Hole
1415
import dotty.tools.dotc.core.tasty.{TastyPickler, TastyPrinter, TastyString}
1516

1617
import scala.quoted.Types._
@@ -25,6 +26,7 @@ object PickledQuotes {
2526
def pickleQuote(tree: Tree)(implicit ctx: Context): scala.runtime.quoted.Unpickler.Pickled = {
2627
if (ctx.reporter.hasErrors) Nil
2728
else {
29+
assert(!tree.isInstanceOf[Hole]) // Should not be pickled as it represents `'(~x)` which should be optimized to `x`
2830
val encapsulated = encapsulateQuote(tree)
2931
val pickled = pickle(encapsulated)
3032
TastyString.pickle(pickled)

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,12 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
348348
*/
349349
private def quotation(body: Tree, quote: Tree)(implicit ctx: Context): Tree = {
350350
val isType = quote.symbol eq defn.typeQuoteMethod
351-
if (level > 0) {
351+
if (body.symbol.isSplice) {
352+
// simplify `'(~x)` to `x` and then transform it
353+
val Select(splice, _) = body
354+
transform(splice)
355+
}
356+
else if (level > 0) {
352357
val body1 = nested(isQuote = true).transform(body)
353358
// Keep quotes as trees to reduce pickled size and have a Expr.show without pickled quotes
354359
if (isType) ref(defn.typeQuoteMethod).appliedToType(body1.tpe.widen)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
val a: quoted.Expr[Int] = <special-ops>.'[Int](4)
3-
<special-ops>.'[Int](a.unary_~)
3+
a
44
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
val a: quoted.Expr[Int] = <special-ops>.'[Int](4)
3-
<special-ops>.'[Int](a.unary_~)
3+
a
44
}

tests/run/quote-simple-hole.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
val x = '(0)
4+
val y = '(~x)
5+
val z = '(~('(~y)))
6+
assert(x eq y)
7+
assert(x eq z)
8+
9+
val i = '[Int]
10+
val j = '[~i]
11+
assert(i eq j)
12+
}
13+
}

0 commit comments

Comments
 (0)