Skip to content

Commit efb28bd

Browse files
committed
Add a note with a link to explain empty types
1 parent 25964b5 commit efb28bd

18 files changed

+171
-17
lines changed

compiler/rustc_mir_build/messages.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ mir_build_unreachable_matches_same_values = matches some of the same values
332332
mir_build_unreachable_pattern = unreachable pattern
333333
.label = no value can reach this
334334
.unreachable_matches_no_values = matches no values because `{$ty}` is uninhabited
335+
.unreachable_uninhabited_note = to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
335336
.unreachable_covered_by_catchall = matches any value
336337
.unreachable_covered_by_one = matches all the relevant values
337338
.unreachable_covered_by_many = multiple earlier patterns match some of the same values

compiler/rustc_mir_build/src/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,8 @@ pub(crate) struct UnreachablePattern<'tcx> {
588588
pub(crate) span: Option<Span>,
589589
#[subdiagnostic]
590590
pub(crate) matches_no_values: Option<UnreachableMatchesNoValues<'tcx>>,
591+
#[note(mir_build_unreachable_uninhabited_note)]
592+
pub(crate) uninhabited_note: Option<()>,
591593
#[label(mir_build_unreachable_covered_by_catchall)]
592594
pub(crate) covered_by_catchall: Option<Span>,
593595
#[label(mir_build_unreachable_covered_by_one)]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,7 @@ fn report_unreachable_pattern<'p, 'tcx>(
921921
let mut lint = UnreachablePattern {
922922
span: Some(pat_span),
923923
matches_no_values: None,
924+
uninhabited_note: None,
924925
covered_by_catchall: None,
925926
covered_by_one: None,
926927
covered_by_many: None,
@@ -929,6 +930,7 @@ fn report_unreachable_pattern<'p, 'tcx>(
929930
[] => {
930931
// Empty pattern; we report the uninhabited type that caused the emptiness.
931932
lint.span = None; // Don't label the pattern itself
933+
lint.uninhabited_note = Some(()); // Give a link about empty types
932934
pat.walk(&mut |subpat| {
933935
let ty = **subpat.ty();
934936
if cx.is_uninhabited(ty) {

tests/ui/pattern/usefulness/empty-match-check-notes.exhaustive_patterns.stderr

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,42 @@ LL | _ => {}
55
| ^
66
|
77
= note: matches no values because `EmptyEnum` is uninhabited
8+
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
89
note: the lint level is defined here
910
--> $DIR/empty-match-check-notes.rs:7:9
1011
|
1112
LL | #![deny(unreachable_patterns)]
1213
| ^^^^^^^^^^^^^^^^^^^^
1314

1415
error: unreachable pattern
15-
--> $DIR/empty-match-check-notes.rs:21:9
16+
--> $DIR/empty-match-check-notes.rs:22:9
1617
|
1718
LL | _ if false => {}
1819
| ^
1920
|
2021
= note: matches no values because `EmptyEnum` is uninhabited
22+
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
2123

2224
error: unreachable pattern
23-
--> $DIR/empty-match-check-notes.rs:29:9
25+
--> $DIR/empty-match-check-notes.rs:31:9
2426
|
2527
LL | _ => {}
2628
| ^
2729
|
2830
= note: matches no values because `EmptyForeignEnum` is uninhabited
31+
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
2932

3033
error: unreachable pattern
31-
--> $DIR/empty-match-check-notes.rs:33:9
34+
--> $DIR/empty-match-check-notes.rs:36:9
3235
|
3336
LL | _ if false => {}
3437
| ^
3538
|
3639
= note: matches no values because `EmptyForeignEnum` is uninhabited
40+
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
3741

3842
error[E0005]: refutable pattern in local binding
39-
--> $DIR/empty-match-check-notes.rs:39:9
43+
--> $DIR/empty-match-check-notes.rs:43:9
4044
|
4145
LL | let None = *x;
4246
| ^^^^ pattern `Some(_)` not covered
@@ -51,7 +55,7 @@ LL | if let None = *x { todo!() };
5155
| ++ +++++++++++
5256

5357
error[E0004]: non-exhaustive patterns: `0_u8..=u8::MAX` not covered
54-
--> $DIR/empty-match-check-notes.rs:49:11
58+
--> $DIR/empty-match-check-notes.rs:53:11
5559
|
5660
LL | match 0u8 {
5761
| ^^^ pattern `0_u8..=u8::MAX` not covered

tests/ui/pattern/usefulness/empty-match-check-notes.normal.stderr

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,42 @@ LL | _ => {}
55
| ^
66
|
77
= note: matches no values because `EmptyEnum` is uninhabited
8+
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
89
note: the lint level is defined here
910
--> $DIR/empty-match-check-notes.rs:7:9
1011
|
1112
LL | #![deny(unreachable_patterns)]
1213
| ^^^^^^^^^^^^^^^^^^^^
1314

1415
error: unreachable pattern
15-
--> $DIR/empty-match-check-notes.rs:21:9
16+
--> $DIR/empty-match-check-notes.rs:22:9
1617
|
1718
LL | _ if false => {}
1819
| ^
1920
|
2021
= note: matches no values because `EmptyEnum` is uninhabited
22+
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
2123

2224
error: unreachable pattern
23-
--> $DIR/empty-match-check-notes.rs:29:9
25+
--> $DIR/empty-match-check-notes.rs:31:9
2426
|
2527
LL | _ => {}
2628
| ^
2729
|
2830
= note: matches no values because `EmptyForeignEnum` is uninhabited
31+
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
2932

3033
error: unreachable pattern
31-
--> $DIR/empty-match-check-notes.rs:33:9
34+
--> $DIR/empty-match-check-notes.rs:36:9
3235
|
3336
LL | _ if false => {}
3437
| ^
3538
|
3639
= note: matches no values because `EmptyForeignEnum` is uninhabited
40+
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
3741

3842
error[E0005]: refutable pattern in local binding
39-
--> $DIR/empty-match-check-notes.rs:39:9
43+
--> $DIR/empty-match-check-notes.rs:43:9
4044
|
4145
LL | let None = *x;
4246
| ^^^^ pattern `Some(_)` not covered
@@ -51,7 +55,7 @@ LL | if let None = *x { todo!() };
5155
| ++ +++++++++++
5256

5357
error[E0004]: non-exhaustive patterns: `0_u8..=u8::MAX` not covered
54-
--> $DIR/empty-match-check-notes.rs:49:11
58+
--> $DIR/empty-match-check-notes.rs:53:11
5559
|
5660
LL | match 0u8 {
5761
| ^^^ pattern `0_u8..=u8::MAX` not covered

tests/ui/pattern/usefulness/empty-match-check-notes.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ fn empty_enum(x: EmptyEnum) {
1616
match x {
1717
_ => {} //~ ERROR unreachable pattern
1818
//~^ NOTE matches no values
19+
//~| NOTE to learn more about uninhabited types, see
1920
}
2021
match x {
2122
_ if false => {} //~ ERROR unreachable pattern
2223
//~^ NOTE matches no values
24+
//~| NOTE to learn more about uninhabited types, see
2325
}
2426
}
2527

@@ -28,10 +30,12 @@ fn empty_foreign_enum(x: empty::EmptyForeignEnum) {
2830
match x {
2931
_ => {} //~ ERROR unreachable pattern
3032
//~^ NOTE matches no values
33+
//~| NOTE to learn more about uninhabited types, see
3134
}
3235
match x {
3336
_ if false => {} //~ ERROR unreachable pattern
3437
//~^ NOTE matches no values
38+
//~| NOTE to learn more about uninhabited types, see
3539
}
3640
}
3741

0 commit comments

Comments
 (0)