@@ -19,11 +19,13 @@ import dotty.tools.dotc.tastyreflect.ReflectionImpl
19
19
import scala .internal .quoted ._
20
20
import scala .reflect .ClassTag
21
21
22
+ import scala .runtime .quoted .Unpickler ._
23
+
22
24
object PickledQuotes {
23
25
import tpd ._
24
26
25
27
/** Pickle the tree of the quote into strings */
26
- def pickleQuote (tree : Tree )(implicit ctx : Context ): scala.runtime.quoted. Unpickler . Pickled = {
28
+ def pickleQuote (tree : Tree )(implicit ctx : Context ): PickledQuote = {
27
29
if (ctx.reporter.hasErrors) Nil
28
30
else {
29
31
assert(! tree.isInstanceOf [Hole ]) // Should not be pickled as it represents `'{$x}` which should be optimized to `x`
@@ -33,33 +35,12 @@ object PickledQuotes {
33
35
}
34
36
35
37
/** Transform the expression into its fully spliced Tree */
36
- def quotedExprToTree [T ](expr : quoted.Expr [T ])(implicit ctx : Context ): Tree = expr match {
37
- case expr : TastyExpr [_] =>
38
- val unpickled = unpickleExpr(expr)
39
- /** Force unpickling of the tree, removes the spliced type `@quotedTypeTag type` definitions and dealiases references to `@quotedTypeTag type` */
40
- val forceAndCleanArtefacts = new TreeMap {
41
- override def transform (tree : tpd.Tree )(implicit ctx : Context ): tpd.Tree = tree match {
42
- case Block (stat :: rest, expr1) if stat.symbol.hasAnnotation(defn.InternalQuoted_QuoteTypeTagAnnot ) =>
43
- assert(rest.forall { case tdef : TypeDef => tdef.symbol.hasAnnotation(defn.InternalQuoted_QuoteTypeTagAnnot ) })
44
- transform(expr1)
45
- case tree => super .transform(tree).withType(dealiasTypeTags(tree.tpe))
46
- }
47
- }
48
- forceAndCleanArtefacts.transform(unpickled)
49
- case expr : TastyTreeExpr [Tree ] @ unchecked => healOwner(expr.tree)
50
- }
38
+ def quotedExprToTree [T ](expr : quoted.Expr [T ])(implicit ctx : Context ): Tree =
39
+ healOwner(expr.asInstanceOf [TastyTreeExpr [Tree ]].tree)
51
40
52
41
/** Transform the expression into its fully spliced TypeTree */
53
- def quotedTypeToTree (expr : quoted.Type [_])(implicit ctx : Context ): Tree = expr match {
54
- case expr : TastyType [_] =>
55
- unpickleType(expr) match {
56
- case Block (aliases, tpt) =>
57
- // `@quoteTypeTag type` aliasses are not required after unpickling
58
- tpt
59
- case tpt => tpt
60
- }
61
- case expr : TreeType [Tree ] @ unchecked => healOwner(expr.typeTree)
62
- }
42
+ def quotedTypeToTree (expr : quoted.Type [_])(implicit ctx : Context ): Tree =
43
+ healOwner(expr.asInstanceOf [TreeType [Tree ]].typeTree)
63
44
64
45
private def dealiasTypeTags (tp : Type )(implicit ctx : Context ): Type = new TypeMap () {
65
46
override def apply (tp : Type ): Type = {
@@ -72,15 +53,31 @@ object PickledQuotes {
72
53
}.apply(tp)
73
54
74
55
/** Unpickle the tree contained in the TastyExpr */
75
- private def unpickleExpr (expr : TastyExpr [_])(implicit ctx : Context ): Tree = {
76
- val tastyBytes = TastyString .unpickle(expr.tasty)
77
- unpickle(tastyBytes, expr.args, isType = false )(ctx.addMode(Mode .ReadPositions ))
56
+ def unpickleExpr (tasty : PickledQuote , args : PickledExprArgs )(implicit ctx : Context ): Tree = {
57
+ val tastyBytes = TastyString .unpickle(tasty)
58
+ val unpickled = unpickle(tastyBytes, args, isType = false )(ctx.addMode(Mode .ReadPositions ))
59
+ /** Force unpickling of the tree, removes the spliced type `@quotedTypeTag type` definitions and dealiases references to `@quotedTypeTag type` */
60
+ val forceAndCleanArtefacts = new TreeMap {
61
+ override def transform (tree : tpd.Tree )(implicit ctx : Context ): tpd.Tree = tree match {
62
+ case Block (stat :: rest, expr1) if stat.symbol.hasAnnotation(defn.InternalQuoted_QuoteTypeTagAnnot ) =>
63
+ assert(rest.forall { case tdef : TypeDef => tdef.symbol.hasAnnotation(defn.InternalQuoted_QuoteTypeTagAnnot ) })
64
+ transform(expr1)
65
+ case tree => super .transform(tree).withType(dealiasTypeTags(tree.tpe))
66
+ }
67
+ }
68
+ forceAndCleanArtefacts.transform(unpickled)
78
69
}
79
70
80
71
/** Unpickle the tree contained in the TastyType */
81
- private def unpickleType (ttpe : TastyType [_])(implicit ctx : Context ): Tree = {
82
- val tastyBytes = TastyString .unpickle(ttpe.tasty)
83
- unpickle(tastyBytes, ttpe.args, isType = true )(ctx.addMode(Mode .ReadPositions ))
72
+ def unpickleType (tasty : PickledQuote , args : PickledTypeArgs )(implicit ctx : Context ): Tree = {
73
+ val tastyBytes = TastyString .unpickle(tasty)
74
+ val unpickled = unpickle(tastyBytes, args, isType = true )(ctx.addMode(Mode .ReadPositions ))
75
+ unpickled match {
76
+ case Block (aliases, tpt) =>
77
+ // `@quoteTypeTag type` aliasses are not required after unpickling
78
+ tpt
79
+ case tpt => tpt
80
+ }
84
81
}
85
82
86
83
// TASTY picklingtests/pos/quoteTest.scala
0 commit comments