Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 49ee7bc

Browse files
committedJul 21, 2024
Auto merge of #128034 - Nadrieril:explain-unreachable, r=<try>
exhaustiveness: Explain why a given pattern is considered unreachable This PR tells the user why a given pattern is considered unreachable. I reused the intersection information we were already computing; even though it's incomplete I convinced myself that it is sufficient to always get a set of patterns that cover the unreachable one. I'm not a fan of the diagnostic messages I came up with, I'm open to suggestions. Fixes #127870. This is also the other one of the two diagnostic improvements I wanted to do before #122792. Note: the first commit is #128015, the second is an unrelated drive-by tweak. r? `@compiler-errors`
2 parents 92c6c03 + fbc2f03 commit 49ee7bc

File tree

53 files changed

+1468
-400
lines changed

Some content is hidden

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

53 files changed

+1468
-400
lines changed
 

‎compiler/rustc_mir_build/messages.ftl

Lines changed: 4 additions & 1 deletion
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

‎compiler/rustc_mir_build/src/errors.rs

Lines changed: 29 additions & 3 deletions
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)]

0 commit comments

Comments
 (0)
Please sign in to comment.