Skip to content

Commit dcbe85a

Browse files
committed
Explain exhaustive matching on {usize,isize} maximum values
1 parent 0cd7ff7 commit dcbe85a

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

src/librustc_mir_build/hair/pattern/check_match.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,14 +468,14 @@ fn check_exhaustive<'p, 'tcx>(
468468
// In the case of an empty match, replace the '`_` not covered' diagnostic with something more
469469
// informative.
470470
let mut err;
471+
let joined_patterns = joined_uncovered_patterns(&witnesses);
471472
if is_empty_match && !non_empty_enum {
472473
err = create_e0004(
473474
cx.tcx.sess,
474475
sp,
475476
format!("non-exhaustive patterns: type `{}` is non-empty", scrut_ty),
476477
);
477478
} else {
478-
let joined_patterns = joined_uncovered_patterns(&witnesses);
479479
err = create_e0004(
480480
cx.tcx.sess,
481481
sp,
@@ -490,6 +490,14 @@ fn check_exhaustive<'p, 'tcx>(
490490
possibly by adding wildcards or more match arms",
491491
);
492492
err.note(&format!("the matched value is of type `{}`", scrut_ty));
493+
if (scrut_ty == cx.tcx.types.usize || scrut_ty == cx.tcx.types.isize)
494+
&& joined_patterns == "`_`"
495+
{
496+
err.note("for `usize` and `isize`, no assumptions about the maximum value are permitted");
497+
err.note(
498+
"to exhaustively match on either pointer-size integer type, wildcards must be used",
499+
);
500+
}
493501
err.emit();
494502
}
495503

src/test/ui/feature-gates/feature-gate-precise_pointer_size_matching.stderr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LL | match 0usize {
66
|
77
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
88
= note: the matched value is of type `usize`
9+
= note: for `usize` and `isize`, no assumptions about the maximum value are permitted
10+
= note: to exhaustively match on either pointer-size integer type, wildcards must be used
911

1012
error[E0004]: non-exhaustive patterns: `_` not covered
1113
--> $DIR/feature-gate-precise_pointer_size_matching.rs:10:11
@@ -15,6 +17,8 @@ LL | match 0isize {
1517
|
1618
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1719
= note: the matched value is of type `isize`
20+
= note: for `usize` and `isize`, no assumptions about the maximum value are permitted
21+
= note: to exhaustively match on either pointer-size integer type, wildcards must be used
1822

1923
error: aborting due to 2 previous errors
2024

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::{usize, isize};
2+
3+
fn main() {
4+
match 0usize {
5+
//~^ ERROR non-exhaustive patterns
6+
//~| NOTE pattern `_` not covered
7+
//~| NOTE the matched value is of type `usize`
8+
//~| NOTE for `usize` and `isize`, no assumptions about the maximum value are permitted
9+
//~| NOTE to exhaustively match on either pointer-size integer type, wildcards must be used
10+
0 ..= usize::MAX => {}
11+
}
12+
13+
match 0isize {
14+
//~^ ERROR non-exhaustive patterns
15+
//~| NOTE pattern `_` not covered
16+
//~| NOTE the matched value is of type `isize`
17+
//~| NOTE for `usize` and `isize`, no assumptions about the maximum value are permitted
18+
//~| NOTE to exhaustively match on either pointer-size integer type, wildcards must be used
19+
isize::MIN ..= isize::MAX => {}
20+
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0004]: non-exhaustive patterns: `_` not covered
2+
--> $DIR/non-exhaustive-pattern-pointer-size-int.rs:4:11
3+
|
4+
LL | match 0usize {
5+
| ^^^^^^ pattern `_` not covered
6+
|
7+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
8+
= note: the matched value is of type `usize`
9+
= note: for `usize` and `isize`, no assumptions about the maximum value are permitted
10+
= note: to exhaustively match on either pointer-size integer type, wildcards must be used
11+
12+
error[E0004]: non-exhaustive patterns: `_` not covered
13+
--> $DIR/non-exhaustive-pattern-pointer-size-int.rs:13:11
14+
|
15+
LL | match 0isize {
16+
| ^^^^^^ pattern `_` not covered
17+
|
18+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
19+
= note: the matched value is of type `isize`
20+
= note: for `usize` and `isize`, no assumptions about the maximum value are permitted
21+
= note: to exhaustively match on either pointer-size integer type, wildcards must be used
22+
23+
error: aborting due to 2 previous errors
24+
25+
For more information about this error, try `rustc --explain E0004`.

0 commit comments

Comments
 (0)