Skip to content

Commit fe67196

Browse files
committed
Don't build the same matrix twice
The exact same logic was used in check_arms and check_match to build the matrix of relevant patterns. It would actually probably have been a bug if it was not the case, since exhaustiveness checking should be the same as checking reachability of an additional `_ => ...` match branch.
1 parent 2da942f commit fe67196

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
181181
}
182182

183183
// Fourth, check for unreachable arms.
184-
check_arms(cx, &inlined_arms, source);
184+
let matrix = check_arms(cx, &inlined_arms, source);
185185

186186
// Then, if the match has no arms, check whether the scrutinee
187187
// is uninhabited.
@@ -248,12 +248,6 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
248248
return;
249249
}
250250

251-
let matrix: Matrix<'_, '_> = inlined_arms
252-
.iter()
253-
.filter(|&&(_, guard)| guard.is_none())
254-
.flat_map(|arm| &arm.0)
255-
.map(|pat| PatStack::from_pattern(pat.0))
256-
.collect();
257251
let scrut_ty = self.tables.node_type(scrut.hir_id);
258252
check_exhaustive(cx, scrut_ty, scrut.span, &matrix, scrut.hir_id);
259253
})
@@ -403,11 +397,11 @@ fn pat_is_catchall(pat: &Pat) -> bool {
403397
}
404398

405399
// Check for unreachable patterns
406-
fn check_arms<'tcx>(
400+
fn check_arms<'p, 'tcx>(
407401
cx: &mut MatchCheckCtxt<'_, 'tcx>,
408-
arms: &[(Vec<(&super::Pat<'tcx>, &hir::Pat)>, Option<&hir::Expr>)],
402+
arms: &[(Vec<(&'p super::Pat<'tcx>, &hir::Pat)>, Option<&hir::Expr>)],
409403
source: hir::MatchSource,
410-
) {
404+
) -> Matrix<'p, 'tcx> {
411405
let mut seen = Matrix::empty();
412406
let mut catchall = None;
413407
for (arm_index, &(ref pats, guard)) in arms.iter().enumerate() {
@@ -485,6 +479,7 @@ fn check_arms<'tcx>(
485479
}
486480
}
487481
}
482+
seen
488483
}
489484

490485
fn check_not_useful(

0 commit comments

Comments
 (0)