@@ -4,6 +4,7 @@ import scala.annotation.internal.sharable
4
4
import scala .annotation .{Annotation , compileTimeOnly }
5
5
6
6
import scala .quoted ._
7
+ import scala .internal .tasty .CompilerInterface .quoteContextWithCompilerInterface
7
8
8
9
/** Matches a quoted tree against a quoted pattern tree.
9
10
* A quoted pattern tree may have type and term holes in addition to normal terms.
@@ -122,7 +123,9 @@ object Matcher {
122
123
@ compileTimeOnly(" Illegal reference to `scala.internal.quoted.CompileTime.fromAbove`" )
123
124
class fromAbove extends Annotation
124
125
125
- class QuoteMatcher [QCtx <: QuoteContext & Singleton ](using val qctx : QCtx ) {
126
+ class QuoteMatcher [QCtx <: QuoteContext & Singleton ](val qctx0 : QCtx ) {
127
+ val qctx = quoteContextWithCompilerInterface(qctx0)
128
+
126
129
// TODO improve performance
127
130
128
131
// TODO use flag from qctx.tasty.rootContext. Maybe -debug or add -debug-macros
@@ -147,14 +150,14 @@ object Matcher {
147
150
def termMatch (scrutineeTerm : Term , patternTerm : Term , hasTypeSplices : Boolean ): Option [Tuple ] = {
148
151
given Env = Map .empty
149
152
if (hasTypeSplices) {
150
- val ctx : Context = internal .Constraints_init (rootContext)
153
+ val ctx : Context = qctx.tasty .Constraints_init (rootContext)
151
154
given Context = ctx
152
155
val matchings = scrutineeTerm =?= patternTerm
153
156
// After matching and doing all subtype checks, we have to approximate all the type bindings
154
157
// that we have found and seal them in a quoted.Type
155
158
matchings.asOptionOfTuple.map { tup =>
156
159
Tuple .fromArray(tup.toArray.map { // TODO improve performance
157
- case x : SymBinding => internal .Constraints_approximation (summon[Context ])(x.sym, ! x.fromAbove).seal
160
+ case x : SymBinding => qctx.tasty .Constraints_approximation (summon[Context ])(x.sym, ! x.fromAbove).seal
158
161
case x => x
159
162
})
160
163
}
@@ -168,14 +171,14 @@ object Matcher {
168
171
def typeTreeMatch (scrutineeTypeTree : TypeTree , patternTypeTree : TypeTree , hasTypeSplices : Boolean ): Option [Tuple ] = {
169
172
given Env = Map .empty
170
173
if (hasTypeSplices) {
171
- val ctx : Context = internal .Constraints_init (rootContext)
174
+ val ctx : Context = qctx.tasty .Constraints_init (rootContext)
172
175
given Context = ctx
173
176
val matchings = scrutineeTypeTree =?= patternTypeTree
174
177
// After matching and doing all subtype checks, we have to approximate all the type bindings
175
178
// that we have found and seal them in a quoted.Type
176
179
matchings.asOptionOfTuple.map { tup =>
177
180
Tuple .fromArray(tup.toArray.map { // TODO improve performance
178
- case x : SymBinding => internal .Constraints_approximation (summon[Context ])(x.sym, ! x.fromAbove).seal
181
+ case x : SymBinding => qctx.tasty .Constraints_approximation (summon[Context ])(x.sym, ! x.fromAbove).seal
179
182
case x => x
180
183
})
181
184
}
@@ -190,13 +193,13 @@ object Matcher {
190
193
private def hasFromAboveAnnotation (sym : Symbol ) = sym.annots.exists(isFromAboveAnnotation)
191
194
192
195
private def isPatternTypeAnnotation (tree : Tree ): Boolean = tree match {
193
- case New (tpt) => tpt.symbol == internal .Definitions_InternalQuotedMatcher_patternTypeAnnot
194
- case annot => annot.symbol.owner == internal .Definitions_InternalQuotedMatcher_patternTypeAnnot
196
+ case New (tpt) => tpt.symbol == qctx.tasty .Definitions_InternalQuotedMatcher_patternTypeAnnot
197
+ case annot => annot.symbol.owner == qctx.tasty .Definitions_InternalQuotedMatcher_patternTypeAnnot
195
198
}
196
199
197
200
private def isFromAboveAnnotation (tree : Tree ): Boolean = tree match {
198
- case New (tpt) => tpt.symbol == internal .Definitions_InternalQuotedMatcher_fromAboveAnnot
199
- case annot => annot.symbol.owner == internal .Definitions_InternalQuotedMatcher_fromAboveAnnot
201
+ case New (tpt) => tpt.symbol == qctx.tasty .Definitions_InternalQuotedMatcher_fromAboveAnnot
202
+ case annot => annot.symbol.owner == qctx.tasty .Definitions_InternalQuotedMatcher_fromAboveAnnot
200
203
}
201
204
202
205
/** Check that all trees match with `mtch` and concatenate the results with &&& */
@@ -250,22 +253,22 @@ object Matcher {
250
253
/* Term hole */
251
254
// Match a scala.internal.Quoted.patternHole typed as a repeated argument and return the scrutinee tree
252
255
case (scrutinee @ Typed (s, tpt1), Typed (TypeApply (patternHole, tpt :: Nil ), tpt2))
253
- if patternHole.symbol == internal .Definitions_InternalQuotedMatcher_patternHole &&
256
+ if patternHole.symbol == qctx.tasty .Definitions_InternalQuotedMatcher_patternHole &&
254
257
s.tpe <:< tpt.tpe &&
255
258
tpt2.tpe.derivesFrom(defn.RepeatedParamClass ) =>
256
259
matched(scrutinee.seal)
257
260
258
261
/* Term hole */
259
262
// Match a scala.internal.Quoted.patternHole and return the scrutinee tree
260
263
case (ClosedPatternTerm (scrutinee), TypeApply (patternHole, tpt :: Nil ))
261
- if patternHole.symbol == internal .Definitions_InternalQuotedMatcher_patternHole &&
264
+ if patternHole.symbol == qctx.tasty .Definitions_InternalQuotedMatcher_patternHole &&
262
265
scrutinee.tpe <:< tpt.tpe =>
263
266
matched(scrutinee.seal)
264
267
265
268
/* Higher order term hole */
266
269
// Matches an open term and wraps it into a lambda that provides the free variables
267
270
case (scrutinee, pattern @ Apply (TypeApply (Ident (" higherOrderHole" ), List (Inferred ())), Repeated (args, _) :: Nil ))
268
- if pattern.symbol == internal .Definitions_InternalQuotedMatcher_higherOrderHole =>
271
+ if pattern.symbol == qctx.tasty .Definitions_InternalQuotedMatcher_higherOrderHole =>
269
272
270
273
def bodyFn (lambdaArgs : List [Tree ]): Tree = {
271
274
val argsMap = args.map(_.symbol).zip(lambdaArgs.asInstanceOf [List [Term ]]).toMap
@@ -323,7 +326,7 @@ object Matcher {
323
326
fn1 =?= fn2 &&& args1 =?= args2
324
327
325
328
case (Block (stats1, expr1), Block (binding :: stats2, expr2)) if isTypeBinding(binding) =>
326
- qctx.tasty.internal. Constraints_add (summon[Context ])(binding.symbol :: Nil )
329
+ qctx.tasty.Constraints_add (summon[Context ])(binding.symbol :: Nil )
327
330
matched(new SymBinding (binding.symbol, hasFromAboveAnnotation(binding.symbol))) &&& Block (stats1, expr1) =?= Block (stats2, expr2)
328
331
329
332
/* Match block */
@@ -340,7 +343,7 @@ object Matcher {
340
343
341
344
case (scrutinee, Block (typeBindings, expr2)) if typeBindings.forall(isTypeBinding) =>
342
345
val bindingSymbols = typeBindings.map(_.symbol)
343
- qctx.tasty.internal. Constraints_add (summon[Context ])(bindingSymbols)
346
+ qctx.tasty.Constraints_add (summon[Context ])(bindingSymbols)
344
347
bindingSymbols.foldRight(scrutinee =?= expr2)((x, acc) => matched(new SymBinding (x, hasFromAboveAnnotation(x))) &&& acc)
345
348
346
349
/* Match if */
0 commit comments