diff --git a/compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala b/compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala index 74c8aa1d2d3b..7f6341b70b44 100644 --- a/compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala +++ b/compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala @@ -11,6 +11,7 @@ import dotty.tools.dotc.core.StdNames._ import dotty.tools.dotc.core.NameKinds import dotty.tools.dotc.core.Symbols._ import dotty.tools.dotc.core.Types.Type +import dotty.tools.dotc.core.tasty.TreePickler.Hole import dotty.tools.dotc.core.tasty.{TastyPickler, TastyPrinter, TastyString} import scala.quoted.Types._ @@ -25,6 +26,7 @@ object PickledQuotes { def pickleQuote(tree: Tree)(implicit ctx: Context): scala.runtime.quoted.Unpickler.Pickled = { if (ctx.reporter.hasErrors) Nil else { + assert(!tree.isInstanceOf[Hole]) // Should not be pickled as it represents `'(~x)` which should be optimized to `x` val encapsulated = encapsulateQuote(tree) val pickled = pickle(encapsulated) TastyString.pickle(pickled) diff --git a/compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala b/compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala index a4844b9d3ea7..0f8276eefbfd 100644 --- a/compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala +++ b/compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala @@ -348,7 +348,12 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer { */ private def quotation(body: Tree, quote: Tree)(implicit ctx: Context): Tree = { val isType = quote.symbol eq defn.typeQuoteMethod - if (level > 0) { + if (body.symbol.isSplice) { + // simplify `'(~x)` to `x` and then transform it + val Select(splice, _) = body + transform(splice) + } + else if (level > 0) { val body1 = nested(isQuote = true).transform(body) // Keep quotes as trees to reduce pickled size and have a Expr.show without pickled quotes if (isType) ref(defn.typeQuoteMethod).appliedToType(body1.tpe.widen) diff --git a/tests/run-with-compiler/quote-nested-2.check b/tests/run-with-compiler/quote-nested-2.check index 5e2472b8ba28..aea924902147 100644 --- a/tests/run-with-compiler/quote-nested-2.check +++ b/tests/run-with-compiler/quote-nested-2.check @@ -1,4 +1,4 @@ { val a: quoted.Expr[Int] = .'[Int](4) - .'[Int](a.unary_~) + a } diff --git a/tests/run-with-compiler/quote-nested-5.check b/tests/run-with-compiler/quote-nested-5.check index 5e2472b8ba28..aea924902147 100644 --- a/tests/run-with-compiler/quote-nested-5.check +++ b/tests/run-with-compiler/quote-nested-5.check @@ -1,4 +1,4 @@ { val a: quoted.Expr[Int] = .'[Int](4) - .'[Int](a.unary_~) + a } diff --git a/tests/run/quote-simple-hole.scala b/tests/run/quote-simple-hole.scala new file mode 100644 index 000000000000..2fcb905abc3e --- /dev/null +++ b/tests/run/quote-simple-hole.scala @@ -0,0 +1,13 @@ +object Test { + def main(args: Array[String]): Unit = { + val x = '(0) + val y = '(~x) + val z = '(~('(~y))) + assert(x eq y) + assert(x eq z) + + val i = '[Int] + val j = '[~i] + assert(i eq j) + } +}