@@ -43,6 +43,42 @@ object QuoteContextImpl {
4343
4444class QuoteContextImpl private (ctx : Context ) extends QuoteContext , QuoteUnpickler , QuoteMatching :
4545
46+ extension [T ](self : scala.quoted.Expr [T ]):
47+ def show : String =
48+ reflect.TreeMethodsImpl .show(self.asReflectTree)
49+
50+ def showAnsiColored : String =
51+ reflect.TreeMethodsImpl .showAnsiColored(self.asReflectTree)
52+
53+ def matches (that : scala.quoted.Expr [Any ]): Boolean =
54+ treeMatch(self.asReflectTree, that.asReflectTree).nonEmpty
55+
56+ def asReflectTree : reflect.Term =
57+ val expr = self.asInstanceOf [ExprImpl ]
58+ expr.checkScopeId(QuoteContextImpl .this .hashCode)
59+ expr.tree
60+
61+ end extension
62+
63+ extension [X ](self : scala.quoted.Expr [Any ]):
64+ /** Checks is the `quoted.Expr[?]` is valid expression of type `X` */
65+ def isExprOf (using scala.quoted.Type [X ]): Boolean =
66+ reflect.TypeReprMethodsImpl .<:< (self.asReflectTree.tpe)(reflect.TypeRepr .of[X ])
67+
68+ /** Convert this to an `quoted.Expr[X]` if this expression is a valid expression of type `X` or throws */
69+ def asExprOf (using scala.quoted.Type [X ]): scala.quoted.Expr [X ] = {
70+ if isExprOf[X ] then
71+ self.asInstanceOf [scala.quoted.Expr [X ]]
72+ else
73+ throw Exception (
74+ s """ Expr cast exception: ${self.show}
75+ |of type: ${reflect.TypeReprMethodsImpl .show(self.asReflectTree.tpe)}
76+ |did not conform to type: ${reflect.TypeReprMethodsImpl .show(reflect.TypeRepr .of[X ])}
77+ | """ .stripMargin
78+ )
79+ }
80+ end extension
81+
4682 object reflect extends scala.tasty.Reflection :
4783
4884 def rootContext : Context = ctx
@@ -72,15 +108,15 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
72108 case _ => false
73109 def asExpr : scala.quoted.Expr [Any ] =
74110 if self.isExpr then
75- new dotty.tools.dotc.quoted. ExprImpl (self, QuoteContextImpl .this .hashCode)
111+ new ExprImpl (self, QuoteContextImpl .this .hashCode)
76112 else self match
77113 case TermTypeTest (self) => throw new Exception (" Expected an expression. This is a partially applied Term. Try eta-expanding the term first." )
78114 case _ => throw new Exception (" Expected a Term but was: " + self)
79115 end extension
80116
81117 extension [T ](self : Tree )
82118 def asExprOf (using tp : scala.quoted.Type [T ]): scala.quoted.Expr [T ] =
83- self.asExpr .asExprOf[T ](using tp )(using QuoteContextImpl . this )
119+ QuoteContextImpl . this .asExprOf[T ](self.asExpr )(using tp )
84120 end extension
85121
86122 end TreeMethodsImpl
@@ -316,11 +352,11 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
316352 object TermMethodsImpl extends TermMethods :
317353 extension (self : Term ):
318354 def seal : scala.quoted.Expr [Any ] =
319- if self.isExpr then new dotty.tools.dotc.quoted. ExprImpl (self, QuoteContextImpl .this .hashCode)
355+ if self.isExpr then new ExprImpl (self, QuoteContextImpl .this .hashCode)
320356 else throw new Exception (" Cannot seal a partially applied Term. Try eta-expanding the term first." )
321357
322358 def sealOpt : Option [scala.quoted.Expr [Any ]] =
323- if self.isExpr then Some (new dotty.tools.dotc.quoted. ExprImpl (self, QuoteContextImpl .this .hashCode))
359+ if self.isExpr then Some (new ExprImpl (self, QuoteContextImpl .this .hashCode))
324360 else None
325361
326362 def tpe : TypeRepr = self.tpe
@@ -1003,7 +1039,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
10031039
10041040 object TypeTree extends TypeTreeModule :
10051041 def of [T <: AnyKind ](using tp : scala.quoted.Type [T ]): TypeTree =
1006- tp.asInstanceOf [dotty.tools.dotc.quoted. TypeImpl ].typeTree
1042+ tp.asInstanceOf [TypeImpl ].typeTree
10071043 end TypeTree
10081044
10091045 object TypeTreeMethodsImpl extends TypeTreeMethods :
@@ -1572,7 +1608,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
15721608
15731609 object TypeRepr extends TypeReprModule :
15741610 def of [T <: AnyKind ](using tp : scala.quoted.Type [T ]): TypeRepr =
1575- tp.asInstanceOf [dotty.tools.dotc.quoted. TypeImpl ].typeTree.tpe
1611+ tp.asInstanceOf [TypeImpl ].typeTree.tpe
15761612 def typeConstructorOf (clazz : Class [? ]): TypeRepr =
15771613 if (clazz.isPrimitive)
15781614 if (clazz == classOf [Boolean ]) dotc.core.Symbols .defn.BooleanType
@@ -1609,7 +1645,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
16091645 def seal : scala.quoted.Type [_] = self.asType
16101646
16111647 def asType : scala.quoted.Type [? ] =
1612- new dotty.tools.dotc.quoted. TypeImpl (Inferred (self), QuoteContextImpl .this .hashCode)
1648+ new TypeImpl (Inferred (self), QuoteContextImpl .this .hashCode)
16131649
16141650 def =:= (that : TypeRepr ): Boolean = self =:= that
16151651 def <:< (that : TypeRepr ): Boolean = self <:< that
@@ -2624,16 +2660,16 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickl
26242660
26252661 def unpickleExpr [T ](pickled : String | List [String ], typeHole : (Int , Seq [Any ]) => scala.quoted.Type [? ], termHole : (Int , Seq [Any ], scala.quoted.QuoteContext ) => scala.quoted.Expr [? ]): scala.quoted.Expr [T ] =
26262662 val tree = PickledQuotes .unpickleTerm(pickled, typeHole, termHole)(using reflect.rootContext)
2627- new dotty.tools.dotc.quoted. ExprImpl (tree, hash).asInstanceOf [scala.quoted.Expr [T ]]
2663+ new ExprImpl (tree, hash).asInstanceOf [scala.quoted.Expr [T ]]
26282664
26292665 def unpickleType [T <: AnyKind ](pickled : String | List [String ], typeHole : (Int , Seq [Any ]) => scala.quoted.Type [? ], termHole : (Int , Seq [Any ], scala.quoted.QuoteContext ) => scala.quoted.Expr [? ]): scala.quoted.Type [T ] =
26302666 val tree = PickledQuotes .unpickleTypeTree(pickled, typeHole, termHole)(using reflect.rootContext)
2631- new dotty.tools.dotc.quoted. TypeImpl (tree, hash).asInstanceOf [scala.quoted.Type [T ]]
2667+ new TypeImpl (tree, hash).asInstanceOf [scala.quoted.Type [T ]]
26322668
26332669 object ExprMatch extends ExprMatchModule :
26342670 def unapply [TypeBindings <: Tuple , Tup <: Tuple ](scrutinee : scala.quoted.Expr [Any ])(using pattern : scala.quoted.Expr [Any ]): Option [Tup ] =
2635- val scrutineeTree = scrutinee.unseal( using QuoteContextImpl .this )
2636- val patternTree = pattern.unseal( using QuoteContextImpl .this )
2671+ val scrutineeTree = QuoteContextImpl .this .asReflectTree(scrutinee )
2672+ val patternTree = QuoteContextImpl .this .asReflectTree(pattern )
26372673 treeMatch(scrutineeTree, patternTree).asInstanceOf [Option [Tup ]]
26382674 end ExprMatch
26392675
0 commit comments