summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2020-02-15 09:45:49 +0100
committerGitHub <noreply@github.com>2020-02-15 09:45:49 +0100
commite9db0613acb2dfdb6a892873d59d0626b20c6cc8 (patch)
tree72b0aff59d492542867e747604c2abee64856211
parentRollup merge of #69166 - JohnTitor:ice-const-enum, r=matthewjasper (diff)
parentSuggest a comma if a struct initializer field fails to parse (diff)
downloadrust-e9db0613acb2dfdb6a892873d59d0626b20c6cc8.tar.gz
rust-e9db0613acb2dfdb6a892873d59d0626b20c6cc8.tar.bz2
rust-e9db0613acb2dfdb6a892873d59d0626b20c6cc8.tar.xz
Rollup merge of #69180 - Aaron1011:feature/comma-struct-init, r=petrochenkov
Suggest a comma if a struct initializer field fails to parse Currently, we emit a "try adding a comma" suggestion if a comma is missing in a struct definition. However, we emit no such suggestion if a comma is missing in a struct initializer. This commit adds a "try adding a comma" suggestion when we don't find a comma during the parsing of a struct initializer field. The change to `src/test/ui/parser/removed-syntax-with-1.stderr` isn't great, but I don't see a good way of avoiding it.
-rw-r--r--src/librustc_parse/parser/expr.rs8
-rw-r--r--src/test/ui/parser/removed-syntax-with-1.stderr5
-rw-r--r--src/test/ui/suggestions/struct-initializer-comma.rs13
-rw-r--r--src/test/ui/suggestions/struct-initializer-comma.stderr23
4 files changed, 46 insertions, 3 deletions
diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs
index 5a4225e..20b9df0 100644
--- a/src/librustc_parse/parser/expr.rs
+++ b/src/librustc_parse/parser/expr.rs
@@ -1832,10 +1832,16 @@ impl<'a> Parser<'a> {
1832 } 1832 }
1833 } 1833 }
1834 Err(mut e) => { 1834 Err(mut e) => {
1835 e.span_label(struct_sp, "while parsing this struct");
1835 if let Some(f) = recovery_field { 1836 if let Some(f) = recovery_field {
1836 fields.push(f); 1837 fields.push(f);
1838 e.span_suggestion(
1839 self.prev_span.shrink_to_hi(),
1840 "try adding a comma",
1841 ",".into(),
1842 Applicability::MachineApplicable,
1843 );
1837 } 1844 }
1838 e.span_label(struct_sp, "while parsing this struct");
1839 e.emit(); 1845 e.emit();
1840 self.recover_stmt_(SemiColonMode::Comma, BlockMode::Ignore); 1846 self.recover_stmt_(SemiColonMode::Comma, BlockMode::Ignore);
1841 self.eat(&token::Comma); 1847 self.eat(&token::Comma);
diff --git a/src/test/ui/parser/removed-syntax-with-1.stderr b/src/test/ui/parser/removed-syntax-with-1.stderr
index 193138d..c3f747b 100644
--- a/src/test/ui/parser/removed-syntax-with-1.stderr
+++ b/src/test/ui/parser/removed-syntax-with-1.stderr
@@ -2,8 +2,9 @@ error: expected one of `,`, `.`, `?`, `}`, or an operator, found `with`
2 --> $DIR/removed-syntax-with-1.rs:8:25 2 --> $DIR/removed-syntax-with-1.rs:8:25
3 | 3 |
4LL | let b = S { foo: () with a, bar: () }; 4LL | let b = S { foo: () with a, bar: () };
5 | - ^^^^ expected one of `,`, `.`, `?`, `}`, or an operator 5 | - -^^^^ expected one of `,`, `.`, `?`, `}`, or an operator
6 | | 6 | | |
7 | | help: try adding a comma: `,`
7 | while parsing this struct 8 | while parsing this struct
8 9
9error: aborting due to previous error 10error: aborting due to previous error
diff --git a/src/test/ui/suggestions/struct-initializer-comma.rs b/src/test/ui/suggestions/struct-initializer-comma.rs
new file mode 100644
index 0000000..613b976
--- /dev/null
+++ b/src/test/ui/suggestions/struct-initializer-comma.rs
@@ -0,0 +1,13 @@
1struct Foo {
2 first: bool,
3 second: u8,
4}
5
6fn main() {
7 let a = Foo {
8 //~^ ERROR missing field
9 first: true
10 second: 25
11 //~^ ERROR expected one of
12 };
13}
diff --git a/src/test/ui/suggestions/struct-initializer-comma.stderr b/src/test/ui/suggestions/struct-initializer-comma.stderr
new file mode 100644
index 0000000..731e8e1
--- /dev/null
+++ b/src/test/ui/suggestions/struct-initializer-comma.stderr
@@ -0,0 +1,23 @@
1error: expected one of `,`, `.`, `?`, `}`, or an operator, found `second`
2 --> $DIR/struct-initializer-comma.rs:10:9
3 |
4LL | let a = Foo {
5 | --- while parsing this struct
6LL |
7LL | first: true
8 | -
9 | |
10 | expected one of `,`, `.`, `?`, `}`, or an operator
11 | help: try adding a comma: `,`
12LL | second: 25
13 | ^^^^^^ unexpected token
14
15error[E0063]: missing field `second` in initializer of `Foo`
16 --> $DIR/struct-initializer-comma.rs:7:13
17 |
18LL | let a = Foo {
19 | ^^^ missing `second`
20
21error: aborting due to 2 previous errors
22
23For more information about this error, try `rustc --explain E0063`.