diff --git a/compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala b/compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala index 95415e7ceeff..9bd48b7560b5 100644 --- a/compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala +++ b/compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala @@ -16,7 +16,7 @@ import dotty.tools.dotc.core.tasty.{ PositionPickler, TastyPickler, TastyPrinter import dotty.tools.dotc.core.tasty.DottyUnpickler import dotty.tools.dotc.core.tasty.TreeUnpickler.UnpickleMode import dotty.tools.dotc.quoted.QuoteContext -import dotty.tools.dotc.tastyreflect.{ReflectionImpl, TastyTreeExpr, TreeType} +import dotty.tools.dotc.tastyreflect.ReflectionImpl import dotty.tools.tasty.TastyString @@ -38,14 +38,14 @@ object PickledQuotes { /** Transform the expression into its fully spliced Tree */ def quotedExprToTree[T](expr: quoted.Expr[T])(implicit ctx: Context): Tree = { - val expr1 = expr.asInstanceOf[TastyTreeExpr] + val expr1 = expr.asInstanceOf[scala.internal.quoted.Expr[Tree]] QuoteContext.checkScopeId(expr1.scopeId) healOwner(expr1.tree) } /** Transform the expression into its fully spliced TypeTree */ def quotedTypeToTree(tpe: quoted.Type[?])(implicit ctx: Context): Tree = { - val tpe1 = tpe.asInstanceOf[TreeType] + val tpe1 = tpe.asInstanceOf[scala.internal.quoted.Type[Tree]] QuoteContext.checkScopeId(tpe1.scopeId) healOwner(tpe1.typeTree) } @@ -74,8 +74,8 @@ object PickledQuotes { override def transform(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = tree match { case Hole(isTerm, idx, args) => val reifiedArgs = args.map { arg => - if (arg.isTerm) (using qctx: scala.quoted.QuoteContext) => new TastyTreeExpr(arg, QuoteContext.scopeId) - else new TreeType(arg, QuoteContext.scopeId) + if (arg.isTerm) (using qctx: scala.quoted.QuoteContext) => new scala.internal.quoted.Expr(arg, QuoteContext.scopeId) + else new scala.internal.quoted.Type(arg, QuoteContext.scopeId) } if isTerm then val splice1 = splices(idx).asInstanceOf[Seq[Any] => scala.quoted.QuoteContext ?=> quoted.Expr[?]] diff --git a/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala b/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala index 0f39262eacca..f8adb6426a7d 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala @@ -39,7 +39,6 @@ import dotty.tools.dotc.quoted.QuoteContext import dotty.tools.tasty.TastyFormat._ import scala.quoted -import dotty.tools.dotc.tastyreflect.{TastyTreeExpr, TreeType} import scala.annotation.constructorOnly import scala.annotation.internal.sharable diff --git a/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala b/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala index 546b7aea7bef..44213d2c3cdc 100644 --- a/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala +++ b/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala @@ -35,11 +35,11 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend // QUOTE UNPICKLING // ////////////////////// - def unpickleExpr(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): scala.quoted.Expr[?] = - new TastyTreeExpr(PickledQuotes.unpickleExpr(repr, args), compilerId) + def unpickleExpr(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): Term = + PickledQuotes.unpickleExpr(repr, args) - def unpickleType(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): scala.quoted.Type[?] = - new TreeType(PickledQuotes.unpickleType(repr, args), compilerId) + def unpickleType(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): TypeTree = + PickledQuotes.unpickleType(repr, args) ///////////// @@ -1903,7 +1903,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend def QuotedExpr_seal(self: Term)(using ctx: Context): Option[scala.quoted.Expr[Any]] = self.tpe.widen match { case _: Types.MethodType | _: Types.PolyType => None - case _ => Some(new TastyTreeExpr(self, compilerId)) + case _ => Some(new scala.internal.quoted.Expr(self, compilerId)) } /** Checked cast to a `quoted.Expr[U]` */ @@ -1923,7 +1923,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend /** Convert `Type` to an `quoted.Type[?]` */ def QuotedType_seal(self: Type)(using ctx: Context): scala.quoted.Type[?] = { val dummySpan = ctx.owner.span // FIXME - new TreeType(tpd.TypeTree(self).withSpan(dummySpan), compilerId) + new scala.internal.quoted.Type(tpd.TypeTree(self).withSpan(dummySpan), compilerId) } ///////////////// @@ -2139,5 +2139,5 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend private def withDefaultPos[T <: Tree](fn: Context ?=> T)(using ctx: Context): T = fn(using ctx.withSource(rootPosition.source)).withSpan(rootPosition.span) - private def compilerId: Int = rootContext.outersIterator.toList.last.hashCode() + def compilerId: Int = rootContext.outersIterator.toList.last.hashCode() } diff --git a/compiler/src/dotty/tools/dotc/tastyreflect/TastyTreeExpr.scala b/compiler/src/dotty/tools/dotc/tastyreflect/TastyTreeExpr.scala deleted file mode 100644 index f831e11b2d8d..000000000000 --- a/compiler/src/dotty/tools/dotc/tastyreflect/TastyTreeExpr.scala +++ /dev/null @@ -1,22 +0,0 @@ -package dotty.tools.dotc.tastyreflect - -import dotty.tools.dotc.ast.tpd.Tree - -/** An Expr backed by a tree. Only the current compiler trees are allowed. - * - * These expressions are used for arguments of macros. They contain and actual tree - * from the program that is being expanded by the macro. - * - * May contain references to code defined outside this TastyTreeExpr instance. - */ -final class TastyTreeExpr(val tree: Tree, val scopeId: Int) extends scala.internal.quoted.Expr[Any] { - override def equals(that: Any): Boolean = that match { - case that: TastyTreeExpr => - // TastyTreeExpr are wrappers around trees, therfore they are equals if their trees are equal. - // All scopeId should be equal unless two different runs of the compiler created the trees. - tree == that.tree && scopeId == that.scopeId - case _ => false - } - override def hashCode: Int = tree.hashCode - override def toString: String = "'{ ... }" -} diff --git a/compiler/src/dotty/tools/dotc/tastyreflect/TreeType.scala b/compiler/src/dotty/tools/dotc/tastyreflect/TreeType.scala deleted file mode 100644 index d471f9c87a84..000000000000 --- a/compiler/src/dotty/tools/dotc/tastyreflect/TreeType.scala +++ /dev/null @@ -1,16 +0,0 @@ -package dotty.tools.dotc.tastyreflect - -import dotty.tools.dotc.ast.tpd.Tree - -/** An Type backed by a tree */ -final class TreeType(val typeTree: Tree, val scopeId: Int) extends scala.internal.quoted.Type[Any] { - override def equals(that: Any): Boolean = that match { - case that: TreeType => typeTree == - // TastyTreeExpr are wrappers around trees, therfore they are equals if their trees are equal. - // All scopeId should be equal unless two different runs of the compiler created the trees. - that.typeTree && scopeId == that.scopeId - case _ => false - } - override def hashCode: Int = typeTree.hashCode - override def toString: String = "'[ ... ]" -} diff --git a/compiler/src/dotty/tools/dotc/transform/Splicer.scala b/compiler/src/dotty/tools/dotc/transform/Splicer.scala index c491255abb5f..d33038d2018a 100644 --- a/compiler/src/dotty/tools/dotc/transform/Splicer.scala +++ b/compiler/src/dotty/tools/dotc/transform/Splicer.scala @@ -17,7 +17,7 @@ import dotty.tools.dotc.core.Types._ import dotty.tools.dotc.core.Symbols._ import dotty.tools.dotc.core.{NameKinds, TypeErasure} import dotty.tools.dotc.core.Constants.Constant -import dotty.tools.dotc.tastyreflect.{ReflectionImpl, TastyTreeExpr, TreeType} +import dotty.tools.dotc.tastyreflect.ReflectionImpl import scala.util.control.NonFatal import dotty.tools.dotc.util.SourcePosition @@ -295,10 +295,10 @@ object Splicer { } private def interpretQuote(tree: Tree)(implicit env: Env): Object = - new TastyTreeExpr(Inlined(EmptyTree, Nil, tree).withSpan(tree.span), QuoteContext.scopeId) + new scala.internal.quoted.Expr(Inlined(EmptyTree, Nil, tree).withSpan(tree.span), QuoteContext.scopeId) private def interpretTypeQuote(tree: Tree)(implicit env: Env): Object = - new TreeType(tree, QuoteContext.scopeId) + new scala.internal.quoted.Type(tree, QuoteContext.scopeId) private def interpretLiteral(value: Any)(implicit env: Env): Object = value.asInstanceOf[Object] diff --git a/library/src/scala/internal/quoted/Expr.scala b/library/src/scala/internal/quoted/Expr.scala index 57dfafedba98..bb9f67f46e52 100644 --- a/library/src/scala/internal/quoted/Expr.scala +++ b/library/src/scala/internal/quoted/Expr.scala @@ -2,12 +2,24 @@ package scala.internal.quoted import scala.quoted._ -/** Quoted expression of type `T` +/** An Expr backed by a tree. Only the current compiler trees are allowed. * - * Restriction: only the QuoteContext.tasty.internal implementation is allowed to extend this trait. - * Any other implementation will result in an undefined behavior. + * These expressions are used for arguments of macros. They contain and actual tree + * from the program that is being expanded by the macro. + * + * May contain references to code defined outside this Expr instance. */ -class Expr[+T] extends scala.quoted.Expr[T] + final class Expr[Tree](val tree: Tree, val scopeId: Int) extends scala.quoted.Expr[Any] { + override def equals(that: Any): Boolean = that match { + case that: Expr[_] => + // Expr are wrappers around trees, therfore they are equals if their trees are equal. + // All scopeId should be equal unless two different runs of the compiler created the trees. + tree == that.tree && scopeId == that.scopeId + case _ => false + } + override def hashCode: Int = tree.hashCode + override def toString: String = "'{ ... }" +} object Expr { diff --git a/library/src/scala/internal/quoted/Type.scala b/library/src/scala/internal/quoted/Type.scala index 52da804b8a6d..8a11172be5fd 100644 --- a/library/src/scala/internal/quoted/Type.scala +++ b/library/src/scala/internal/quoted/Type.scala @@ -2,12 +2,18 @@ package scala.internal.quoted import scala.quoted._ -/** Quoted type (or kind) `T` - * - * Restriction: only the QuoteContext.tasty.internal implementation is allowed to extend this trait. - * Any other implementation will result in an undefined behavior. - */ -class Type[T <: AnyKind] extends scala.quoted.Type[T] +/** Quoted type (or kind) `T` backed by a tree */ +final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quoted.Type[Any] { + override def equals(that: Any): Boolean = that match { + case that: Type[_] => typeTree == + // TastyTreeExpr are wrappers around trees, therfore they are equals if their trees are equal. + // All scopeId should be equal unless two different runs of the compiler created the trees. + that.typeTree && scopeId == that.scopeId + case _ => false + } + override def hashCode: Int = typeTree.hashCode + override def toString: String = "'[ ... ]" +} object Type { diff --git a/library/src/scala/internal/quoted/Unpickler.scala b/library/src/scala/internal/quoted/Unpickler.scala index 14dd501c4cee..86fed70f4cf3 100644 --- a/library/src/scala/internal/quoted/Unpickler.scala +++ b/library/src/scala/internal/quoted/Unpickler.scala @@ -12,12 +12,16 @@ object Unpickler { * replacing splice nodes with `args` */ def unpickleExpr[T](repr: PickledQuote, args: PickledArgs): QuoteContext ?=> Expr[T] = - summon[QuoteContext].tasty.internal.unpickleExpr(repr, args).asInstanceOf[Expr[T]] + val ctx = summon[QuoteContext] + val tree = ctx.tasty.internal.unpickleExpr(repr, args) + new scala.internal.quoted.Expr(tree, ctx.tasty.internal.compilerId).asInstanceOf[Expr[T]] /** Unpickle `repr` which represents a pickled `Type` tree, * replacing splice nodes with `args` */ def unpickleType[T](repr: PickledQuote, args: PickledArgs): QuoteContext ?=> Type[T] = - summon[QuoteContext].tasty.internal.unpickleType(repr, args).asInstanceOf[Type[T]] + val ctx = summon[QuoteContext] + val tree = ctx.tasty.internal.unpickleType(repr, args) + new scala.internal.quoted.Type(tree, ctx.tasty.internal.compilerId).asInstanceOf[Type[T]] } diff --git a/library/src/scala/tasty/reflect/CompilerInterface.scala b/library/src/scala/tasty/reflect/CompilerInterface.scala index e6d953579fb1..a90f4f1b3e87 100644 --- a/library/src/scala/tasty/reflect/CompilerInterface.scala +++ b/library/src/scala/tasty/reflect/CompilerInterface.scala @@ -125,12 +125,12 @@ trait CompilerInterface { /** Unpickle `repr` which represents a pickled `Expr` tree, * replacing splice nodes with `args` */ - def unpickleExpr(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): scala.quoted.Expr[Any] + def unpickleExpr(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): Term /** Unpickle `repr` which represents a pickled `Type` tree, * replacing splice nodes with `args` */ - def unpickleType(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): scala.quoted.Type[_] + def unpickleType(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): TypeTree ///////////// @@ -1576,4 +1576,6 @@ trait CompilerInterface { def lambdaExtractor(term: Term, paramTypes: List[Type])(using ctx: Context): Option[List[Term] => Term] + def compilerId: Int + }