about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2018-03-18 23:26:57 -0700
committerEsteban Küber <esteban@kuber.com.ar>2018-03-19 14:08:57 -0700
commitf332a9ce5676859f0f07c4d6bf2d2c415be67066 (patch)
treec2108c33b284c59186f61f040c905812cbc3de4f
parent685c3c1b4a7c1949048177409ca43fc05daefd3f (diff)
downloadrust-f332a9ce5676859f0f07c4d6bf2d2c415be67066.tar.gz
rust-f332a9ce5676859f0f07c4d6bf2d2c415be67066.zip
Single diagnostic for all non-mentioned fields in a pattern
-rw-r--r--src/librustc_typeck/check/_match.rs22
-rw-r--r--src/test/ui/missing-fields-in-struct-pattern.rs2
-rw-r--r--src/test/ui/missing-fields-in-struct-pattern.stderr22
3 files changed, 41 insertions, 5 deletions
diff --git a/src/librustc_typeck/check/_match.rs b/src/librustc_typeck/check/_match.rs
index 0b5383f1f8d..cc72a565ba2 100644
--- a/src/librustc_typeck/check/_match.rs
+++ b/src/librustc_typeck/check/_match.rs
@@ -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");
                 }
diff --git a/src/test/ui/missing-fields-in-struct-pattern.rs b/src/test/ui/missing-fields-in-struct-pattern.rs
index 2469f245345..28ed151b986 100644
--- a/src/test/ui/missing-fields-in-struct-pattern.rs
+++ b/src/test/ui/missing-fields-in-struct-pattern.rs
@@ -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");
     }
 }
diff --git a/src/test/ui/missing-fields-in-struct-pattern.stderr b/src/test/ui/missing-fields-in-struct-pattern.stderr
new file mode 100644
index 00000000000..88fba19d14e
--- /dev/null
+++ b/src/test/ui/missing-fields-in-struct-pattern.stderr
@@ -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`.