Skip to content

Commit 82d36bd

Browse files
committed
Encapsulate quoted types in top level type
1 parent 8b94c86 commit 82d36bd

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dotty.tools.dotc.core.quoted
22

33
import dotty.tools.dotc.ast.Trees._
4-
import dotty.tools.dotc.ast.tpd
4+
import dotty.tools.dotc.ast.{tpd, untpd}
55
import dotty.tools.dotc.config.Printers._
66
import dotty.tools.dotc.core.Constants.Constant
77
import dotty.tools.dotc.core.Contexts._
@@ -35,22 +35,28 @@ object PickledQuotes {
3535
private def unpickleQuote(expr: quoted.TastyQuoted)(implicit ctx: Context): Tree = {
3636
val tastyBytes = TastyString.stringToTasty(expr.tasty)
3737
val unpickled = unpickle(tastyBytes, expr.args)
38-
unpickled match { // Expects `package _root_ { val ': Any = <tree> }`
38+
unpickled match {
3939
case PackageDef(_, (vdef: ValDef) :: Nil) => vdef.rhs
40+
case PackageDef(_, (tdef: TypeDef) :: Nil) => tdef.rhs
4041
}
4142
}
4243

43-
/** Encapsulate the tree in a top level `val`
44+
/** Encapsulate the tree in a top level `val` or `type`
4445
* `<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> }`
4948
*/
5049
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)
5460
}
5561

5662
// TASTY picklingtests/pos/quoteTest.scala

0 commit comments

Comments
 (0)