Skip to content

Commit 5157190

Browse files
authored
Rollup merge of rust-lang#120727 - Nadrieril:tweak-int-reporting, r=compiler-errors
exhaustiveness: Prefer "`0..MAX` not covered" to "`_` not covered" There was an exception when reporting integer ranges as missing, it's been there for as long as I can remember. This PR removes it. I think it's nicer to report "`0..MAX` not covered" than "`_` not covered". This also makes it consistent with enums, where we report individual enum variants in this case (as showcased in the rest of the `empty-match.rs` test). r? ``@estebank``
2 parents fec3235 + 9dca6be commit 5157190

7 files changed

+192
-50
lines changed

compiler/rustc_pattern_analysis/src/usefulness.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1520,11 +1520,9 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: TypeCx>(
15201520
split_ctors.push(Constructor::Missing);
15211521
}
15221522

1523-
// Decide what constructors to report.
1524-
let is_integers = matches!(ctors_for_ty, ConstructorSet::Integers { .. });
1525-
let always_report_all = place.is_scrutinee && !is_integers;
1526-
// Whether we should report "Enum::A and Enum::C are missing" or "_ is missing".
1527-
let report_individual_missing_ctors = always_report_all || !all_missing;
1523+
// Whether we should report "Enum::A and Enum::C are missing" or "_ is missing". At the top
1524+
// level we prefer to list all constructors.
1525+
let report_individual_missing_ctors = place.is_scrutinee || !all_missing;
15281526
// Which constructors are considered missing. We ensure that `!missing_ctors.is_empty() =>
15291527
// split_ctors.contains(Missing)`. The converse usually holds except when
15301528
// `!place_validity.is_known_valid()`.

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ help: you might want to use `if let` to ignore the variant that isn't matched
4343
LL | if let None = x { todo!() };
4444
| ++ +++++++++++
4545

