Skip to content

Commit

Permalink
Don't build the same matrix twice
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Nadrieril committed Dec 2, 2019
1 parent 2da942f commit fe67196
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/librustc_mir/hair/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
})
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -485,6 +479,7 @@ fn check_arms<'tcx>(
}
}
}
seen
}

fn check_not_useful(
Expand Down

0 comments on commit fe67196

Please sign in to comment.