@@ -12,6 +12,7 @@ import tasty.TreePickler.Hole
12
12
import MegaPhase .MiniPhase
13
13
import SymUtils ._
14
14
import NameKinds ._
15
+ import dotty .tools .dotc .ast .tpd .Tree
15
16
import typer .Implicits .SearchFailureType
16
17
17
18
import scala .collection .mutable
@@ -290,12 +291,25 @@ class ReifyQuotes extends MacroTransformWithImplicits {
290
291
}
291
292
else {
292
293
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 () = {
293
302
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)
296
305
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()
299
313
}
300
314
}
301
315
@@ -473,4 +487,13 @@ class ReifyQuotes extends MacroTransformWithImplicits {
473
487
}
474
488
}
475
489
}
476
- }
490
+ }
491
+
492
+ object ReifyQuotes {
493
+ def toValue (tree : Tree ): Option [Any ] = tree match {
494
+ case Literal (Constant (c)) => Some (c)
495
+ case Block (Nil , e) => toValue(e)
496
+ case Inlined (_, Nil , e) => toValue(e)
497
+ case _ => None
498
+ }
499
+ }
0 commit comments