46-
error[E0004]: non-exhaustive patterns: `_` not covered
46+
error[E0004]: non-exhaustive patterns: `0_u8..=u8::MAX` not covered
4747
--> $DIR/empty-match-check-notes.rs:45:11
4848
|
4949
LL | match 0u8 {
50-
| ^^^ pattern `_` not covered
50+
| ^^^ pattern `0_u8..=u8::MAX` not covered
5151
|
5252
= note: the matched value is of type `u8`
5353
= note: match arms with guards don't count towards exhaustivity
5454
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
5555
|
5656
LL ~ _ if false => {},
57-
LL + _ => todo!()
57+
LL + 0_u8..=u8::MAX => todo!()
5858
|
5959

6060
error: aborting due to 6 previous errors

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ help: you might want to use `if let` to ignore the variant that isn't matched
4242
LL | if let None = x { todo!() };
4343
| ++ +++++++++++
4444

45-
error[E0004]: non-exhaustive patterns: `_` not covered
45+
error[E0004]: non-exhaustive patterns: `0_u8..=u8::MAX` not covered
4646
--> $DIR/empty-match-check-notes.rs:45:11
4747
|
4848
LL | match 0u8 {
49-
| ^^^ pattern `_` not covered
49+
| ^^^ pattern `0_u8..=u8::MAX` not covered
5050
|
5151
= note: the matched value is of type `u8`
5252
= note: match arms with guards don't count towards exhaustivity
5353
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
5454
|
5555
LL ~ _ if false => {},
56-
LL + _ => todo!()
56+
LL + 0_u8..=u8::MAX => todo!()
5757
|
5858

5959
error: aborting due to 6 previous errors

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ fn empty_foreign_enum_private(x: Option<empty::SecretlyUninhabitedForeignStruct>
4343

4444
fn main() {
4545
match 0u8 {
46-
//~^ ERROR `_` not covered
46+
//~^ ERROR not covered
4747
//~| NOTE the matched value is of type
4848
//~| NOTE match arms with guards don't count towards exhaustivity
49-
//~| NOTE pattern `_` not covered
49+
//~| NOTE not covered
5050
_ if false => {}
5151
}
5252
}

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

+87-18
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,36 @@ LL | match_no_arms!(0u8);
77
= note: the matched value is of type `u8`
88
= help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern
99

10-
error[E0004]: non-exhaustive patterns: type `NonEmptyStruct1` is non-empty
10+
error[E0004]: non-exhaustive patterns: type `i8` is non-empty
1111
--> $DIR/empty-match.rs:47:20
1212
|
13+
LL | match_no_arms!(0i8);
14+
| ^^^
15+
|
16+
= note: the matched value is of type `i8`
17+
= help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern
18+
19+
error[E0004]: non-exhaustive patterns: type `usize` is non-empty
20+
--> $DIR/empty-match.rs:48:20
21+
|
22+
LL | match_no_arms!(0usize);
23+
| ^^^^^^
24+
|
25+
= note: the matched value is of type `usize`
26+
= help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern
27+
28+
error[E0004]: non-exhaustive patterns: type `isize` is non-empty
29+
--> $DIR/empty-match.rs:49:20
30+
|
31+
LL | match_no_arms!(0isize);
32+
| ^^^^^^
33+
|
34+
= note: the matched value is of type `isize`
35+
= help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern
36+
37+
error[E0004]: non-exhaustive patterns: type `NonEmptyStruct1` is non-empty
38+
--> $DIR/empty-match.rs:50:20
39+
|
1340
LL | match_no_arms!(NonEmptyStruct1);
1441
| ^^^^^^^^^^^^^^^
1542
|
@@ -22,7 +49,7 @@ LL | struct NonEmptyStruct1;
2249
= help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern
2350

2451
error[E0004]: non-exhaustive patterns: type `NonEmptyStruct2` is non-empty
25-
--> $DIR/empty-match.rs:48:20
52+
--> $DIR/empty-match.rs:51:20
2653
|
2754
LL | match_no_arms!(NonEmptyStruct2(true));
2855
| ^^^^^^^^^^^^^^^^^^^^^
@@ -36,7 +63,7 @@ LL | struct NonEmptyStruct2(bool);
3663
= help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern
3764

3865
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion1` is non-empty
39-
--> $DIR/empty-match.rs:49:20
66+
--> $DIR/empty-match.rs:52:20
4067
|
4168
LL | match_no_arms!((NonEmptyUnion1 { foo: () }));
4269
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -50,7 +77,7 @@ LL | union NonEmptyUnion1 {
5077
= help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern
5178

5279
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion2` is non-empty
53-
--> $DIR/empty-match.rs:50:20
80+
--> $DIR/empty-match.rs:53:20
5481
|
5582
LL | match_no_arms!((NonEmptyUnion2 { foo: () }));
5683
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -64,7 +91,7 @@ LL | union NonEmptyUnion2 {
6491
= help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern
6592

6693
error[E0004]: non-exhaustive patterns: `NonEmptyEnum1::Foo(_)` not covered
67-
--> $DIR/empty-match.rs:51:20
94+
--> $DIR/empty-match.rs:54:20
6895
|
6996
LL | match_no_arms!(NonEmptyEnum1::Foo(true));
7097
| ^^^^^^^^^^^^^^^^^^^^^^^^ pattern `NonEmptyEnum1::Foo(_)` not covered
@@ -80,7 +107,7 @@ LL | Foo(bool),
80107
= help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern
81108

82109
error[E0004]: non-exhaustive patterns: `NonEmptyEnum2::Foo(_)` and `NonEmptyEnum2::Bar` not covered
83-
--> $DIR/empty-match.rs:52:20
110+
--> $DIR/empty-match.rs:55:20
84111
|
85112
LL | match_no_arms!(NonEmptyEnum2::Foo(true));
86113
| ^^^^^^^^^^^^^^^^^^^^^^^^ patterns `NonEmptyEnum2::Foo(_)` and `NonEmptyEnum2::Bar` not covered
@@ -98,7 +125,7 @@ LL | Bar,
98125
= help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or multiple match arms
99126

100127
error[E0004]: non-exhaustive patterns: `NonEmptyEnum5::V1`, `NonEmptyEnum5::V2`, `NonEmptyEnum5::V3` and 2 more not covered
101-
--> $DIR/empty-match.rs:53:20
128+
--> $DIR/empty-match.rs:56:20
102129
|
103130
LL | match_no_arms!(NonEmptyEnum5::V1);
104131
| ^^^^^^^^^^^^^^^^^ patterns `NonEmptyEnum5::V1`, `NonEmptyEnum5::V2`, `NonEmptyEnum5::V3` and 2 more not covered
@@ -121,22 +148,64 @@ LL | V5,
121148
= note: the matched value is of type `NonEmptyEnum5`
122149
= help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or multiple match arms
123150

124-
error[E0004]: non-exhaustive patterns: `_` not covered
125-
--> $DIR/empty-match.rs:55:24
151+
error[E0004]: non-exhaustive patterns: `0_u8..=u8::MAX` not covered
152+
--> $DIR/empty-match.rs:58:24
126153
|
127154
LL | match_guarded_arm!(0u8);
128-
| ^^^ pattern `_` not covered
155+
| ^^^ pattern `0_u8..=u8::MAX` not covered
129156
|
130157
= note: the matched value is of type `u8`
131158
= note: match arms with guards don't count towards exhaustivity
132159
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
133160
|
134161
LL ~ _ if false => {},
162+
LL + 0_u8..=u8::MAX => todo!()
163+
|
164+
165+
error[E0004]: non-exhaustive patterns: `i8::MIN..=i8::MAX` not covered
166+
--> $DIR/empty-match.rs:59:24
167+
|
168+
LL | match_guarded_arm!(0i8);
169+
| ^^^ pattern `i8::MIN..=i8::MAX` not covered
170+
|
171+
= note: the matched value is of type `i8`
172+
= note: match arms with guards don't count towards exhaustivity
173+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
174+
|
175+
LL ~ _ if false => {},
176+
LL + i8::MIN..=i8::MAX => todo!()
177+
|
178+
179+
error[E0004]: non-exhaustive patterns: `0_usize..` not covered
180+
--> $DIR/empty-match.rs:60:24
181+
|
182+
LL | match_guarded_arm!(0usize);
183+
| ^^^^^^ pattern `0_usize..` not covered
184+
|
185+
= note: the matched value is of type `usize`
186+
= note: match arms with guards don't count towards exhaustivity
187+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
188+
|
189+
LL ~ _ if false => {},
190+
LL + 0_usize.. => todo!()
191+
|
192+
193+
error[E0004]: non-exhaustive patterns: `_` not covered
194+
--> $DIR/empty-match.rs:61:24
195+
|
196+
LL | match_guarded_arm!(0isize);
197+
| ^^^^^^ pattern `_` not covered
198+
|
199+
= note: the matched value is of type `isize`
200+
= note: match arms with guards don't count towards exhaustivity
201+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
202+
|
203+
LL ~ _ if false => {},
135204
LL + _ => todo!()
136205
|
137206

138207
error[E0004]: non-exhaustive patterns: `NonEmptyStruct1` not covered
139-
--> $DIR/empty-match.rs:56:24
208+
--> $DIR/empty-match.rs:62:24
140209
|
141210
LL | match_guarded_arm!(NonEmptyStruct1);
142211
| ^^^^^^^^^^^^^^^ pattern `NonEmptyStruct1` not covered
@@ -155,7 +224,7 @@ LL + NonEmptyStruct1 => todo!()
155224
|
156225

157226
error[E0004]: non-exhaustive patterns: `NonEmptyStruct2(_)` not covered
158-
--> $DIR/empty-match.rs:57:24
227+
--> $DIR/empty-match.rs:63:24
159228
|
160229
LL | match_guarded_arm!(NonEmptyStruct2(true));
161230
| ^^^^^^^^^^^^^^^^^^^^^ pattern `NonEmptyStruct2(_)` not covered
@@ -174,7 +243,7 @@ LL + NonEmptyStruct2(_) => todo!()
174243
|
175244

176245
error[E0004]: non-exhaustive patterns: `NonEmptyUnion1 { .. }` not covered
177-
--> $DIR/empty-match.rs:58:24
246+
--> $DIR/empty-match.rs:64:24
178247
|
179248
LL | match_guarded_arm!((NonEmptyUnion1 { foo: () }));
180249
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pattern `NonEmptyUnion1 { .. }` not covered
@@ -193,7 +262,7 @@ LL + NonEmptyUnion1 { .. } => todo!()
193262
|
194263

195264
error[E0004]: non-exhaustive patterns: `NonEmptyUnion2 { .. }` not covered
196-
--> $DIR/empty-match.rs:59:24
265+
--> $DIR/empty-match.rs:65:24
197266
|
198267
LL | match_guarded_arm!((NonEmptyUnion2 { foo: () }));
199268
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pattern `NonEmptyUnion2 { .. }` not covered
@@ -212,7 +281,7 @@ LL + NonEmptyUnion2 { .. } => todo!()
212281
|
213282

214283
error[E0004]: non-exhaustive patterns: `NonEmptyEnum1::Foo(_)` not covered
215-
--> $DIR/empty-match.rs:60:24
284+
--> $DIR/empty-match.rs:66:24
216285
|
217286
LL | match_guarded_arm!(NonEmptyEnum1::Foo(true));
218287
| ^^^^^^^^^^^^^^^^^^^^^^^^ pattern `NonEmptyEnum1::Foo(_)` not covered
@@ -233,7 +302,7 @@ LL + NonEmptyEnum1::Foo(_) => todo!()
233302
|
234303

235304
error[E0004]: non-exhaustive patterns: `NonEmptyEnum2::Foo(_)` and `NonEmptyEnum2::Bar` not covered
236-
--> $DIR/empty-match.rs:61:24
305+
--> $DIR/empty-match.rs:67:24
237306
|
238307
LL | match_guarded_arm!(NonEmptyEnum2::Foo(true));
239308
| ^^^^^^^^^^^^^^^^^^^^^^^^ patterns `NonEmptyEnum2::Foo(_)` and `NonEmptyEnum2::Bar` not covered
@@ -256,7 +325,7 @@ LL + NonEmptyEnum2::Foo(_) | NonEmptyEnum2::Bar => todo!()
256325
|
257326

258327
error[E0004]: non-exhaustive patterns: `NonEmptyEnum5::V1`, `NonEmptyEnum5::V2`, `NonEmptyEnum5::V3` and 2 more not covered
259-
--> $DIR/empty-match.rs:62:24
328+
--> $DIR/empty-match.rs:68:24
260329
|
261330
LL | match_guarded_arm!(NonEmptyEnum5::V1);
262331
| ^^^^^^^^^^^^^^^^^ patterns `NonEmptyEnum5::V1`, `NonEmptyEnum5::V2`, `NonEmptyEnum5::V3` and 2 more not covered
@@ -284,6 +353,6 @@ LL ~ _ if false => {},
284353
LL + _ => todo!()
285354
|
286355

287-
error: aborting due to 16 previous errors
356+
error: aborting due to 22 previous errors
288357

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

0 commit comments

Comments
 (0)