Skip to content

Commit a5d6cb9

Browse files
committed
Use same logic to match over all lists
1 parent fbc9d0a commit a5d6cb9

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

library/src-3.x/scala/internal/quoted/Matcher.scala

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,17 @@ object Matcher {
3939

4040
inline def withEnv[T](env: Env)(body: => given Env => T): T = body given env
4141

42-
/** Check that all trees match with =#= and concatenate the results with && */
43-
def (scrutinees: List[Tree]) =##= (patterns: List[Tree]) given Env: Matching = {
44-
def rec(l1: List[Tree], l2: List[Tree]): Matching = (l1, l2) match {
45-
case (x :: xs, y :: ys) => x =#= y && rec(xs, ys)
46-
case (Nil, Nil) => matched
47-
case _ => notMatched
48-
}
49-
rec(scrutinees, patterns)
42+
/** Check that all trees match with `mtch` and concatenate the results with && */
43+
def matchLists[T](l1: List[T], l2: List[T])(mtch: (T, T) => Matching): Matching = (l1, l2) match {
44+
case (x :: xs, y :: ys) => mtch(x, y) && matchLists(xs, ys)(mtch)
45+
case (Nil, Nil) => matched
46+
case _ => notMatched
5047
}
5148

49+
/** Check that all trees match with =#= and concatenate the results with && */
50+
def (scrutinees: List[Tree]) =##= (patterns: List[Tree]) given Env: Matching =
51+
matchLists(scrutinees, patterns)(_ =#= _)
52+
5253
/** Check that the trees match and return the contents from the pattern holes.
5354
* Return None if the trees do not match otherwise return Some of a tuple containing all the contents in the holes.
5455
*
@@ -178,9 +179,7 @@ object Matcher {
178179

179180
case (DefDef(_, typeParams1, paramss1, tpt1, Some(rhs1)), DefDef(_, typeParams2, paramss2, tpt2, Some(rhs2))) =>
180181
val typeParmasMatch = typeParams1 =##= typeParams2
181-
val paramssMatch =
182-
if (paramss1.size != paramss2.size) notMatched
183-
else foldMatchings(paramss1.zip(paramss2).map { (params1, params2) => params1 =##= params2 }: _*)
182+
val paramssMatch = matchLists(paramss1, paramss2)(_ =##= _)
184183
val bindMatch =
185184
if (hasBindAnnotation(pattern.symbol)) bindingMatch(scrutinee.symbol)
186185
else matched
@@ -199,16 +198,12 @@ object Matcher {
199198

200199
case (Match(scru1, cases1), Match(scru2, cases2)) =>
201200
val scrutineeMacth = scru1 =#= scru2
202-
val casesMatch =
203-
if (cases1.size != cases2.size) notMatched
204-
else foldMatchings(cases1.zip(cases2).map(caseMatches): _*)
201+
val casesMatch = matchLists(cases1, cases2)(caseMatches)
205202
scrutineeMacth && casesMatch
206203

207204
case (Try(body1, cases1, finalizer1), Try(body2, cases2, finalizer2)) =>
208205
val bodyMacth = body1 =#= body2
209-
val casesMatch =
210-
if (cases1.size != cases2.size) notMatched
211-
else foldMatchings(cases1.zip(cases2).map(caseMatches): _*)
206+
val casesMatch = matchLists(cases1, cases2)(caseMatches)
212207
val finalizerMatch = treeOptMatches(finalizer1, finalizer2)
213208
bodyMacth && casesMatch && finalizerMatch
214209

@@ -282,9 +277,7 @@ object Matcher {
282277

283278
case (Pattern.Unapply(fun1, implicits1, patterns1), Pattern.Unapply(fun2, implicits2, patterns2)) =>
284279
val funMatch = fun1 =#= fun2
285-
val implicitsMatch =
286-
if (implicits1.size != implicits2.size) notMatched
287-
else foldMatchings(implicits1.zip(implicits2).map((i1, i2) => i1 =#= i2): _*)
280+
val implicitsMatch = implicits1 =##= implicits2
288281
val (patEnv, patternsMatch) = foldPatterns(patterns1, patterns2)
289282
(patEnv, funMatch && implicitsMatch && patternsMatch)
290283

0 commit comments

Comments
 (0)