Skip to content

Commit c1a9ce3

Browse files
Merge pull request #8978 from dotty-staging/move-implementation-of-expr-to-lib
Move implementation of Expr/Type to lib
2 parents b5eded6 + b7ad505 commit c1a9ce3

File tree

10 files changed

+53
-68
lines changed

10 files changed

+53
-68
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import dotty.tools.dotc.core.tasty.{ PositionPickler, TastyPickler, TastyPrinter
1616
import dotty.tools.dotc.core.tasty.DottyUnpickler
1717
import dotty.tools.dotc.core.tasty.TreeUnpickler.UnpickleMode
1818
import dotty.tools.dotc.quoted.QuoteContext
19-
import dotty.tools.dotc.tastyreflect.{ReflectionImpl, TastyTreeExpr, TreeType}
19+
import dotty.tools.dotc.tastyreflect.ReflectionImpl
2020

2121
import dotty.tools.tasty.TastyString
2222

@@ -38,14 +38,14 @@ object PickledQuotes {
3838

3939
/** Transform the expression into its fully spliced Tree */
4040
def quotedExprToTree[T](expr: quoted.Expr[T])(implicit ctx: Context): Tree = {
41-
val expr1 = expr.asInstanceOf[TastyTreeExpr]
41+
val expr1 = expr.asInstanceOf[scala.internal.quoted.Expr[Tree]]
4242
QuoteContext.checkScopeId(expr1.scopeId)
4343
healOwner(expr1.tree)
4444
}
4545

4646
/** Transform the expression into its fully spliced TypeTree */
4747
def quotedTypeToTree(tpe: quoted.Type[?])(implicit ctx: Context): Tree = {
48-
val tpe1 = tpe.asInstanceOf[TreeType]
48+
val tpe1 = tpe.asInstanceOf[scala.internal.quoted.Type[Tree]]
4949
QuoteContext.checkScopeId(tpe1.scopeId)
5050
healOwner(tpe1.typeTree)
5151
}
@@ -74,8 +74,8 @@ object PickledQuotes {
7474
override def transform(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = tree match {
7575
case Hole(isTerm, idx, args) =>
7676
val reifiedArgs = args.map { arg =>
77-
if (arg.isTerm) (using qctx: scala.quoted.QuoteContext) => new TastyTreeExpr(arg, QuoteContext.scopeId)
78-
else new TreeType(arg, QuoteContext.scopeId)
77+
if (arg.isTerm) (using qctx: scala.quoted.QuoteContext) => new scala.internal.quoted.Expr(arg, QuoteContext.scopeId)
78+
else new scala.internal.quoted.Type(arg, QuoteContext.scopeId)
7979
}
8080
if isTerm then
8181
val splice1 = splices(idx).asInstanceOf[Seq[Any] => scala.quoted.QuoteContext ?=> quoted.Expr[?]]

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import dotty.tools.dotc.quoted.QuoteContext
3939
import dotty.tools.tasty.TastyFormat._
4040

4141
import scala.quoted
42-
import dotty.tools.dotc.tastyreflect.{TastyTreeExpr, TreeType}
4342
import scala.annotation.constructorOnly
4443
import scala.annotation.internal.sharable
4544

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
3535
// QUOTE UNPICKLING //
3636
//////////////////////
3737

38-
def unpickleExpr(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): scala.quoted.Expr[?] =
39-
new TastyTreeExpr(PickledQuotes.unpickleExpr(repr, args), compilerId)
38+
def unpickleExpr(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): Term =
39+
PickledQuotes.unpickleExpr(repr, args)
4040

41-
def unpickleType(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): scala.quoted.Type[?] =
42-
new TreeType(PickledQuotes.unpickleType(repr, args), compilerId)
41+
def unpickleType(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): TypeTree =
42+
PickledQuotes.unpickleType(repr, args)
4343

4444

4545
/////////////
@@ -1903,7 +1903,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
19031903

19041904
def QuotedExpr_seal(self: Term)(using ctx: Context): Option[scala.quoted.Expr[Any]] = self.tpe.widen match {
19051905
case _: Types.MethodType | _: Types.PolyType => None
1906-
case _ => Some(new TastyTreeExpr(self, compilerId))
1906+
case _ => Some(new scala.internal.quoted.Expr(self, compilerId))
19071907
}
19081908

19091909
/** Checked cast to a `quoted.Expr[U]` */
@@ -1923,7 +1923,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
19231923
/** Convert `Type` to an `quoted.Type[?]` */
19241924
def QuotedType_seal(self: Type)(using ctx: Context): scala.quoted.Type[?] = {
19251925
val dummySpan = ctx.owner.span // FIXME
1926-
new TreeType(tpd.TypeTree(self).withSpan(dummySpan), compilerId)
1926+
new scala.internal.quoted.Type(tpd.TypeTree(self).withSpan(dummySpan), compilerId)
19271927
}
19281928

19291929
/////////////////
@@ -2139,5 +2139,5 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
21392139
private def withDefaultPos[T <: Tree](fn: Context ?=> T)(using ctx: Context): T =
21402140
fn(using ctx.withSource(rootPosition.source)).withSpan(rootPosition.span)
21412141

2142-
private def compilerId: Int = rootContext.outersIterator.toList.last.hashCode()
2142+
def compilerId: Int = rootContext.outersIterator.toList.last.hashCode()
21432143
}

compiler/src/dotty/tools/dotc/tastyreflect/TastyTreeExpr.scala

Lines changed: 0 additions & 22 deletions
This file was deleted.

compiler/src/dotty/tools/dotc/tastyreflect/TreeType.scala

Lines changed: 0 additions & 16 deletions
This file was deleted.

compiler/src/dotty/tools/dotc/transform/Splicer.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import dotty.tools.dotc.core.Types._
1717
import dotty.tools.dotc.core.Symbols._
1818
import dotty.tools.dotc.core.{NameKinds, TypeErasure}
1919
import dotty.tools.dotc.core.Constants.Constant
20-
import dotty.tools.dotc.tastyreflect.{ReflectionImpl, TastyTreeExpr, TreeType}
20+
import dotty.tools.dotc.tastyreflect.ReflectionImpl
2121

2222
import scala.util.control.NonFatal
2323
import dotty.tools.dotc.util.SourcePosition
@@ -295,10 +295,10 @@ object Splicer {
295295
}
296296

297297
private def interpretQuote(tree: Tree)(implicit env: Env): Object =
298-
new TastyTreeExpr(Inlined(EmptyTree, Nil, tree).withSpan(tree.span), QuoteContext.scopeId)
298+
new scala.internal.quoted.Expr(Inlined(EmptyTree, Nil, tree).withSpan(tree.span), QuoteContext.scopeId)
299299

300300
private def interpretTypeQuote(tree: Tree)(implicit env: Env): Object =
301-
new TreeType(tree, QuoteContext.scopeId)
301+
new scala.internal.quoted.Type(tree, QuoteContext.scopeId)
302302

303303
private def interpretLiteral(value: Any)(implicit env: Env): Object =
304304
value.asInstanceOf[Object]

library/src/scala/internal/quoted/Expr.scala

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@ package scala.internal.quoted
22

33
import scala.quoted._
44

5-
/** Quoted expression of type `T`
5+
/** An Expr backed by a tree. Only the current compiler trees are allowed.
66
*
7-
* Restriction: only the QuoteContext.tasty.internal implementation is allowed to extend this trait.
8-
* Any other implementation will result in an undefined behavior.
7+
* These expressions are used for arguments of macros. They contain and actual tree
8+
* from the program that is being expanded by the macro.
9+
*
10+
* May contain references to code defined outside this Expr instance.
911
*/
10-
class Expr[+T] extends scala.quoted.Expr[T]
12+
final class Expr[Tree](val tree: Tree, val scopeId: Int) extends scala.quoted.Expr[Any] {
13+
override def equals(that: Any): Boolean = that match {
14+
case that: Expr[_] =>
15+
// Expr are wrappers around trees, therfore they are equals if their trees are equal.
16+
// All scopeId should be equal unless two different runs of the compiler created the trees.
17+
tree == that.tree && scopeId == that.scopeId
18+
case _ => false
19+
}
20+
override def hashCode: Int = tree.hashCode
21+
override def toString: String = "'{ ... }"
22+
}
1123

1224
object Expr {
1325

library/src/scala/internal/quoted/Type.scala

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ package scala.internal.quoted
22

33
import scala.quoted._
44

5-
/** Quoted type (or kind) `T`
6-
*
7-
* Restriction: only the QuoteContext.tasty.internal implementation is allowed to extend this trait.
8-
* Any other implementation will result in an undefined behavior.
9-
*/
10-
class Type[T <: AnyKind] extends scala.quoted.Type[T]
5+
/** Quoted type (or kind) `T` backed by a tree */
6+
final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quoted.Type[Any] {
7+
override def equals(that: Any): Boolean = that match {
8+
case that: Type[_] => typeTree ==
9+
// TastyTreeExpr are wrappers around trees, therfore they are equals if their trees are equal.
10+
// All scopeId should be equal unless two different runs of the compiler created the trees.
11+
that.typeTree && scopeId == that.scopeId
12+
case _ => false
13+
}
14+
override def hashCode: Int = typeTree.hashCode
15+
override def toString: String = "'[ ... ]"
16+
}
1117

1218
object Type {
1319

library/src/scala/internal/quoted/Unpickler.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ object Unpickler {
1212
* replacing splice nodes with `args`
1313
*/
1414
def unpickleExpr[T](repr: PickledQuote, args: PickledArgs): QuoteContext ?=> Expr[T] =
15-
summon[QuoteContext].tasty.internal.unpickleExpr(repr, args).asInstanceOf[Expr[T]]
15+
val ctx = summon[QuoteContext]
16+
val tree = ctx.tasty.internal.unpickleExpr(repr, args)
17+
new scala.internal.quoted.Expr(tree, ctx.tasty.internal.compilerId).asInstanceOf[Expr[T]]
1618

1719
/** Unpickle `repr` which represents a pickled `Type` tree,
1820
* replacing splice nodes with `args`
1921
*/
2022
def unpickleType[T](repr: PickledQuote, args: PickledArgs): QuoteContext ?=> Type[T] =
21-
summon[QuoteContext].tasty.internal.unpickleType(repr, args).asInstanceOf[Type[T]]
23+
val ctx = summon[QuoteContext]
24+
val tree = ctx.tasty.internal.unpickleType(repr, args)
25+
new scala.internal.quoted.Type(tree, ctx.tasty.internal.compilerId).asInstanceOf[Type[T]]
2226

2327
}

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ trait CompilerInterface {
125125
/** Unpickle `repr` which represents a pickled `Expr` tree,
126126
* replacing splice nodes with `args`
127127
*/
128-
def unpickleExpr(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): scala.quoted.Expr[Any]
128+
def unpickleExpr(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): Term
129129

130130
/** Unpickle `repr` which represents a pickled `Type` tree,
131131
* replacing splice nodes with `args`
132132
*/
133-
def unpickleType(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): scala.quoted.Type[_]
133+
def unpickleType(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): TypeTree
134134

135135

136136
/////////////
@@ -1576,4 +1576,6 @@ trait CompilerInterface {
15761576

15771577
def lambdaExtractor(term: Term, paramTypes: List[Type])(using ctx: Context): Option[List[Term] => Term]
15781578

1579+
def compilerId: Int
1580+
15791581
}

0 commit comments

Comments
 (0)