@@ -16,6 +16,13 @@ private[quoted] object Matcher {
16
16
import qctx .tasty .{_ , given }
17
17
import Matching ._
18
18
19
+ /** A map relating equivalent symbols from the scrutinee and the pattern
20
+ * For example in
21
+ * ```
22
+ * '{val a = 4; a * a} match case '{ val x = 4; x * x }
23
+ * ```
24
+ * when matching `a * a` with `x * x` the enviroment will contain `Map(a -> x)`.
25
+ */
19
26
private type Env = Map [Symbol , Symbol ]
20
27
21
28
inline private def withEnv [T ](env : Env )(body : => (given Env ) => T ): T = body(given env )
@@ -139,7 +146,7 @@ private[quoted] object Matcher {
139
146
matched(scrutinee.seal)
140
147
141
148
// Match a scala.internal.Quoted.patternHole and return the scrutinee tree
142
- case (ClosedTerm (scrutinee), TypeApply (patternHole, tpt :: Nil ))
149
+ case (ClosedPatternTerm (scrutinee), TypeApply (patternHole, tpt :: Nil ))
143
150
if patternHole.symbol == internal.Definitions_InternalQuoted_patternHole &&
144
151
scrutinee.tpe <:< tpt.tpe =>
145
152
matched(scrutinee.seal)
@@ -301,18 +308,20 @@ private[quoted] object Matcher {
301
308
}
302
309
end treeOps
303
310
304
- private object ClosedTerm {
311
+ private object ClosedPatternTerm {
312
+ /** Matches a term that does not contain free variables defined in the pattern (i.e. not defined in `Env`) */
305
313
def unapply (term : Term )(given Context , Env ): Option [term.type ] =
306
- if freeVars (term).isEmpty then Some (term) else None
314
+ if freePatternVars (term).isEmpty then Some (term) else None
307
315
308
- def freeVars (tree : Tree )(given qctx : Context , env : Env ): Set [Symbol ] =
316
+ /** Return all free variables of the term defined in the pattern (i.e. defined in `Env`) */
317
+ def freePatternVars (term : Term )(given qctx : Context , env : Env ): Set [Symbol ] =
309
318
val accumulator = new TreeAccumulator [Set [Symbol ]] {
310
319
def foldTree (x : Set [Symbol ], tree : Tree )(given ctx : Context ): Set [Symbol ] =
311
320
tree match
312
321
case tree : Ident if env.contains(tree.symbol) => foldOverTree(x + tree.symbol, tree)
313
322
case _ => foldOverTree(x, tree)
314
323
}
315
- accumulator.foldTree(Set .empty, tree )
324
+ accumulator.foldTree(Set .empty, term )
316
325
}
317
326
318
327
private object IdentArgs {
0 commit comments