Skip to content

Commit fbc2f03

Browse files
committed
Explain why a given pattern is considered unreachable
1 parent 9f15c93 commit fbc2f03

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1283
-263
lines changed

Diff for: compiler/rustc_mir_build/messages.ftl

+4-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,10 @@ mir_build_union_pattern = cannot use unions in constant patterns
327327
328328
mir_build_unreachable_pattern = unreachable pattern
329329
.label = unreachable pattern
330-
.catchall_label = matches any value
330+
.unreachable_matches_no_values = this pattern matches no values because `{$ty}` is uninhabited
331+
.unreachable_covered_by_catchall = matches any value
332+
.unreachable_covered_by_one = matches all the values already
333+
.unreachable_covered_by_many = matches some of the same values
331334
332335
mir_build_unsafe_fn_safe_body = an unsafe function restricts its caller, but its body is safe by default
333336
mir_build_unsafe_not_inherited = items do not inherit unsafety from separate enclosing items

Diff for: compiler/rustc_mir_build/src/errors.rs

+29-3
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,37 @@ pub(crate) struct NonConstPath {
582582

583583
#[derive(LintDiagnostic)]
584584
#[diag(mir_build_unreachable_pattern)]
585-
pub(crate) struct UnreachablePattern {
585+
pub(crate) struct UnreachablePattern<'tcx> {
586586
#[label]
587587
pub(crate) span: Option<Span>,
588-
#[label(mir_build_catchall_label)]
589-
pub(crate) catchall: Option<Span>,
588+
#[subdiagnostic]
589+
pub(crate) matches_no_values: Option<UnreachableMatchesNoValues<'tcx>>,
590+
#[label(mir_build_unreachable_covered_by_catchall)]
591+
pub(crate) covered_by_catchall: Option<Span>,
592+
#[label(mir_build_unreachable_covered_by_one)]
593+
pub(crate) covered_by_one: Option<Span>,
594+
#[subdiagnostic]
595+
pub(crate) covered_by_many: Option<UnreachableCoveredByMany>,
596+
}
597+
598+
#[derive(Subdiagnostic)]
599+
#[note(mir_build_unreachable_matches_no_values)]
600+
pub(crate) struct UnreachableMatchesNoValues<'tcx> {
601+
pub(crate) ty: Ty<'tcx>,
602+
}
603+
604+
pub(crate) struct UnreachableCoveredByMany(pub(crate) Vec<Span>);
605+
606+
impl Subdiagnostic for UnreachableCoveredByMany {
607+
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
608+
self,
609+
diag: &mut Diag<'_, G>,
610+
_f: &F,
611+
) {
612+
for span in self.0 {
613+
diag.span_label(span, fluent::mir_build_unreachable_covered_by_many);
614+
}
615+
}
590616
}
591617

592618
#[derive(Diagnostic)]

