Skip to content

Commit

Permalink
Rollup merge of #129281 - Nadrieril:tweak-unreachable-lint-wording, r…
Browse files Browse the repository at this point in the history
…=estebank

Tweak unreachable lint wording

Some tweaks to the notes added in #128034.

r? `@estebank`
  • Loading branch information
matthiaskrgr authored Aug 21, 2024
2 parents 7da4b2d + f30392a commit 9bb17d3
Show file tree
Hide file tree
Showing 47 changed files with 737 additions and 668 deletions.
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 `{$matches_no_values_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
14 changes: 6 additions & 8 deletions compiler/rustc_mir_build/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,20 +586,18 @@ pub(crate) struct NonConstPath {
pub(crate) struct UnreachablePattern<'tcx> {
#[label]
pub(crate) span: Option<Span>,
#[subdiagnostic]
pub(crate) matches_no_values: Option<UnreachableMatchesNoValues<'tcx>>,
#[label(mir_build_unreachable_matches_no_values)]
pub(crate) matches_no_values: Option<Span>,
pub(crate) matches_no_values_ty: Ty<'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>,
}

#[derive(Subdiagnostic)]
#[note(mir_build_unreachable_matches_no_values)]
pub(crate) struct UnreachableMatchesNoValues<'tcx> {
pub(crate) ty: Ty<'tcx>,
pub(crate) covered_by_many_n_more_count: usize,
}

#[derive(Diagnostic)]
Expand Down
26 changes: 22 additions & 4 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,22 +917,28 @@ 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,
matches_no_values_ty: **pat.ty(),
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
lint.matches_no_values = Some(pat_span);
pat.walk(&mut |subpat| {
let ty = **subpat.ty();
if cx.is_uninhabited(ty) {
lint.matches_no_values = Some(UnreachableMatchesNoValues { ty });
lint.matches_no_values_ty = ty;
false // No need to dig further.
} else if matches!(subpat.ctor(), Constructor::Ref | Constructor::UnionField) {
false // Don't explore further since they are not by-value.
Expand All @@ -948,15 +954,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

0 comments on commit 9bb17d3

Please sign in to comment.