about summary refs log tree commit diff
path: root/src/librustc_parse/parser
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-12-04 03:23:20 +0100
committerMazdak Farrokhzad <twingoow@gmail.com>2019-12-23 13:27:44 +0100
commite77b9d36ca5b33c7c76ca420b4021dadb8b2a05e (patch)
treeffb5575a22984b0be95a5c1cac8f41dfd5c6b7b6 /src/librustc_parse/parser
parenta916ac22b9f7f1f0f7aba0a41a789b3ecd765018 (diff)
downloadrust-e77b9d36ca5b33c7c76ca420b4021dadb8b2a05e.tar.gz
rust-e77b9d36ca5b33c7c76ca420b4021dadb8b2a05e.zip
refactor parse_field
Diffstat (limited to 'src/librustc_parse/parser')
-rw-r--r--src/librustc_parse/parser/expr.rs63
1 files changed, 33 insertions, 30 deletions
diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs
index fa68ddf272a..ce17b8fa546 100644
--- a/src/librustc_parse/parser/expr.rs
+++ b/src/librustc_parse/parser/expr.rs
@@ -1866,48 +1866,51 @@ impl<'a> Parser<'a> {
 
     /// Parses `ident (COLON expr)?`.
     fn parse_field(&mut self) -> PResult<'a, Field> {
-        let attrs = self.parse_outer_attributes()?;
+        let attrs = self.parse_outer_attributes()?.into();
         let lo = self.token.span;
 
         // Check if a colon exists one ahead. This means we're parsing a fieldname.
-        let (fieldname, expr, is_shorthand) =
-            if self.look_ahead(1, |t| t == &token::Colon || t == &token::Eq) {
-                let fieldname = self.parse_field_name()?;
-
-                // Check for an equals token. This means the source incorrectly attempts to
-                // initialize a field with an eq rather than a colon.
-                if self.token == token::Eq {
-                    self.diagnostic()
-                        .struct_span_err(self.token.span, "expected `:`, found `=`")
-                        .span_suggestion(
-                            fieldname.span.shrink_to_hi().to(self.token.span),
-                            "replace equals symbol with a colon",
-                            ":".to_string(),
-                            Applicability::MachineApplicable,
-                        )
-                        .emit();
-                }
-                self.bump(); // `:`
-                (fieldname, self.parse_expr()?, false)
-            } else {
-                let fieldname = self.parse_ident_common(false)?;
-
-                // Mimic `x: x` for the `x` field shorthand.
-                let path = ast::Path::from_ident(fieldname);
-                let expr = self.mk_expr(fieldname.span, ExprKind::Path(None, path), AttrVec::new());
-                (fieldname, expr, true)
-            };
+        let is_shorthand = !self.look_ahead(1, |t| t == &token::Colon || t == &token::Eq);
+        let (ident, expr) = if is_shorthand {
+            // Mimic `x: x` for the `x` field shorthand.
+            let ident = self.parse_ident_common(false)?;
+            let path = ast::Path::from_ident(ident);
+            (ident, self.mk_expr(ident.span, ExprKind::Path(None, path), AttrVec::new()))
+        } else {
+            let ident = self.parse_field_name()?;
+            self.error_on_eq_field_init(ident);
+            self.bump(); // `:`
+            (ident, self.parse_expr()?)
+        };
         Ok(ast::Field {
-            ident: fieldname,
+            ident,
             span: lo.to(expr.span),
             expr,
             is_shorthand,
-            attrs: attrs.into(),
+            attrs,
             id: DUMMY_NODE_ID,
             is_placeholder: false,
         })
     }
 
+    /// Check for `=`. This means the source incorrectly attempts to
+    /// initialize a field with an eq rather than a colon.
+    fn error_on_eq_field_init(&self, field_name: Ident) {
+        if self.token != token::Eq {
+            return;
+        }
+
+        self.diagnostic()
+            .struct_span_err(self.token.span, "expected `:`, found `=`")
+            .span_suggestion(
+                field_name.span.shrink_to_hi().to(self.token.span),
+                "replace equals symbol with a colon",
+                ":".to_string(),
+                Applicability::MachineApplicable,
+            )
+            .emit();
+    }
+
     fn err_dotdotdot_syntax(&self, span: Span) {
         self.struct_span_err(span, "unexpected token: `...`")
             .span_suggestion(