Diff for: compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+45-19
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
1616
use rustc_middle::ty::{self, AdtDef, Ty, TyCtxt};
1717
use rustc_pattern_analysis::errors::Uncovered;
1818
use rustc_pattern_analysis::rustc::{
19-
Constructor, DeconstructedPat, MatchArm, RustcPatCtxt as PatCtxt, Usefulness, UsefulnessReport,
20-
WitnessPat,
19+
Constructor, DeconstructedPat, MatchArm, RedundancyExplanation, RustcPatCtxt as PatCtxt,
20+
Usefulness, UsefulnessReport, WitnessPat,
2121
};
2222
use rustc_session::lint::builtin::{
2323
BINDINGS_WITH_VARIANT_NAME, IRREFUTABLE_LET_PATTERNS, UNREACHABLE_PATTERNS,
@@ -409,9 +409,9 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
409409
{
410410
let mut redundant_subpats = redundant_subpats.clone();
411411
// Emit lints in the order in which they occur in the file.
412-
redundant_subpats.sort_unstable_by_key(|pat| pat.data().span);
413-
for pat in redundant_subpats {
414-
report_unreachable_pattern(cx, arm.arm_data, pat.data().span, None)
412+
redundant_subpats.sort_unstable_by_key(|(pat, _)| pat.data().span);
413+
for (pat, explanation) in redundant_subpats {
414+
report_unreachable_pattern(cx, arm.arm_data, pat, &explanation)
415415
}
416416
}
417417
}
@@ -910,26 +910,52 @@ fn report_irrefutable_let_patterns(
910910
fn report_unreachable_pattern<'p, 'tcx>(
911911
cx: &PatCtxt<'p, 'tcx>,
912912
hir_id: HirId,
913-
span: Span,
914-
catchall: Option<Span>,
913+
pat: &DeconstructedPat<'p, 'tcx>,
914+
explanation: &RedundancyExplanation<'p, 'tcx>,
915915
) {
916-
cx.tcx.emit_node_span_lint(
917-
UNREACHABLE_PATTERNS,
918-
hir_id,
919-
span,
920-
UnreachablePattern { span: if catchall.is_some() { Some(span) } else { None }, catchall },
921-
);
916+
let pat_span = pat.data().span;
917+
let mut lint = UnreachablePattern {
918+
span: Some(pat_span),
919+
matches_no_values: None,
920+
covered_by_catchall: None,
921+
covered_by_one: None,
922+
covered_by_many: None,
923+
};
924+
match explanation.covered_by.as_slice() {
925+
[] => {
926+
// Empty pattern; we report the uninhabited type that caused the emptiness.
927+
lint.span = None; // Don't label the pattern itself
928+
pat.walk(&mut |subpat| {
929+
let ty = **subpat.ty();
930+
if cx.is_uninhabited(ty) {
931+
lint.matches_no_values = Some(UnreachableMatchesNoValues { ty });
932+
false // No need to dig further.
933+
} else if matches!(subpat.ctor(), Constructor::Ref | Constructor::UnionField) {
934+
false // Don't explore further since they are not by-value.
935+
} else {
936+
true
937+
}
938+
});
939+
}
940+
[covering_pat] if pat_is_catchall(covering_pat) => {
941+
lint.covered_by_catchall = Some(covering_pat.data().span);
942+
}
943+
[covering_pat] => {
944+
lint.covered_by_one = Some(covering_pat.data().span);
945+
}
946+
covering_pats => {
947+
let covering_spans = covering_pats.iter().map(|p| p.data().span).collect();
948+
lint.covered_by_many = Some(UnreachableCoveredByMany(covering_spans));
949+
}
950+
}
951+
cx.tcx.emit_node_span_lint(UNREACHABLE_PATTERNS, hir_id, pat_span, lint);
922952
}
923953

924954
/// Report unreachable arms, if any.
925955
fn report_arm_reachability<'p, 'tcx>(cx: &PatCtxt<'p, 'tcx>, report: &UsefulnessReport<'p, 'tcx>) {
926-
let mut catchall = None;
927956
for (arm, is_useful) in report.arm_usefulness.iter() {
928-
if matches!(is_useful, Usefulness::Redundant) {
929-
report_unreachable_pattern(cx, arm.arm_data, arm.pat.data().span, catchall)
930-
}
931-
if !arm.has_guard && catchall.is_none() && pat_is_catchall(arm.pat) {
932-
catchall = Some(arm.pat.data().span);
957+
if let Usefulness::Redundant(explanation) = is_useful {
958+
report_unreachable_pattern(cx, arm.arm_data, arm.pat, explanation)
933959
}
934960
}
935961
}

Diff for: compiler/rustc_pattern_analysis/src/pat.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{PatCx, PrivateUninhabitedField};
1111
use self::Constructor::*;
1212

1313
/// A globally unique id to distinguish patterns.
14-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
14+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1515
pub(crate) struct PatId(u32);
1616
impl PatId {
1717
fn new() -> Self {
@@ -147,6 +147,21 @@ impl<Cx: PatCx> fmt::Debug for DeconstructedPat<Cx> {
147147
}
148148
}
149149

150+
/// Delegate to `uid`.
151+
impl<Cx: PatCx> PartialEq for DeconstructedPat<Cx> {
152+
fn eq(&self, other: &Self) -> bool {
153+
self.uid == other.uid
154+
}
155+
}
156+
/// Delegate to `uid`.
157+
impl<Cx: PatCx> Eq for DeconstructedPat<Cx> {}
158+
/// Delegate to `uid`.
159+
impl<Cx: PatCx> std::hash::Hash for DeconstructedPat<Cx> {
160+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
161+
self.uid.hash(state);
162+
}
163+
}
164+
150165
/// Represents either a pattern obtained from user input or a wildcard constructed during the
151166
/// algorithm. Do not use `Wild` to represent a wildcard pattern comping from user input.
152167
///

Diff for: compiler/rustc_pattern_analysis/src/rustc.rs

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub type Constructor<'p, 'tcx> = crate::constructor::Constructor<RustcPatCtxt<'p
3232
pub type ConstructorSet<'p, 'tcx> = crate::constructor::ConstructorSet<RustcPatCtxt<'p, 'tcx>>;
3333
pub type DeconstructedPat<'p, 'tcx> = crate::pat::DeconstructedPat<RustcPatCtxt<'p, 'tcx>>;
3434
pub type MatchArm<'p, 'tcx> = crate::MatchArm<'p, RustcPatCtxt<'p, 'tcx>>;
35+
pub type RedundancyExplanation<'p, 'tcx> =
36+
crate::usefulness::RedundancyExplanation<'p, RustcPatCtxt<'p, 'tcx>>;
3537
pub type Usefulness<'p, 'tcx> = crate::usefulness::Usefulness<'p, RustcPatCtxt<'p, 'tcx>>;
3638
pub type UsefulnessReport<'p, 'tcx> =
3739
crate::usefulness::UsefulnessReport<'p, RustcPatCtxt<'p, 'tcx>>;

0 commit comments

Comments
 (0)