|
1 | 1 | package dotty.tools.dotc.core.quoted |
2 | 2 |
|
3 | 3 | import dotty.tools.dotc.ast.Trees._ |
4 | | -import dotty.tools.dotc.ast.tpd |
| 4 | +import dotty.tools.dotc.ast.{tpd, untpd} |
5 | 5 | import dotty.tools.dotc.config.Printers._ |
6 | 6 | import dotty.tools.dotc.core.Constants.Constant |
7 | 7 | import dotty.tools.dotc.core.Contexts._ |
@@ -35,22 +35,28 @@ object PickledQuotes { |
35 | 35 | private def unpickleQuote(expr: quoted.TastyQuoted)(implicit ctx: Context): Tree = { |
36 | 36 | val tastyBytes = TastyString.stringToTasty(expr.tasty) |
37 | 37 | val unpickled = unpickle(tastyBytes, expr.args) |
38 | | - unpickled match { // Expects `package _root_ { val ': Any = <tree> }` |
| 38 | + unpickled match { |
39 | 39 | case PackageDef(_, (vdef: ValDef) :: Nil) => vdef.rhs |
| 40 | + case PackageDef(_, (tdef: TypeDef) :: Nil) => tdef.rhs |
40 | 41 | } |
41 | 42 | } |
42 | 43 |
|
43 | | - /** Encapsulate the tree in a top level `val` |
| 44 | + /** Encapsulate the tree in a top level `val` or `type` |
44 | 45 | * `<tree>` ==> `package _root_ { val ': Any = <tree> }` |
45 | | - * |
46 | | - * Note: Trees for types are also encapsulated this way to preserve the holes in the tree. |
47 | | - * Encapsulating the type of the tree in a `type ' = <tree.tpe>` can potentially |
48 | | - * contain references to the outer environment. |
| 46 | + * or |
| 47 | + * `<type tree>` ==> `package _root_ { type ' = <tree tree> }` |
49 | 48 | */ |
50 | 49 | private def encapsulateQuote(tree: Tree)(implicit ctx: Context): Tree = { |
51 | | - val sym = ctx.newSymbol(ctx.owner, "'".toTermName, Synthetic, defn.AnyType, coord = tree.pos) |
52 | | - val quotedVal = ValDef(sym, tree).withPos(tree.pos) |
53 | | - PackageDef(ref(defn.RootPackage).asInstanceOf[Ident], quotedVal :: Nil).withPos(tree.pos) |
| 50 | + def encapsulatedTerm = { |
| 51 | + val sym = ctx.newSymbol(ctx.owner, "'".toTermName, Synthetic, defn.AnyType, coord = tree.pos) |
| 52 | + ValDef(sym, tree).withPos(tree.pos) |
| 53 | + } |
| 54 | + |
| 55 | + def encapsulatedType = |
| 56 | + untpd.TypeDef("'".toTypeName, tree).withPos(tree.pos).withType(defn.AnyType) |
| 57 | + |
| 58 | + val quoted = if (tree.isTerm) encapsulatedTerm else encapsulatedType |
| 59 | + PackageDef(ref(defn.RootPackage).asInstanceOf[Ident], quoted :: Nil).withPos(tree.pos) |
54 | 60 | } |
55 | 61 |
|
56 | 62 | // TASTY picklingtests/pos/quoteTest.scala |
|
0 commit comments