Skip to content

Commit b93ecc1

Browse files
committed
Address code reviews
1 parent dcbe85a commit b93ecc1

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed

src/librustc_mir_build/hair/pattern/check_match.rs

+18-7
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;
@@ -468,14 +469,14 @@ fn check_exhaustive<'p, 'tcx>(
468469
// In the case of an empty match, replace the '`_` not covered' diagnostic with something more
469470
// informative.
470471
let mut err;
471-
let joined_patterns = joined_uncovered_patterns(&witnesses);
472472
if is_empty_match && !non_empty_enum {
473473
err = create_e0004(
474474
cx.tcx.sess,
475475
sp,
476476
format!("non-exhaustive patterns: type `{}` is non-empty", scrut_ty),
477477
);
478478
} else {
479+
let joined_patterns = joined_uncovered_patterns(&witnesses);
479480
err = create_e0004(
480481
cx.tcx.sess,
481482
sp,
@@ -487,16 +488,26 @@ 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));
493494
if (scrut_ty == cx.tcx.types.usize || scrut_ty == cx.tcx.types.isize)
494-
&& joined_patterns == "`_`"
495+
&& !is_empty_match
496+
&& witnesses.len() == 1
497+
&& witnesses[0].is_wildcard()
495498
{
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-
);
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+
}
500511
}
501512
err.emit();
502513
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +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
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
1111

1212
error[E0004]: non-exhaustive patterns: `_` not covered
1313
--> $DIR/feature-gate-precise_pointer_size_matching.rs:10:11
@@ -17,8 +17,8 @@ LL | match 0isize {
1717
|
1818
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1919
= 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
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
2222

2323
error: aborting due to 2 previous errors
2424

src/test/ui/pattern/usefulness/non-exhaustive-pattern-pointer-size-int.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ fn main() {
55
//~^ ERROR non-exhaustive patterns
66
//~| NOTE pattern `_` not covered
77
//~| 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
8+
//~| NOTE `usize` does not have a fixed maximum value
109
0 ..= usize::MAX => {}
1110
}
1211

1312
match 0isize {
1413
//~^ ERROR non-exhaustive patterns
1514
//~| NOTE pattern `_` not covered
1615
//~| 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
16+
//~| NOTE `isize` does not have a fixed maximum value
1917
isize::MIN ..= isize::MAX => {}
2018
}
19+
20+
match 7usize {}
21+
//~^ ERROR non-exhaustive patterns
22+
//~| NOTE the matched value is of type `usize`
2123
}

src/test/ui/pattern/usefulness/non-exhaustive-pattern-pointer-size-int.stderr

+15-6
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,29 @@ 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
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
1111

1212
error[E0004]: non-exhaustive patterns: `_` not covered
13-
--> $DIR/non-exhaustive-pattern-pointer-size-int.rs:13:11
13+
--> $DIR/non-exhaustive-pattern-pointer-size-int.rs:12:11
1414
|
1515
LL | match 0isize {
1616
| ^^^^^^ pattern `_` not covered
1717
|
1818
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1919
= 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
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
2222

23-
error: aborting due to 2 previous errors
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
2433

2534
For more information about this error, try `rustc --explain E0004`.

0 commit comments

Comments
 (0)