Small improvement to exhaustiveness diagnostics

This commit is contained in:
Nadrieril 2019-11-17 17:48:45 +00:00
parent 54e97e889b
commit daa117eab7
3 changed files with 20 additions and 8 deletions

View File

@ -957,8 +957,20 @@ fn apply<'a>(
PatKind::Slice { prefix: subpatterns.collect(), slice: None, suffix: vec![] }
}
VarLen(prefix, _) => {
let prefix = subpatterns.by_ref().take(prefix as usize).collect();
let suffix = subpatterns.collect();
let mut prefix: Vec<_> = subpatterns.by_ref().take(prefix as usize).collect();
let mut suffix: Vec<_> = subpatterns.collect();
if slice.array_len.is_some() {
// Improves diagnostics a bit: if the type is a known-size array, instead
// of reporting `[x, _, .., _, y]`, we prefer to report `[x, .., y]`.
// This is incorrect if the size is not known, since `[_, ..]` captures
// arrays of lengths `>= 1` whereas `[..]` captures any length.
while !suffix.is_empty() && suffix.first().unwrap().is_wildcard() {
suffix.remove(0);
}
while !prefix.is_empty() && prefix.last().unwrap().is_wildcard() {
prefix.pop();
}
}
let wild = Pat::wildcard_from_ty(ty);
PatKind::Slice { prefix, slice: Some(wild), suffix }
}

View File

@ -12,11 +12,11 @@ fn main() {
[true, .., true] => {}
}
match s3 {
//~^ ERROR `&[false, .., _]` not covered
//~^ ERROR `&[false, ..]` not covered
[true, .., true] => {}
}
match s10 {
//~^ ERROR `&[false, .., _]` not covered
//~^ ERROR `&[false, ..]` not covered
[true, .., true] => {}
}

View File

@ -6,19 +6,19 @@ LL | match s2 {
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
error[E0004]: non-exhaustive patterns: `&[false, .., _]` not covered
error[E0004]: non-exhaustive patterns: `&[false, ..]` not covered
--> $DIR/slice-patterns-exhaustiveness.rs:14:11
|
LL | match s3 {
| ^^ pattern `&[false, .., _]` not covered
| ^^ pattern `&[false, ..]` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
error[E0004]: non-exhaustive patterns: `&[false, .., _]` not covered
error[E0004]: non-exhaustive patterns: `&[false, ..]` not covered
--> $DIR/slice-patterns-exhaustiveness.rs:18:11
|
LL | match s10 {
| ^^^ pattern `&[false, .., _]` not covered
| ^^^ pattern `&[false, ..]` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms