about summary refs log tree commit diff
path: root/src/libsyntax/parse
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2017-10-13 23:37:57 +0800
committerGitHub <noreply@github.com>2017-10-13 23:37:57 +0800
commite6a6d980e086d88d7b87163a16834ceb72a1f9de (patch)
treeac1b0401e89e42fc36e13a41b75ddf196c38fbec /src/libsyntax/parse
parent46d86d38479546c0356103b1589b47a284767a26 (diff)
parent72cfd209410da7f9dd09357a3361bb5b561dee33 (diff)
downloadrust-e6a6d980e086d88d7b87163a16834ceb72a1f9de.tar.gz
rust-e6a6d980e086d88d7b87163a16834ceb72a1f9de.zip
Rollup merge of #45178 - Badel2:comma-after-struct, r=petrochenkov
Better error message for comma after base struct

#41834

This adds a better error for commas after the base struct:
```
let foo = Foo {
    one: 111,
    ..Foo::default(), // This comma is a syntax error
};
```

The current error is a generic `expected one of ...` which isn't beginner-friendly. My error looks like this:

```
error: cannot use a comma after the base struct
  --> tmp/example.rs:26:9
   |
26 |         ..Foo::default(),
   |         ^^^^^^^^^^^^^^^^- help: remove this comma
   |
   = note: the base struct expansion must always be the last field
```

I even added a note for people who don't know why this isn't allowed.
Diffstat (limited to 'src/libsyntax/parse')
-rw-r--r--src/libsyntax/parse/parser.rs11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 6dbda11e021..04e6cdcfe58 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -2314,6 +2314,7 @@ impl<'a> Parser<'a> {
 
         while self.token != token::CloseDelim(token::Brace) {
             if self.eat(&token::DotDot) {
+                let exp_span = self.prev_span;
                 match self.parse_expr() {
                     Ok(e) => {
                         base = Some(e);
@@ -2323,6 +2324,16 @@ impl<'a> Parser<'a> {
                         self.recover_stmt();
                     }
                 }
+                if self.token == token::Comma {
+                    let mut err = self.sess.span_diagnostic.mut_span_err(
+                        exp_span.to(self.prev_span),
+                        "cannot use a comma after the base struct",
+                    );
+                    err.span_suggestion_short(self.span, "remove this comma", "".to_owned());
+                    err.note("the base struct must always be the last field");
+                    err.emit();
+                    self.recover_stmt();
+                }
                 break;
             }