@@ -45,7 +45,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
45
45
reflect.Printer .TreeCode .show(reflect.asTerm(self))
46
46
47
47
def matches (that : scala.quoted.Expr [Any ]): Boolean =
48
- treeMatch(reflect.asTerm(self), reflect.asTerm(that)).nonEmpty
48
+ QuoteMatcher . treeMatch(reflect.asTerm(self), reflect.asTerm(that)).nonEmpty
49
49
50
50
def valueOrAbort (using fromExpr : FromExpr [T ]): T =
51
51
def reportError =
@@ -3155,65 +3155,14 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
3155
3155
def unapply [TypeBindings , Tup <: Tuple ](scrutinee : scala.quoted.Expr [Any ])(using pattern : scala.quoted.Expr [Any ]): Option [Tup ] =
3156
3156
val scrutineeTree = reflect.asTerm(scrutinee)
3157
3157
val patternTree = reflect.asTerm(pattern)
3158
- treeMatch(scrutineeTree, patternTree).asInstanceOf [Option [Tup ]]
3158
+ QuoteMatcher . treeMatch(scrutineeTree, patternTree).asInstanceOf [Option [Tup ]]
3159
3159
end ExprMatch
3160
3160
3161
3161
object TypeMatch extends TypeMatchModule :
3162
3162
def unapply [TypeBindings , Tup <: Tuple ](scrutinee : scala.quoted.Type [? ])(using pattern : scala.quoted.Type [? ]): Option [Tup ] =
3163
3163
val scrutineeTree = reflect.TypeTree .of(using scrutinee)
3164
3164
val patternTree = reflect.TypeTree .of(using pattern)
3165
- treeMatch(scrutineeTree, patternTree).asInstanceOf [Option [Tup ]]
3165
+ QuoteMatcher . treeMatch(scrutineeTree, patternTree).asInstanceOf [Option [Tup ]]
3166
3166
end TypeMatch
3167
3167
3168
- private def treeMatch (scrutinee : reflect.Tree , pattern : reflect.Tree ): Option [Tuple ] = {
3169
- import reflect ._
3170
- def isTypeHoleDef (tree : Tree ): Boolean =
3171
- tree match
3172
- case tree : TypeDef =>
3173
- tree.symbol.hasAnnotation(dotc.core.Symbols .defn.QuotedRuntimePatterns_patternTypeAnnot )
3174
- case _ => false
3175
-
3176
- def extractTypeHoles (pat : Term ): (Term , List [Symbol ]) =
3177
- pat match
3178
- case tpd.Inlined (_, Nil , pat2) => extractTypeHoles(pat2)
3179
- case tpd.Block (stats @ ((typeHole : TypeDef ) :: _), expr) if isTypeHoleDef(typeHole) =>
3180
- val holes = stats.takeWhile(isTypeHoleDef).map(_.symbol)
3181
- val otherStats = stats.dropWhile(isTypeHoleDef)
3182
- (tpd.cpy.Block (pat)(otherStats, expr), holes)
3183
- case _ =>
3184
- (pat, Nil )
3185
-
3186
- val (pat1, typeHoles) = extractTypeHoles(pattern)
3187
-
3188
- val ctx1 =
3189
- if typeHoles.isEmpty then ctx
3190
- else
3191
- val ctx1 = ctx.fresh.setFreshGADTBounds.addMode(dotc.core.Mode .GadtConstraintInference )
3192
- ctx1.gadtState.addToConstraint(typeHoles)
3193
- ctx1
3194
-
3195
- // After matching and doing all subtype checks, we have to approximate all the type bindings
3196
- // that we have found, seal them in a quoted.Type and add them to the result
3197
- def typeHoleApproximation (sym : Symbol ) =
3198
- val fromAboveAnnot = sym.hasAnnotation(dotc.core.Symbols .defn.QuotedRuntimePatterns_fromAboveAnnot )
3199
- val fullBounds = ctx1.gadt.fullBounds(sym)
3200
- if fromAboveAnnot then fullBounds.hi else fullBounds.lo
3201
-
3202
- QuoteMatcher .treeMatch(scrutinee, pat1)(using ctx1).map { matchings =>
3203
- import QuoteMatcher .MatchResult .*
3204
- lazy val spliceScope = SpliceScope .getCurrent
3205
- val typeHoleApproximations = typeHoles.map(typeHoleApproximation)
3206
- val typeHoleMapping = Map (typeHoles.zip(typeHoleApproximations)* )
3207
- val typeHoleMap = new Types .TypeMap {
3208
- def apply (tp : Types .Type ): Types .Type = tp match
3209
- case Types .TypeRef (Types .NoPrefix , _) => typeHoleMapping.getOrElse(tp.typeSymbol, tp)
3210
- case _ => mapOver(tp)
3211
- }
3212
- val matchedExprs = matchings.map(_.toExpr(typeHoleMap, spliceScope))
3213
- val matchedTypes = typeHoleApproximations.map(reflect.TypeReprMethods .asType)
3214
- val results = matchedTypes ++ matchedExprs
3215
- Tuple .fromIArray(IArray .unsafeFromArray(results.toArray))
3216
- }
3217
- }
3218
-
3219
3168
end QuotesImpl
0 commit comments