Skip to content

Commit 20f5c5c

Browse files
committed
Take advantage of short circuiting && on Matchings
1 parent a5d6cb9 commit 20f5c5c

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

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

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -172,40 +172,33 @@ object Matcher {
172172
val bindMatch =
173173
if (hasBindAnnotation(pattern.symbol) || hasBindTypeAnnotation(tpt2)) bindingMatch(scrutinee.symbol)
174174
else matched
175-
val returnTptMatch = tpt1 =#= tpt2
176-
val rhsEnv = the[Env] + (scrutinee.symbol -> pattern.symbol)
177-
val rhsMatchings = treeOptMatches(rhs1, rhs2) given rhsEnv
178-
bindMatch && returnTptMatch && rhsMatchings
175+
def rhsEnv = the[Env] + (scrutinee.symbol -> pattern.symbol)
176+
bindMatch && tpt1 =#= tpt2 && (treeOptMatches(rhs1, rhs2) given rhsEnv)
179177

180178
case (DefDef(_, typeParams1, paramss1, tpt1, Some(rhs1)), DefDef(_, typeParams2, paramss2, tpt2, Some(rhs2))) =>
181-
val typeParmasMatch = typeParams1 =##= typeParams2
182-
val paramssMatch = matchLists(paramss1, paramss2)(_ =##= _)
183179
val bindMatch =
184180
if (hasBindAnnotation(pattern.symbol)) bindingMatch(scrutinee.symbol)
185181
else matched
186-
val tptMatch = tpt1 =#= tpt2
187-
val rhsEnv =
182+
def rhsEnv =
188183
the[Env] + (scrutinee.symbol -> pattern.symbol) ++
189184
typeParams1.zip(typeParams2).map((tparam1, tparam2) => tparam1.symbol -> tparam2.symbol) ++
190185
paramss1.flatten.zip(paramss2.flatten).map((param1, param2) => param1.symbol -> param2.symbol)
191-
val rhsMatch = (rhs1 =#= rhs2) given rhsEnv
192186

193-
bindMatch && typeParmasMatch && paramssMatch && tptMatch && rhsMatch
187+
bindMatch &&
188+
typeParams1 =##= typeParams2 &&
189+
matchLists(paramss1, paramss2)(_ =##= _) &&
190+
tpt1 =#= tpt2 &&
191+
withEnv(rhsEnv)(rhs1 =#= rhs2)
194192

195193
case (Lambda(_, tpt1), Lambda(_, tpt2)) =>
196194
// TODO match tpt1 with tpt2?
197195
matched
198196

199197
case (Match(scru1, cases1), Match(scru2, cases2)) =>
200-
val scrutineeMacth = scru1 =#= scru2
201-
val casesMatch = matchLists(cases1, cases2)(caseMatches)
202-
scrutineeMacth && casesMatch
198+
scru1 =#= scru2 && matchLists(cases1, cases2)(caseMatches)
203199

204200
case (Try(body1, cases1, finalizer1), Try(body2, cases2, finalizer2)) =>
205-
val bodyMacth = body1 =#= body2
206-
val casesMatch = matchLists(cases1, cases2)(caseMatches)
207-
val finalizerMatch = treeOptMatches(finalizer1, finalizer2)
208-
bodyMacth && casesMatch && finalizerMatch
201+
body1 =#= body2 && matchLists(cases1, cases2)(caseMatches) && treeOptMatches(finalizer1, finalizer2)
209202

210203
// Ignore type annotations
211204
case (Annotated(tpt, _), _) =>
@@ -247,9 +240,9 @@ object Matcher {
247240
def caseMatches(scrutinee: CaseDef, pattern: CaseDef) given Env: Matching = {
248241
val (caseEnv, patternMatch) = scrutinee.pattern =%= pattern.pattern
249242
withEnv(caseEnv) {
250-
val guardMatch = treeOptMatches(scrutinee.guard, pattern.guard)
251-
val rhsMatch = scrutinee.rhs =#= pattern.rhs
252-
patternMatch && guardMatch && rhsMatch
243+
patternMatch &&
244+
treeOptMatches(scrutinee.guard, pattern.guard) &&
245+
scrutinee.rhs =#= pattern.rhs
253246
}
254247
}
255248

@@ -276,10 +269,8 @@ object Matcher {
276269
(body1 =%= body2) given bindEnv
277270

278271
case (Pattern.Unapply(fun1, implicits1, patterns1), Pattern.Unapply(fun2, implicits2, patterns2)) =>
279-
val funMatch = fun1 =#= fun2
280-
val implicitsMatch = implicits1 =##= implicits2
281272
val (patEnv, patternsMatch) = foldPatterns(patterns1, patterns2)
282-
(patEnv, funMatch && implicitsMatch && patternsMatch)
273+
(patEnv, fun1 =#= fun2 && implicits1 =##= implicits2 && patternsMatch)
283274

284275
case (Pattern.Alternatives(patterns1), Pattern.Alternatives(patterns2)) =>
285276
foldPatterns(patterns1, patterns2)

0 commit comments

Comments
 (0)