@@ -6,12 +6,32 @@ import dotty.tools.dotc.core.Flags._
66import dotty .tools .dotc .core .Symbols .defn
77import dotty .tools .dotc .core .StdNames .nme
88import dotty .tools .dotc .core .quoted .PickledQuotes
9- import dotty .tools .dotc .core .Types
9+ import dotty .tools .dotc .core .{Contexts , Types }
10+
11+ import scala .quoted .Expr
1012
1113trait QuotedOpsImpl extends scala.tasty.reflect.QuotedOps with CoreImpl {
1214
13- def QuotedExprDeco [T ](x : scala.quoted.Expr [T ]): QuotedExprAPI = new QuotedExprAPI {
14- def unseal (implicit ctx : Context ): Term = PickledQuotes .quotedExprToTree(x)
15+ def QuotedExprDeco [T ](thisExpr : scala.quoted.Expr [T ]) = new QuotedExprAPI {
16+ type Tpe = T
17+
18+ def unseal (implicit ctx : Context ): Term = PickledQuotes .quotedExprToTree(thisExpr)
19+
20+ def asExprOf [T : scala.quoted.Type ](implicit ctx : Context ): scala.quoted.Expr [T ] = {
21+ val expectedType = QuotedTypeDeco (implicitly[scala.quoted.Type [T ]]).unseal.tpe
22+ if (unseal.tpe <:< expectedType) thisExpr.asInstanceOf [scala.quoted.Expr [T ]]
23+ else throw new scala.tasty.TastyTypecheckError (
24+ s """ Expr: ${unseal.show}
25+ |did not conform to type: ${expectedType.show}
26+ | """ .stripMargin
27+ )
28+ }
29+
30+ def tpe (implicit ctx : Context ): quoted.Type [T ] = {
31+ val tree = unseal
32+ val tpt = tpd.TypeTree (tree.tpe).withPos(tree.pos)
33+ new scala.quoted.Types .TreeType (tpt).asInstanceOf [quoted.Type [T ]]
34+ }
1535 }
1636
1737 def QuotedTypeDeco [T ](x : scala.quoted.Type [T ]): QuotedTypeAPI = new QuotedTypeAPI {
@@ -20,10 +40,10 @@ trait QuotedOpsImpl extends scala.tasty.reflect.QuotedOps with CoreImpl {
2040
2141 def TermToQuoteDeco (term : Term ): TermToQuotedAPI = new TermToQuotedAPI {
2242
23- def seal [T : scala.quoted.Type ](implicit ctx : Context ): scala.quoted.Expr [T ] = {
24-
25- val expectedType = QuotedTypeDeco (implicitly[scala.quoted.Type [T ]]).unseal.tpe
43+ def seal [T : scala.quoted.Type ](implicit ctx : Context ): scala.quoted.Expr [T ] =
44+ QuotedExprDeco (seal2).asExprOf[T ]
2645
46+ def seal2 (implicit ctx : Context ): scala.quoted.Expr [Tpe ] = {
2747 def etaExpand (term : Term ): Term = term.tpe.widen match {
2848 case mtpe : Types .MethodType if ! mtpe.isParamDependent =>
2949 val closureResType = mtpe.resType match {
@@ -37,22 +57,8 @@ trait QuotedOpsImpl extends scala.tasty.reflect.QuotedOps with CoreImpl {
3757 }
3858
3959 val expanded = etaExpand(term)
40- if (expanded.tpe <:< expectedType) {
41- new scala.quoted.Exprs .TastyTreeExpr (expanded).asInstanceOf [scala.quoted.Expr [T ]]
42- } else {
43- throw new scala.tasty.TastyTypecheckError (
44- s """ Term: ${term.show}
45- |did not conform to type: ${expectedType.show}
46- | """ .stripMargin
47- )
48- }
60+ new scala.quoted.Exprs .TastyTreeExpr (expanded).asInstanceOf [scala.quoted.Expr [Tpe ]]
4961 }
5062 }
5163
52- def TypeToQuoteDeco (tpe : Types .Type ): TypeToQuotedAPI = new TypeToQuotedAPI {
53- def seal (implicit ctx : Context ): quoted.Type [_] = {
54- val dummyPos = ctx.owner.pos // FIXME
55- new scala.quoted.Types .TreeType (tpd.TypeTree (tpe).withPos(dummyPos))
56- }
57- }
5864}
0 commit comments