Don't expose Usefulness in the api

This commit is contained in:
Nadrieril 2020-12-22 11:21:34 +00:00
parent 3a4c135a2f
commit 5547105f6b
2 changed files with 22 additions and 9 deletions

View file

@ -1,6 +1,6 @@
use super::usefulness::Usefulness::*;
use super::usefulness::{
compute_match_usefulness, expand_pattern, MatchArm, MatchCheckCtxt, UsefulnessReport,
compute_match_usefulness, expand_pattern, MatchArm, MatchCheckCtxt, Reachability,
UsefulnessReport,
};
use super::{PatCtxt, PatKind, PatternError};
@ -398,10 +398,11 @@ fn report_arm_reachability<'p, 'tcx>(
report: &UsefulnessReport<'p, 'tcx>,
source: hir::MatchSource,
) {
use Reachability::*;
let mut catchall = None;
for (arm_index, (arm, is_useful)) in report.arm_usefulness.iter().enumerate() {
match is_useful {
NotUseful => {
Unreachable => {
match source {
hir::MatchSource::WhileDesugar => bug!(),
@ -430,9 +431,9 @@ fn report_arm_reachability<'p, 'tcx>(
hir::MatchSource::AwaitDesugar | hir::MatchSource::TryDesugar => {}
}
}
Useful(unreachables) if unreachables.is_empty() => {}
Reachable(unreachables) if unreachables.is_empty() => {}
// The arm is reachable, but contains unreachable subpatterns (from or-patterns).
Useful(unreachables) => {
Reachable(unreachables) => {
let mut unreachables: Vec<_> = unreachables.iter().collect();
// Emit lints in the order in which they occur in the file.
unreachables.sort_unstable();
@ -440,7 +441,6 @@ fn report_arm_reachability<'p, 'tcx>(
unreachable_pattern(cx.tcx, span, arm.hir_id, None);
}
}
UsefulWithWitness(_) => bug!(),
}
if !arm.has_guard && catchall.is_none() && pat_is_catchall(arm.pat) {
catchall = Some(arm.pat.span);

View file

@ -679,7 +679,7 @@ impl SpanSet {
}
#[derive(Clone, Debug)]
crate enum Usefulness<'tcx> {
enum Usefulness<'tcx> {
/// Pontentially carries a set of sub-branches that have been found to be unreachable. Used
/// only in the presence of or-patterns, otherwise it stays empty.
Useful(SpanSet),
@ -1024,10 +1024,18 @@ crate struct MatchArm<'p, 'tcx> {
crate has_guard: bool,
}
#[derive(Clone, Debug)]
crate enum Reachability {
/// Potentially carries a set of sub-branches that have been found to be unreachable. Used only
/// in the presence of or-patterns, otherwise it stays empty.
Reachable(SpanSet),
Unreachable,
}
/// The output of checking a match for exhaustiveness and arm reachability.
crate struct UsefulnessReport<'p, 'tcx> {
/// For each arm of the input, whether that arm is reachable after the arms above it.
crate arm_usefulness: Vec<(MatchArm<'p, 'tcx>, Usefulness<'tcx>)>,
crate arm_usefulness: Vec<(MatchArm<'p, 'tcx>, Reachability)>,
/// If the match is exhaustive, this is empty. If not, this contains witnesses for the lack of
/// exhaustiveness.
crate non_exhaustiveness_witnesses: Vec<super::Pat<'tcx>>,
@ -1055,7 +1063,12 @@ crate fn compute_match_usefulness<'p, 'tcx>(
if !arm.has_guard {
matrix.push(v);
}
(arm, usefulness)
let reachability = match usefulness {
Useful(spans) => Reachability::Reachable(spans),
NotUseful => Reachability::Unreachable,
UsefulWithWitness(..) => bug!(),
};
(arm, reachability)
})
.collect();