Skip to content

Commit 9bb17d3

Browse files
authored
Rollup merge of #129281 - Nadrieril:tweak-unreachable-lint-wording, r=estebank
Tweak unreachable lint wording Some tweaks to the notes added in #128034. r? `@estebank`
2 parents 7da4b2d + f30392a commit 9bb17d3

Some content is hidden

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

47 files changed

+737
-668
lines changed

compiler/rustc_mir_build/messages.ftl

+7-4
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,17 @@ mir_build_union_pattern = cannot use unions in constant patterns
327327
328328
mir_build_unreachable_making_this_unreachable = collectively making this unreachable
329329
330+
mir_build_unreachable_making_this_unreachable_n_more = ...and {$covered_by_many_n_more_count} other patterns collectively make this unreachable
331+
330332
mir_build_unreachable_matches_same_values = matches some of the same values
331333
332334
mir_build_unreachable_pattern = unreachable pattern
333-
.label = unreachable pattern
334-
.unreachable_matches_no_values = this pattern matches no values because `{$ty}` is uninhabited
335+
.label = no value can reach this
336+
.unreachable_matches_no_values = matches no values because `{$matches_no_values_ty}` is uninhabited
337+
.unreachable_uninhabited_note = to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
335338
.unreachable_covered_by_catchall = matches any value
336-
.unreachable_covered_by_one = matches all the values already
337-
.unreachable_covered_by_many = these patterns collectively make the last one unreachable
339+
.unreachable_covered_by_one = matches all the relevant values
340+
.unreachable_covered_by_many = multiple earlier patterns match some of the same values
338341
339342
mir_build_unsafe_fn_safe_body = an unsafe function restricts its caller, but its body is safe by default
340343
mir_build_unsafe_not_inherited = items do not inherit unsafety from separate enclosing items

compiler/rustc_mir_build/src/errors.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -586,20 +586,18 @@ pub(crate) struct NonConstPath {
586586
pub(crate) struct UnreachablePattern<'tcx> {
587587
#[label]
588588
pub(crate) span: Option<Span>,
589-
#[subdiagnostic]
590-
pub(crate) matches_no_values: Option<UnreachableMatchesNoValues<'tcx>>,
589+
#[label(mir_build_unreachable_matches_no_values)]
590+
pub(crate) matches_no_values: Option<Span>,
591+
pub(crate) matches_no_values_ty: Ty<'tcx>,
592+
#[note(mir_build_unreachable_uninhabited_note)]
593+
pub(crate) uninhabited_note: Option<()>,
591594
#[label(mir_build_unreachable_covered_by_catchall)]
592595
pub(crate) covered_by_catchall: Option<Span>,
593596
#[label(mir_build_unreachable_covered_by_one)]
594597
pub(crate) covered_by_one: Option<Span>,
595598
#[note(mir_build_unreachable_covered_by_many)]
596599
pub(crate) covered_by_many: Option<MultiSpan>,
597-
}
598-
599-
#[derive(Subdiagnostic)]
600-
#[note(mir_build_unreachable_matches_no_values)]
601-
pub(crate) struct UnreachableMatchesNoValues<'tcx> {
602-
pub(crate) ty: Ty<'tcx>,
600+
pub(crate) covered_by_many_n_more_count: usize,
603601
}
604602

