about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-08-24 17:44:43 +0200
committerMazdak Farrokhzad <twingoow@gmail.com>2019-08-24 19:57:05 +0200
commitba2a784c38c5e87e746457645184d4dec957941f (patch)
tree5aebb72ef076288b53d8b3e0e8a75370cca63650
parent25f605ae99b1ac3565fc0bb65d97083f39444d60 (diff)
downloadrust-ba2a784c38c5e87e746457645184d4dec957941f.tar.gz
rust-ba2a784c38c5e87e746457645184d4dec957941f.zip
typeck/pat.rs: extract `error_unmentioned_fields`.
-rw-r--r--src/librustc_typeck/check/pat.rs65
1 files changed, 37 insertions, 28 deletions
diff --git a/src/librustc_typeck/check/pat.rs b/src/librustc_typeck/check/pat.rs
index 0aed814b2da..265f032179b 100644
--- a/src/librustc_typeck/check/pat.rs
+++ b/src/librustc_typeck/check/pat.rs
@@ -873,34 +873,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
             if etc {
                 tcx.sess.span_err(span, "`..` cannot be used in union patterns");
             }
-        } else if !etc {
-            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_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");
-                }
-                if tcx.sess.teach(&diag.get_code().unwrap()) {
-                    diag.note(
-                        "This error indicates that a pattern for a struct fails to specify a \
-                         sub-pattern for every one of the struct's fields. Ensure that each field \
-                         from the struct's definition is mentioned in the pattern, or use `..` to \
-                         ignore unwanted fields."
-                    );
-                }
-                diag.emit();
-            }
+        } else if !etc && unmentioned_fields.len() > 0 {
+            self.error_unmentioned_fields(span, unmentioned_fields, variant);
         }
         no_field_errors
     }
@@ -916,6 +890,41 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
         .emit();
     }
 
+    fn error_unmentioned_fields(
+        &self,
+        span: Span,
+        unmentioned_fields: Vec<ast::Ident>,
+        variant: &ty::VariantDef,
+    ) {
+        let field_names = if unmentioned_fields.len() == 1 {
+            format!("field `{}`", unmentioned_fields[0])
+        } else {
+            let fields = unmentioned_fields.iter()
+                .map(|name| format!("`{}`", name))
+                .collect::<Vec<String>>()
+                .join(", ");
+            format!("fields {}", fields)
+        };
+        let mut diag = struct_span_err!(
+            self.tcx.sess, span, E0027,
+            "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");
+        }
+        if self.tcx.sess.teach(&diag.get_code().unwrap()) {
+            diag.note(
+                "This error indicates that a pattern for a struct fails to specify a \
+                    sub-pattern for every one of the struct's fields. Ensure that each field \
+                    from the struct's definition is mentioned in the pattern, or use `..` to \
+                    ignore unwanted fields."
+            );
+        }
+        diag.emit();
+    }
+
     fn check_pat_box(
         &self,
         span: Span,