Skip to content

Commit a1ac4d6

Browse files
authored
Rollup merge of #73937 - JohnTitor:note-exhaustive-sized-int, r=varkor
Explain exhaustive matching on {usize,isize} maximum values The wording is taken from https://github.com/rust-lang/rfcs/blob/master/text/2591-exhaustive-integer-pattern-matching.md#reference-level-explanation. Fixes #73919 r? @varkor
2 parents 3e78eac + b93ecc1 commit a1ac4d6

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

src/librustc_mir_build/hair/pattern/check_match.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_hir::def_id::DefId;
1212
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
1313
use rustc_hir::{HirId, Pat};
1414
use rustc_middle::ty::{self, Ty, TyCtxt};
15+
use rustc_session::config::nightly_options;
1516
use rustc_session::lint::builtin::BINDINGS_WITH_VARIANT_NAME;
1617
use rustc_session::lint::builtin::{IRREFUTABLE_LET_PATTERNS, UNREACHABLE_PATTERNS};
1718
use rustc_session::parse::feature_err;
@@ -487,9 +488,27 @@ fn check_exhaustive<'p, 'tcx>(
487488
adt_defined_here(cx, &mut err, scrut_ty, &witnesses);
488489
err.help(
489490
"ensure that all possible cases are being handled, \
490-
possibly by adding wildcards or more match arms",
491+
possibly by adding wildcards or more match arms",
491492
);
492493
err.note(&format!("the matched value is of type `{}`", scrut_ty));
494+
if (scrut_ty == cx.tcx.types.usize || scrut_ty == cx.tcx.types.isize)
495+
&& !is_empty_match
496+
&& witnesses.len() == 1
497+
&& witnesses[0].is_wildcard()
498+
{
499+
err.note(&format!(
500+
"`{}` does not have a fixed maximum value, \
501+
so a wildcard `_` is necessary to match exhaustively",
502+
scrut_ty,
503+
));
504+
if nightly_options::is_nightly_build() {
505+
err.help(&format!(
506+
"add `#![feature(precise_pointer_size_matching)]` \
507+
to the crate attributes to enable precise `{}` matching",
508+
scrut_ty,
509+
));
510+
}
511+
}
493512
err.emit();
494513
}
495514

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

+4
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: `usize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively
10+
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching
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: `isize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively
21+
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching
1822

1923
error: aborting due to 2 previous errors
2024

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 `usize` does not have a fixed maximum value
9+
0 ..= usize::MAX => {}
10+
}
11+
12+
match 0isize {
13+
//~^ ERROR non-exhaustive patterns
14+
//~| NOTE pattern `_` not covered
15+
//~| NOTE the matched value is of type `isize`
16+
//~| NOTE `isize` does not have a fixed maximum value
17+
isize::MIN ..= isize::MAX => {}
18+
}
19+
20+
match 7usize {}
21+
//~^ ERROR non-exhaustive patterns
22+
//~| NOTE the matched value is of type `usize`
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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: `usize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively
10+
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching
11+
12+
error[E0004]: non-exhaustive patterns: `_` not covered
13+
--> $DIR/non-exhaustive-pattern-pointer-size-int.rs:12: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: `isize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively
21+
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching
22+
23+
error[E0004]: non-exhaustive patterns: type `usize` is non-empty
24+
--> $DIR/non-exhaustive-pattern-pointer-size-int.rs:20:11
25+
|
26+
LL | match 7usize {}
27+
| ^^^^^^
28+
|
29+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
30+
= note: the matched value is of type `usize`
31+
32+
error: aborting due to 3 previous errors
33+
34+
For more information about this error, try `rustc --explain E0004`.

0 commit comments

Comments
 (0)