605603
#[derive(Diagnostic)]

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -917,22 +917,28 @@ fn report_unreachable_pattern<'p, 'tcx>(
917917
pat: &DeconstructedPat<'p, 'tcx>,
918918
explanation: &RedundancyExplanation<'p, 'tcx>,
919919
) {
920+
static CAP_COVERED_BY_MANY: usize = 4;
920921
let pat_span = pat.data().span;
921922
let mut lint = UnreachablePattern {
922923
span: Some(pat_span),
923924
matches_no_values: None,
925+
matches_no_values_ty: **pat.ty(),
926+
uninhabited_note: None,
924927
covered_by_catchall: None,
925928
covered_by_one: None,
926929
covered_by_many: None,
930+
covered_by_many_n_more_count: 0,
927931
};
928932
match explanation.covered_by.as_slice() {
929933
[] => {
930934
// Empty pattern; we report the uninhabited type that caused the emptiness.
931935
lint.span = None; // Don't label the pattern itself
936+
lint.uninhabited_note = Some(()); // Give a link about empty types
937+
lint.matches_no_values = Some(pat_span);
932938
pat.walk(&mut |subpat| {
933939
let ty = **subpat.ty();
934940
if cx.is_uninhabited(ty) {
935-
lint.matches_no_values = Some(UnreachableMatchesNoValues { ty });
941+
lint.matches_no_values_ty = ty;
936942
false // No need to dig further.
937943
} else if matches!(subpat.ctor(), Constructor::Ref | Constructor::UnionField) {
938944
false // Don't explore further since they are not by-value.
@@ -948,15 +954,27 @@ fn report_unreachable_pattern<'p, 'tcx>(
948954
lint.covered_by_one = Some(covering_pat.data().span);
949955
}
950956
covering_pats => {
957+
let mut iter = covering_pats.iter();
951958
let mut multispan = MultiSpan::from_span(pat_span);
952-
for p in covering_pats {
959+
for p in iter.by_ref().take(CAP_COVERED_BY_MANY) {
953960
multispan.push_span_label(
954961
p.data().span,
955962
fluent::mir_build_unreachable_matches_same_values,
956963
);
957964
}
958-
multispan
959-
.push_span_label(pat_span, fluent::mir_build_unreachable_making_this_unreachable);
965+
let remain = iter.count();
966+
if remain == 0 {
967+
multispan.push_span_label(
968+
pat_span,
969+
fluent::mir_build_unreachable_making_this_unreachable,
970+
);
971+
} else {
972+
lint.covered_by_many_n_more_count = remain;
973+
multispan.push_span_label(
974+
pat_span,
975+
fluent::mir_build_unreachable_making_this_unreachable_n_more,
976+
);
977+
}
960978
lint.covered_by_many = Some(multispan);
961979
}
962980
}

tests/ui/consts/packed_pattern.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ warning: unreachable pattern
22
--> $DIR/packed_pattern.rs:16:9
33
|
44
LL | Foo { field: (5, 6, 7, 8) } => {},
5-
| --------------------------- matches all the values already
5+
| --------------------------- matches all the relevant values
66
LL | FOO => unreachable!(),
7-
| ^^^ unreachable pattern
7+
| ^^^ no value can reach this
88
|
99
= note: `#[warn(unreachable_patterns)]` on by default
1010

tests/ui/consts/packed_pattern2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ warning: unreachable pattern
22
--> $DIR/packed_pattern2.rs:24:9
33
|
44
LL | Bar { a: Foo { field: (5, 6) } } => {},
5-
| -------------------------------- matches all the values already
5+
| -------------------------------- matches all the relevant values
66
LL | FOO => unreachable!(),
7-
| ^^^ unreachable pattern
7+
| ^^^ no value can reach this
88
|
99
= note: `#[warn(unreachable_patterns)]` on by default
1010

tests/ui/error-codes/E0001.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ error: unreachable pattern
22
--> $DIR/E0001.rs:8:9
33
|
44
LL | _ => {/* ... */}
5-
| ^ unreachable pattern
5+
| ^ no value can reach this
66
|
7-
note: these patterns collectively make the last one unreachable
7+
note: multiple earlier patterns match some of the same values
88
--> $DIR/E0001.rs:8:9
99
|
1010
LL | Some(_) => {/* ... */}

tests/ui/lint/issue-30302.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | Nil => true,
1313
| --- matches any value
1414
LL |
1515
LL | _ => false
16-
| ^ unreachable pattern
16+
| ^ no value can reach this
1717
|
1818
note: the lint level is defined here
1919
--> $DIR/issue-30302.rs:4:9

0 commit comments

Comments
 (0)