@@ -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,28 @@ 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.Unpickler_unpickleValueExpr )
301+ .appliedToType(body.tpe.widen)
302+ .appliedTo(Literal (Constant (value)))
303+ }
304+ def pickleAsTasty () = {
293305 val meth =
294- if (isType) ref(defn.Unpickler_unpickleType ).appliedToType(body1 .tpe)
295- else ref(defn.Unpickler_unpickleExpr ).appliedToType(body1 .tpe.widen)
306+ if (isType) ref(defn.Unpickler_unpickleType ).appliedToType(body .tpe)
307+ else ref(defn.Unpickler_unpickleExpr ).appliedToType(body .tpe.widen)
296308 meth.appliedTo(
297- liftList(PickledQuotes .pickleQuote(body1).map(x => Literal (Constant (x))), defn.StringType ),
298- liftList(splices, defn.AnyType )).withPos(quote.pos)
309+ liftList(PickledQuotes .pickleQuote(body).map(x => Literal (Constant (x))), defn.StringType ),
310+ liftList(splices, defn.AnyType ))
311+ }
312+ if (splices.nonEmpty) pickleAsTasty()
313+ else ReifyQuotes .toValue(body) match {
314+ case Some (value) => pickleAsValue(value)
315+ case _ => pickleAsTasty()
299316 }
300317 }
301318
@@ -473,4 +490,13 @@ class ReifyQuotes extends MacroTransformWithImplicits {
473490 }
474491 }
475492 }
476- }
493+ }
494+
495+ object ReifyQuotes {
496+ def toValue (tree : Tree ): Option [Any ] = tree match {
497+ case Literal (Constant (c)) => Some (c)
498+ case Block (Nil , e) => toValue(e)
499+ case Inlined (_, Nil , e) => toValue(e)
500+ case _ => None
501+ }
502+ }
0 commit comments