Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions compiler/rustc_mir_build/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,17 @@ mir_build_union_pattern = cannot use unions in constant patterns

mir_build_unreachable_making_this_unreachable = collectively making this unreachable

mir_build_unreachable_making_this_unreachable_n_more = ...and {$covered_by_many_n_more_count} other patterns collectively make this unreachable

mir_build_unreachable_matches_same_values = matches some of the same values

mir_build_unreachable_pattern = unreachable pattern
.label = unreachable pattern
.unreachable_matches_no_values = this pattern matches no values because `{$ty}` is uninhabited
.label = no value can reach this
.unreachable_matches_no_values = matches no values because `{$ty}` is uninhabited
.unreachable_uninhabited_note = to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
.unreachable_covered_by_catchall = matches any value
.unreachable_covered_by_one = matches all the values already
.unreachable_covered_by_many = these patterns collectively make the last one unreachable
.unreachable_covered_by_one = matches all the relevant values
.unreachable_covered_by_many = multiple earlier patterns match some of the same values

mir_build_unsafe_fn_safe_body = an unsafe function restricts its caller, but its body is safe by default
mir_build_unsafe_not_inherited = items do not inherit unsafety from separate enclosing items
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_mir_build/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,12 +588,15 @@ pub(crate) struct UnreachablePattern<'tcx> {
pub(crate) span: Option<Span>,
#[subdiagnostic]
pub(crate) matches_no_values: Option<UnreachableMatchesNoValues<'tcx>>,
#[note(mir_build_unreachable_uninhabited_note)]
pub(crate) uninhabited_note: Option<()>,
#[label(mir_build_unreachable_covered_by_catchall)]
pub(crate) covered_by_catchall: Option<Span>,
#[label(mir_build_unreachable_covered_by_one)]
pub(crate) covered_by_one: Option<Span>,
#[note(mir_build_unreachable_covered_by_many)]
pub(crate) covered_by_many: Option<MultiSpan>,
pub(crate) covered_by_many_n_more_count: usize,
}

#[derive(Subdiagnostic)]
Expand Down
22 changes: 19 additions & 3 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,18 +917,22 @@ fn report_unreachable_pattern<'p, 'tcx>(
pat: &DeconstructedPat<'p, 'tcx>,
explanation: &RedundancyExplanation<'p, 'tcx>,
) {
static CAP_COVERED_BY_MANY: usize = 4;
let pat_span = pat.data().span;
let mut lint = UnreachablePattern {
span: Some(pat_span),
matches_no_values: None,
uninhabited_note: None,
covered_by_catchall: None,
covered_by_one: None,
covered_by_many: None,
covered_by_many_n_more_count: 0,
};
match explanation.covered_by.as_slice() {
[] => {
// Empty pattern; we report the uninhabited type that caused the emptiness.
lint.span = None; // Don't label the pattern itself
lint.uninhabited_note = Some(()); // Give a link about empty types
pat.walk(&mut |subpat| {
let ty = **subpat.ty();
if cx.is_uninhabited(ty) {
Expand All @@ -948,15 +952,27 @@ fn report_unreachable_pattern<'p, 'tcx>(
lint.covered_by_one = Some(covering_pat.data().span);
}
covering_pats => {
let mut iter = covering_pats.iter();
let mut multispan = MultiSpan::from_span(pat_span);
for p in covering_pats {
for p in iter.by_ref().take(CAP_COVERED_BY_MANY) {
multispan.push_span_label(
p.data().span,
fluent::mir_build_unreachable_matches_same_values,
);
}
multispan
.push_span_label(pat_span, fluent::mir_build_unreachable_making_this_unreachable);
let remain = iter.count();
if remain == 0 {
multispan.push_span_label(
pat_span,
fluent::mir_build_unreachable_making_this_unreachable,
);
} else {
lint.covered_by_many_n_more_count = remain;
multispan.push_span_label(
pat_span,
fluent::mir_build_unreachable_making_this_unreachable_n_more,
);
}
lint.covered_by_many = Some(multispan);
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/consts/packed_pattern.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ warning: unreachable pattern
--> $DIR/packed_pattern.rs:16:9
|
LL | Foo { field: (5, 6, 7, 8) } => {},
| --------------------------- matches all the values already
| --------------------------- matches all the relevant values
LL | FOO => unreachable!(),
| ^^^ unreachable pattern
| ^^^ no value can reach this
|
= note: `#[warn(unreachable_patterns)]` on by default

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/consts/packed_pattern2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ warning: unreachable pattern
--> $DIR/packed_pattern2.rs:24:9
|
LL | Bar { a: Foo { field: (5, 6) } } => {},
| -------------------------------- matches all the values already
| -------------------------------- matches all the relevant values
LL | FOO => unreachable!(),
| ^^^ unreachable pattern
| ^^^ no value can reach this
|
= note: `#[warn(unreachable_patterns)]` on by default

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/error-codes/E0001.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ error: unreachable pattern
--> $DIR/E0001.rs:8:9
|
LL | _ => {/* ... */}
| ^ unreachable pattern
| ^ no value can reach this
|
note: these patterns collectively make the last one unreachable
note: multiple earlier patterns match some of the same values
--> $DIR/E0001.rs:8:9
|
LL | Some(_) => {/* ... */}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/lint/issue-30302.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ LL | Nil => true,
| --- matches any value
LL |
LL | _ => false
| ^ unreachable pattern
| ^ no value can reach this
|
note: the lint level is defined here
--> $DIR/issue-30302.rs:4:9
Expand Down
Loading