Skip to content

Optimize '(~x) to x to avoid pickling #4210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand All @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/run-with-compiler/quote-nested-2.check
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
val a: quoted.Expr[Int] = <special-ops>.'[Int](4)
<special-ops>.'[Int](a.unary_~)
a
}
2 changes: 1 addition & 1 deletion tests/run-with-compiler/quote-nested-5.check
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
val a: quoted.Expr[Int] = <special-ops>.'[Int](4)
<special-ops>.'[Int](a.unary_~)
a
}
13 changes: 13 additions & 0 deletions tests/run/quote-simple-hole.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}