Skip to content

Commit 65bc67e

Browse files
committed
Make exhaustiveness error message more consistent for slice patterns
This improves error messages by indicating when slices above a certain lengths have not been matched. Previously, we would only report examples of such lengths, but of course never all of them.
1 parent b669730 commit 65bc67e

File tree

8 files changed

+26
-18
lines changed

8 files changed

+26
-18
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -841,9 +841,17 @@ impl<'tcx> Constructor<'tcx> {
841841

842842
ty::Ref(..) => PatKind::Deref { subpattern: subpatterns.nth(0).unwrap() },
843843

844-
ty::Slice(_) | ty::Array(..) => {
845-
PatKind::Slice { prefix: subpatterns.collect(), slice: None, suffix: vec![] }
846-
}
844+
ty::Slice(_) | ty::Array(..) => match self {
845+
FixedLenSlice(_) => {
846+
PatKind::Slice { prefix: subpatterns.collect(), slice: None, suffix: vec![] }
847+
}
848+
VarLenSlice(_) => {
849+
let prefix = subpatterns.collect();
850+
let wild = Pat { ty, span: DUMMY_SP, kind: Box::new(PatKind::Wild) };
851+
PatKind::Slice { prefix, slice: Some(wild), suffix: vec![] }
852+
}
853+
_ => bug!("bad slice pattern {:?} {:?}", self, ty),
854+
},
847855

848856
_ => match *self {
849857
ConstantValue(value, _) => PatKind::Constant { value },

src/test/ui/consts/const_let_refutable.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0005]: refutable pattern in function argument: `&[]`, `&[_]` and `&[_, _, _]` not covered
1+
error[E0005]: refutable pattern in function argument: `&[]`, `&[_]` and `&[_, _, _, ..]` not covered
22
--> $DIR/const_let_refutable.rs:3:16
33
|
44
LL | const fn slice([a, b]: &[i32]) -> i32 {
5-
| ^^^^^^ patterns `&[]`, `&[_]` and `&[_, _, _]` not covered
5+
| ^^^^^^ patterns `&[]`, `&[_]` and `&[_, _, _, ..]` not covered
66

77
error[E0723]: can only call other `const fn` within a `const fn`, but `const <&i32 as std::ops::Add>::add` is not stable as `const fn`
88
--> $DIR/const_let_refutable.rs:4:5

src/test/ui/pattern/usefulness/match-byte-array-patterns-2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ LL | match buf {
66
|
77
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
88

9-
error[E0004]: non-exhaustive patterns: `&[]` not covered
9+
error[E0004]: non-exhaustive patterns: `&[..]` not covered
1010
--> $DIR/match-byte-array-patterns-2.rs:10:11
1111
|
1212
LL | match buf {
13-
| ^^^ pattern `&[]` not covered
13+
| ^^^ pattern `&[..]` not covered
1414
|
1515
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1616

src/test/ui/pattern/usefulness/non-exhaustive-match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn main() {
4444
}
4545
let vec = vec![0.5f32];
4646
let vec: &[f32] = &vec;
47-
match *vec { //~ ERROR non-exhaustive patterns: `[_, _, _, _]` not covered
47+
match *vec { //~ ERROR non-exhaustive patterns: `[_, _, _, _, ..]` not covered
4848
[0.1, 0.2, 0.3] => (),
4949
[0.1, 0.2] => (),
5050
[0.1] => (),

src/test/ui/pattern/usefulness/non-exhaustive-match.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ LL | match *vec {
6666
|
6767
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
6868

69-
error[E0004]: non-exhaustive patterns: `[_, _, _, _]` not covered
69+
error[E0004]: non-exhaustive patterns: `[_, _, _, _, ..]` not covered
7070
--> $DIR/non-exhaustive-match.rs:47:11
7171
|
7272
LL | match *vec {
73-
| ^^^^ pattern `[_, _, _, _]` not covered
73+
| ^^^^ pattern `[_, _, _, _, ..]` not covered
7474
|
7575
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
7676

src/test/ui/pattern/usefulness/slice-patterns.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ fn main() {
4848
[true, .., true] => {}
4949
}
5050
match s {
51-
//~^ ERROR `&[_]` not covered
51+
//~^ ERROR `&[_, ..]` not covered
5252
[] => {}
5353
}
5454
match s {
55-
//~^ ERROR `&[_, _]` not covered
55+
//~^ ERROR `&[_, _, ..]` not covered
5656
[] => {}
5757
[_] => {}
5858
}

src/test/ui/pattern/usefulness/slice-patterns.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ LL | match s3 {
3030
|
3131
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
3232

33-
error[E0004]: non-exhaustive patterns: `&[_]` not covered
33+
error[E0004]: non-exhaustive patterns: `&[_, ..]` not covered
3434
--> $DIR/slice-patterns.rs:50:11
3535
|
3636
LL | match s {
37-
| ^ pattern `&[_]` not covered
37+
| ^ pattern `&[_, ..]` not covered
3838
|
3939
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
4040

41-
error[E0004]: non-exhaustive patterns: `&[_, _]` not covered
41+
error[E0004]: non-exhaustive patterns: `&[_, _, ..]` not covered
4242
--> $DIR/slice-patterns.rs:54:11
4343
|
4444
LL | match s {
45-
| ^ pattern `&[_, _]` not covered
45+
| ^ pattern `&[_, _, ..]` not covered
4646
|
4747
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
4848

src/test/ui/uninhabited/uninhabited-matches-feature-gated.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ LL | let _ = match x {};
3030
|
3131
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
3232

33-
error[E0004]: non-exhaustive patterns: `&[_]` not covered
33+
error[E0004]: non-exhaustive patterns: `&[_, ..]` not covered
3434
--> $DIR/uninhabited-matches-feature-gated.rs:21:19
3535
|
3636
LL | let _ = match x {
37-
| ^ pattern `&[_]` not covered
37+
| ^ pattern `&[_, ..]` not covered
3838
|
3939
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
4040

0 commit comments

Comments
 (0)