@@ -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,28 @@ 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.Unpickler_unpickleValueExpr )
301
+ .appliedToType(body.tpe.widen)
302
+ .appliedTo(Literal (Constant (value)))
303
+ }
304
+ def pickleAsTasty () = {
293
305
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)
296
308
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()
299
316
}
300
317
}
301
318
@@ -473,4 +490,13 @@ class ReifyQuotes extends MacroTransformWithImplicits {
473
490
}
474
491
}
475
492
}
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