about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Howell <michael@notriddle.com>2022-07-07 15:20:08 -0700
committerMichael Howell <michael@notriddle.com>2022-07-07 15:20:08 -0700
commit6713dde898ef81042a51d940d36150c52cb697b6 (patch)
treeb188e771ae70c770fa730a17d23cecd2855f6ef0
parentd496a4f8bb26709e58ce8821de07f7078d2f615d (diff)
downloadrust-6713dde898ef81042a51d940d36150c52cb697b6.tar.gz
rust-6713dde898ef81042a51d940d36150c52cb697b6.zip
diagnostics: suggest naming a field after failing to parse
-rw-r--r--compiler/rustc_parse/src/parser/expr.rs13
-rw-r--r--src/test/ui/parser/issues/issue-52496.stderr5
-rw-r--r--src/test/ui/parser/issues/issue-62973.stderr14
3 files changed, 27 insertions, 5 deletions
diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs
index 36d63378550..f2765e4997c 100644
--- a/compiler/rustc_parse/src/parser/expr.rs
+++ b/compiler/rustc_parse/src/parser/expr.rs
@@ -3037,6 +3037,19 @@ impl<'a> Parser<'a> {
                                 ",",
                                 Applicability::MachineApplicable,
                             );
+                        } else if is_shorthand
+                            && (AssocOp::from_token(&self.token).is_some()
+                                || matches!(&self.token.kind, token::OpenDelim(_))
+                                || self.token.kind == token::Dot)
+                        {
+                            // Looks like they tried to write a shorthand, complex expression.
+                            let ident = parsed_field.expect("is_shorthand implies Some").ident;
+                            e.span_suggestion(
+                                ident.span.shrink_to_lo(),
+                                "try naming a field",
+                                &format!("{ident}: "),
+                                Applicability::HasPlaceholders,
+                            );
                         }
                     }
                     if !recover {
diff --git a/src/test/ui/parser/issues/issue-52496.stderr b/src/test/ui/parser/issues/issue-52496.stderr
index cf5c621020f..77335c64c21 100644
--- a/src/test/ui/parser/issues/issue-52496.stderr
+++ b/src/test/ui/parser/issues/issue-52496.stderr
@@ -8,8 +8,9 @@ error: expected one of `,`, `:`, or `}`, found `.`
   --> $DIR/issue-52496.rs:8:22
    |
 LL |     let _ = Foo { bar.into(), bat: -1, . };
-   |             ---      ^ expected one of `,`, `:`, or `}`
-   |             |
+   |             ---   -  ^ expected one of `,`, `:`, or `}`
+   |             |     |
+   |             |     help: try naming a field: `bar:`
    |             while parsing this struct
 
 error: expected identifier, found `.`
diff --git a/src/test/ui/parser/issues/issue-62973.stderr b/src/test/ui/parser/issues/issue-62973.stderr
index d526c9d0bcc..4737bc71860 100644
--- a/src/test/ui/parser/issues/issue-62973.stderr
+++ b/src/test/ui/parser/issues/issue-62973.stderr
@@ -24,11 +24,19 @@ error: expected one of `,`, `:`, or `}`, found `{`
   --> $DIR/issue-62973.rs:6:8
    |
 LL | fn p() { match s { v, E { [) {) }
-   |        ^       -       -^ expected one of `,`, `:`, or `}`
-   |        |       |       |
-   |        |       |       help: `}` may belong here
+   |        ^       -        ^ expected one of `,`, `:`, or `}`
+   |        |       |
    |        |       while parsing this struct
    |        unclosed delimiter
+   |
+help: `}` may belong here
+   |
+LL | fn p() { match s { v, E} { [) {) }
+   |                        +
+help: try naming a field
+   |
+LL | fn p() { match s { v, E: E { [) {) }
+   |                       ++
 
 error: struct literals are not allowed here
   --> $DIR/issue-62973.rs:6:16