Skip to content

Commit daa117e

Browse files
committed
Small improvement to exhaustiveness diagnostics
1 parent 54e97e8 commit daa117e

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -957,8 +957,20 @@ impl<'tcx> Constructor<'tcx> {
957957
PatKind::Slice { prefix: subpatterns.collect(), slice: None, suffix: vec![] }
958958
}
959959
VarLen(prefix, _) => {
960-
let prefix = subpatterns.by_ref().take(prefix as usize).collect();
961-
let suffix = subpatterns.collect();
960+
let mut prefix: Vec<_> = subpatterns.by_ref().take(prefix as usize).collect();
961+
let mut suffix: Vec<_> = subpatterns.collect();
962+
if slice.array_len.is_some() {
963+
// Improves diagnostics a bit: if the type is a known-size array, instead
964+
// of reporting `[x, _, .., _, y]`, we prefer to report `[x, .., y]`.
965+
// This is incorrect if the size is not known, since `[_, ..]` captures
966+
// arrays of lengths `>= 1` whereas `[..]` captures any length.
967+
while !suffix.is_empty() && suffix.first().unwrap().is_wildcard() {
968+
suffix.remove(0);
969+
}
970+
while !prefix.is_empty() && prefix.last().unwrap().is_wildcard() {
971+
prefix.pop();
972+
}
973+
}
962974
let wild = Pat::wildcard_from_ty(ty);
963975
PatKind::Slice { prefix, slice: Some(wild), suffix }
964976
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ fn main() {
1212
[true, .., true] => {}
1313
}
1414
match s3 {
15-
//~^ ERROR `&[false, .., _]` not covered
15+
//~^ ERROR `&[false, ..]` not covered
1616
[true, .., true] => {}
1717
}
1818
match s10 {
19-
//~^ ERROR `&[false, .., _]` not covered
19+
//~^ ERROR `&[false, ..]` not covered
2020
[true, .., true] => {}
2121
}
2222

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ LL | match s2 {
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: `&[false, .., _]` not covered
9+
error[E0004]: non-exhaustive patterns: `&[false, ..]` not covered
1010
--> $DIR/slice-patterns-exhaustiveness.rs:14:11
1111
|
1212
LL | match s3 {
13-
| ^^ pattern `&[false, .., _]` not covered
13+
| ^^ pattern `&[false, ..]` not covered
1414
|
1515
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1616

17-
error[E0004]: non-exhaustive patterns: `&[false, .., _]` not covered
17+
error[E0004]: non-exhaustive patterns: `&[false, ..]` not covered
1818
--> $DIR/slice-patterns-exhaustiveness.rs:18:11
1919
|
2020
LL | match s10 {
21-
| ^^^ pattern `&[false, .., _]` not covered
21+
| ^^^ pattern `&[false, ..]` not covered
2222
|
2323
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
2424

0 commit comments

Comments
 (0)