]> git.lizzy.rs Git - rust.git/commitdiff
Single diagnostic for all non-mentioned fields in a pattern
authorEsteban Küber <esteban@kuber.com.ar>
Mon, 19 Mar 2018 06:26:57 +0000 (23:26 -0700)
committerEsteban Küber <esteban@kuber.com.ar>
Mon, 19 Mar 2018 21:08:57 +0000 (14:08 -0700)
src/librustc_typeck/check/_match.rs
src/test/ui/missing-fields-in-struct-pattern.rs
src/test/ui/missing-fields-in-struct-pattern.stderr [new file with mode: 0644]

index 0b5383f1f8d1ed06cac2c209129ea8eb8c7fb83f..cc72a565ba28ed1b06ab8192c61eb3bad2ed8361 100644 (file)
@@ -993,13 +993,25 @@ fn check_struct_pat_fields(&self,
                 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");
                 }
index 2469f245345430feda1bd1c314769b907c77d12e..28ed151b986064f6c6faaed69d07ddcd4109a65c 100644 (file)
@@ -12,6 +12,8 @@
 
 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 (file)
index 0000000..88fba19
--- /dev/null
@@ -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`.