From fe671966796e8bd7e468fcce86893d7a27af08bc Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 3 Nov 2019 17:10:33 +0000 Subject: [PATCH] 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. --- src/librustc_mir/hair/pattern/check_match.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/librustc_mir/hair/pattern/check_match.rs b/src/librustc_mir/hair/pattern/check_match.rs index 30586e95acf8..4463fb43583a 100644 --- a/src/librustc_mir/hair/pattern/check_match.rs +++ b/src/librustc_mir/hair/pattern/check_match.rs @@ -181,7 +181,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> { } // Fourth, check for unreachable arms. - check_arms(cx, &inlined_arms, source); + let matrix = check_arms(cx, &inlined_arms, source); // Then, if the match has no arms, check whether the scrutinee // is uninhabited. @@ -248,12 +248,6 @@ impl<'tcx> MatchVisitor<'_, 'tcx> { return; } - let matrix: Matrix<'_, '_> = inlined_arms - .iter() - .filter(|&&(_, guard)| guard.is_none()) - .flat_map(|arm| &arm.0) - .map(|pat| PatStack::from_pattern(pat.0)) - .collect(); let scrut_ty = self.tables.node_type(scrut.hir_id); check_exhaustive(cx, scrut_ty, scrut.span, &matrix, scrut.hir_id); }) @@ -403,11 +397,11 @@ fn pat_is_catchall(pat: &Pat) -> bool { } // Check for unreachable patterns -fn check_arms<'tcx>( +fn check_arms<'p, 'tcx>( cx: &mut MatchCheckCtxt<'_, 'tcx>, - arms: &[(Vec<(&super::Pat<'tcx>, &hir::Pat)>, Option<&hir::Expr>)], + arms: &[(Vec<(&'p super::Pat<'tcx>, &hir::Pat)>, Option<&hir::Expr>)], source: hir::MatchSource, -) { +) -> Matrix<'p, 'tcx> { let mut seen = Matrix::empty(); let mut catchall = None; for (arm_index, &(ref pats, guard)) in arms.iter().enumerate() { @@ -485,6 +479,7 @@ fn check_arms<'tcx>( } } } + seen } fn check_not_useful(