@@ -46,7 +46,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
46
46
reflect.Printer .TreeCode .show(reflect.asTerm(self))
47
47
48
48
def matches (that : scala.quoted.Expr [Any ]): Boolean =
49
- treeMatch(reflect.asTerm(self), reflect.asTerm(that)).nonEmpty
49
+ QuoteMatcher . treeMatch(reflect.asTerm(self), reflect.asTerm(that)).nonEmpty
50
50
51
51
def valueOrAbort (using fromExpr : FromExpr [T ]): T =
52
52
def reportError =
@@ -3110,65 +3110,14 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
3110
3110
def unapply [TypeBindings , Tup <: Tuple ](scrutinee : scala.quoted.Expr [Any ])(using pattern : scala.quoted.Expr [Any ]): Option [Tup ] =
3111
3111
val scrutineeTree = reflect.asTerm(scrutinee)
3112
3112
val patternTree = reflect.asTerm(pattern)
3113
- treeMatch(scrutineeTree, patternTree).asInstanceOf [Option [Tup ]]
3113
+ QuoteMatcher . treeMatch(scrutineeTree, patternTree).asInstanceOf [Option [Tup ]]
3114
3114
end ExprMatch
3115
3115
3116
3116
object TypeMatch extends TypeMatchModule :
3117
3117
def unapply [TypeBindings , Tup <: Tuple ](scrutinee : scala.quoted.Type [? ])(using pattern : scala.quoted.Type [? ]): Option [Tup ] =
3118
3118
val scrutineeTree = reflect.TypeTree .of(using scrutinee)
3119
3119
val patternTree = reflect.TypeTree .of(using pattern)
3120
- treeMatch(scrutineeTree, patternTree).asInstanceOf [Option [Tup ]]
3120
+ QuoteMatcher . treeMatch(scrutineeTree, patternTree).asInstanceOf [Option [Tup ]]
3121
3121
end TypeMatch
3122
3122
3123
- private def treeMatch (scrutinee : reflect.Tree , pattern : reflect.Tree ): Option [Tuple ] = {
3124
- import reflect ._
3125
- def isTypeHoleDef (tree : Tree ): Boolean =
3126
- tree match
3127
- case tree : TypeDef =>
3128
- tree.symbol.hasAnnotation(dotc.core.Symbols .defn.QuotedRuntimePatterns_patternTypeAnnot )
3129
- case _ => false
3130
-
3131
- def extractTypeHoles (pat : Term ): (Term , List [Symbol ]) =
3132
- pat match
3133
- case tpd.Inlined (_, Nil , pat2) => extractTypeHoles(pat2)
3134
- case tpd.Block (stats @ ((typeHole : TypeDef ) :: _), expr) if isTypeHoleDef(typeHole) =>
3135
- val holes = stats.takeWhile(isTypeHoleDef).map(_.symbol)
3136
- val otherStats = stats.dropWhile(isTypeHoleDef)
3137
- (tpd.cpy.Block (pat)(otherStats, expr), holes)
3138
- case _ =>
3139
- (pat, Nil )
3140
-
3141
- val (pat1, typeHoles) = extractTypeHoles(pattern)
3142
-
3143
- val ctx1 =
3144
- if typeHoles.isEmpty then ctx
3145
- else
3146
- val ctx1 = ctx.fresh.setFreshGADTBounds.addMode(dotc.core.Mode .GadtConstraintInference )
3147
- ctx1.gadtState.addToConstraint(typeHoles)
3148
- ctx1
3149
-
3150
- // After matching and doing all subtype checks, we have to approximate all the type bindings
3151
- // that we have found, seal them in a quoted.Type and add them to the result
3152
- def typeHoleApproximation (sym : Symbol ) =
3153
- val fromAboveAnnot = sym.hasAnnotation(dotc.core.Symbols .defn.QuotedRuntimePatterns_fromAboveAnnot )
3154
- val fullBounds = ctx1.gadt.fullBounds(sym)
3155
- if fromAboveAnnot then fullBounds.hi else fullBounds.lo
3156
-
3157
- QuoteMatcher .treeMatch(scrutinee, pat1)(using ctx1).map { matchings =>
3158
- import QuoteMatcher .MatchResult .*
3159
- lazy val spliceScope = SpliceScope .getCurrent
3160
- val typeHoleApproximations = typeHoles.map(typeHoleApproximation)
3161
- val typeHoleMapping = Map (typeHoles.zip(typeHoleApproximations)* )
3162
- val typeHoleMap = new Types .TypeMap {
3163
- def apply (tp : Types .Type ): Types .Type = tp match
3164
- case Types .TypeRef (Types .NoPrefix , _) => typeHoleMapping.getOrElse(tp.typeSymbol, tp)
3165
- case _ => mapOver(tp)
3166
- }
3167
- val matchedExprs = matchings.map(_.toExpr(typeHoleMap, spliceScope))
3168
- val matchedTypes = typeHoleApproximations.map(reflect.TypeReprMethods .asType)
3169
- val results = matchedTypes ++ matchedExprs
3170
- Tuple .fromIArray(IArray .unsafeFromArray(results.toArray))
3171
- }
3172
- }
3173
-
3174
3123
end QuotesImpl
0 commit comments