Single diagnostic for all non-mentioned fields in a pattern

This commit is contained in:
Esteban Küber 2018-03-18 23:26:57 -07:00
parent 685c3c1b4a
commit f332a9ce56
3 changed files with 41 additions and 5 deletions

View file

@ -993,13 +993,25 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
tcx.sess.span_err(span, "`..` cannot be used in union patterns");
}
} else if !etc {
for field in variant.fields
let unmentioned_fields = variant.fields
.iter()
.filter(|field| !used_fields.contains_key(&field.name)) {
.map(|field| field.name)
.filter(|field| !used_fields.contains_key(&field))
.collect::<Vec<_>>();
if unmentioned_fields.len() > 0 {
let field_names = if unmentioned_fields.len() == 1 {
format!("field `{}`", unmentioned_fields[0])
} else {
format!("fields {}",
unmentioned_fields.iter()
.map(|name| format!("`{}`", name))
.collect::<Vec<String>>()
.join(", "))
};
let mut diag = struct_span_err!(tcx.sess, span, E0027,
"pattern does not mention field `{}`",
field.name);
diag.span_label(span, format!("missing field `{}`", field.name));
"pattern does not mention {}",
field_names);
diag.span_label(span, format!("missing {}", field_names));
if variant.ctor_kind == CtorKind::Fn {
diag.note("trying to match a tuple variant with a struct variant pattern");
}

View file

@ -12,6 +12,8 @@ struct S(usize, usize, usize, usize);
fn main() {
if let S { a, b, c, d } = S(1, 2, 3, 4) {
//~^ ERROR struct `S` does not have fields named `a`, `b`, `c`, `d` [E0026]
//~^ ERROR pattern does not mention fields `0`, `1`, `2`, `3` [E0027]
println!("hi");
}
}

View file

@ -0,0 +1,22 @@
error[E0026]: struct `S` does not have fields named `a`, `b`, `c`, `d`
--> $DIR/missing-fields-in-struct-pattern.rs:14:16
|
LL | if let S { a, b, c, d } = S(1, 2, 3, 4) {
| ^ ^ ^ ^ struct `S` does not have field `d`
| | | |
| | | struct `S` does not have field `c`
| | struct `S` does not have field `b`
| struct `S` does not have field `a`
error[E0027]: pattern does not mention fields `0`, `1`, `2`, `3`
--> $DIR/missing-fields-in-struct-pattern.rs:14:12
|
LL | if let S { a, b, c, d } = S(1, 2, 3, 4) {
| ^^^^^^^^^^^^^^^^ missing fields `0`, `1`, `2`, `3`
|
= note: trying to match a tuple variant with a struct variant pattern
error: aborting due to 2 previous errors
Some errors occurred: E0026, E0027.
For more information about an error, try `rustc --explain E0026`.