Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Single diagnostic for all non-mentioned fields in a pattern
  • Loading branch information
estebank committed Mar 19, 2018
commit f332a9ce5676859f0f07c4d6bf2d2c415be67066
22 changes: 17 additions & 5 deletions src/librustc_typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/missing-fields-in-struct-pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
22 changes: 22 additions & 0 deletions src/test/ui/missing-fields-in-struct-pattern.stderr
Original file line number Diff line number Diff line change
@@ -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`.