@@ -12,6 +12,7 @@ import tasty.TreePickler.Hole
1212import MegaPhase .MiniPhase
1313import SymUtils ._
1414import NameKinds ._
15+ import dotty .tools .dotc .ast .tpd .Tree
1516import typer .Implicits .SearchFailureType
1617
1718import scala .collection .mutable
@@ -290,12 +291,25 @@ class ReifyQuotes extends MacroTransformWithImplicits {
290291 }
291292 else {
292293 val (body1, splices) = nested(isQuote = true ).split(body)
294+ pickledQuote(body1, splices, isType).withPos(quote.pos)
295+ }
296+ }
297+
298+ private def pickledQuote (body : Tree , splices : List [Tree ], isType : Boolean )(implicit ctx : Context ) = {
299+ def pickleAsValue [T ](value : T ) =
300+ ref(defn.QuotedExprs_valueExpr ).appliedToType(body.tpe.widen).appliedTo(Literal (Constant (value)))
301+ def pickleAsTasty () = {
293302 val meth =
294- if (isType) ref(defn.Unpickler_unpickleType ).appliedToType(body1 .tpe)
295- else ref(defn.Unpickler_unpickleExpr ).appliedToType(body1 .tpe.widen)
303+ if (isType) ref(defn.Unpickler_unpickleType ).appliedToType(body .tpe)
304+ else ref(defn.Unpickler_unpickleExpr ).appliedToType(body .tpe.widen)
296305 meth.appliedTo(
297- liftList(PickledQuotes .pickleQuote(body1).map(x => Literal (Constant (x))), defn.StringType ),
298- liftList(splices, defn.AnyType )).withPos(quote.pos)
306+ liftList(PickledQuotes .pickleQuote(body).map(x => Literal (Constant (x))), defn.StringType ),
307+ liftList(splices, defn.AnyType ))
308+ }
309+ if (splices.nonEmpty) pickleAsTasty()
310+ else ReifyQuotes .toValue(body) match {
311+ case Some (value) => pickleAsValue(value)
312+ case _ => pickleAsTasty()
299313 }
300314 }
301315
@@ -473,3 +487,12 @@ class ReifyQuotes extends MacroTransformWithImplicits {
473487 }
474488 }
475489}
490+
491+ object ReifyQuotes {
492+ def toValue (tree : Tree ): Option [Any ] = tree match {
493+ case Literal (Constant (c)) => Some (c)
494+ case Block (Nil , e) => toValue(e)
495+ case Inlined (_, Nil , e) => toValue(e)
496+ case _ => None
497+ }
498+ }
0 commit comments