about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-01-24 18:25:49 +0100
committerGitHub <noreply@github.com>2019-01-24 18:25:49 +0100
commit1a3b3d429873b0f6926a98e4f0a4b78a5230c925 (patch)
treefd129a1094037513da75df834a81af81cb906c4a /src/libsyntax
parent5b288ae0eb26f5d17edd6ca4a2d0c5f28be81674 (diff)
parentf14d007ee4f4b69cdb3880db49608b432bea8352 (diff)
downloadrust-1a3b3d429873b0f6926a98e4f0a4b78a5230c925.tar.gz
rust-1a3b3d429873b0f6926a98e4f0a4b78a5230c925.zip
Rollup merge of #57863 - davidtwco:issue-57684, r=estebank
Add suggestion for incorrect field syntax.

Fixes #57684.

This commit adds a suggestion when a `=` character is used when
specifying the value of a field in a struct constructor incorrectly
instead of a `:` character.

r? @estebank
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/parse/parser.rs18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index c7e33a16564..9f1a9bfa6e6 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -2322,8 +2322,24 @@ impl<'a> Parser<'a> {
         let lo = self.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) {
+        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.span, "expected `:`, found `=`")
+                    .span_suggestion_with_applicability(
+                        fieldname.span.shrink_to_hi().to(self.span),
+                        "replace equals symbol with a colon",
+                        ":".to_string(),
+                        Applicability::MachineApplicable,
+                    )
+                    .emit();
+            }
             self.bump(); // `:`
             (fieldname, self.parse_expr()?, false)
         } else {