Skip to content

Commit 2d40614

Browse files
committed
Optimize '(~x) to x to avoid pickling
1 parent 1183d0d commit 2d40614

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
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
@@ -10,6 +10,7 @@ import dotty.tools.dotc.core.Flags._
1010
import dotty.tools.dotc.core.StdNames._
1111
import dotty.tools.dotc.core.NameKinds
1212
import dotty.tools.dotc.core.Symbols._
13+
import dotty.tools.dotc.core.tasty.TreePickler.Hole
1314
import dotty.tools.dotc.core.tasty.{TastyPickler, TastyPrinter, TastyString}
1415

1516
import scala.quoted.Types._
@@ -24,6 +25,7 @@ object PickledQuotes {
2425
def pickleQuote(tree: Tree)(implicit ctx: Context): scala.runtime.quoted.Unpickler.Pickled = {
2526
if (ctx.reporter.hasErrors) Nil
2627
else {
28+
assert(!tree.isInstanceOf[Hole]) // Should not be pickled as it represents `'(~x)` which should be optimized to `x`
2729
val encapsulated = encapsulateQuote(tree)
2830
val pickled = pickle(encapsulated)
2931
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)

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)