Skip to content

Commit 532dd44

Browse files
committed
Recover from missing comma between enum variants
1 parent 7cf074a commit 532dd44

File tree

5 files changed

+63
-22
lines changed

5 files changed

+63
-22
lines changed

src/libsyntax/parse/parser.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -7711,11 +7711,29 @@ impl<'a> Parser<'a> {
77117711
};
77127712
variants.push(respan(vlo.to(self.prev_span), vr));
77137713

7714-
if !self.eat(&token::Comma) { break; }
7714+
if !self.eat(&token::Comma) {
7715+
if self.token.is_ident() &&
7716+
!self.token.is_special_ident() &&
7717+
!self.token.is_used_keyword() &&
7718+
!self.token.is_unused_keyword()
7719+
{
7720+
let sp = self.sess.source_map().next_point(self.prev_span);
7721+
let mut err = self.struct_span_err(sp, "missing comma");
7722+
err.span_suggestion_short(
7723+
sp,
7724+
"missing comma",
7725+
",".to_owned(),
7726+
Applicability::MaybeIncorrect,
7727+
);
7728+
err.emit();
7729+
} else {
7730+
break;
7731+
}
7732+
}
77157733
}
77167734
self.expect(&token::CloseDelim(token::Brace))?;
77177735
if !any_disr.is_empty() && !all_nullary {
7718-
let mut err =self.struct_span_err(
7736+
let mut err = self.struct_span_err(
77197737
any_disr.clone(),
77207738
"discriminator values can only be used with a field-less enum",
77217739
);

src/test/ui/issues/issue-28433.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// compile-flags: -Z continue-parse-after-error
22

3-
enum bird {
4-
pub duck,
5-
//~^ ERROR: expected identifier, found keyword `pub`
6-
//~| ERROR: expected
7-
goose
3+
enum Bird {
4+
pub Duck,
5+
//~^ ERROR expected identifier, found keyword `pub`
6+
//~| ERROR missing comma
7+
//~| WARN variant `pub` should have an upper camel case name
8+
Goose
89
}
910

1011

1112
fn main() {
12-
let y = bird::goose;
13+
let y = Bird::Goose;
1314
}

src/test/ui/issues/issue-28433.stderr

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
error: expected identifier, found keyword `pub`
22
--> $DIR/issue-28433.rs:4:5
33
|
4-
LL | pub duck,
4+
LL | pub Duck,
55
| ^^^ expected identifier, found keyword
66
help: you can escape reserved keywords to use them as identifiers
77
|
8-
LL | r#pub duck,
8+
LL | r#pub Duck,
99
| ^^^^^
1010

11-
error: expected one of `(`, `,`, `=`, `{`, or `}`, found `duck`
12-
--> $DIR/issue-28433.rs:4:9
11+
error: missing comma
12+
--> $DIR/issue-28433.rs:4:8
1313
|
14-
LL | pub duck,
15-
| ^^^^ expected one of `(`, `,`, `=`, `{`, or `}` here
14+
LL | pub Duck,
15+
| ^ help: missing comma
16+
17+
warning: variant `pub` should have an upper camel case name
18+
--> $DIR/issue-28433.rs:4:5
19+
|
20+
LL | pub Duck,
21+
| ^^^ help: convert the identifier to upper camel case: `Pub`
22+
|
23+
= note: #[warn(non_camel_case_types)] on by default
1624

1725
error: aborting due to 2 previous errors
1826

src/test/ui/parser/recover-enum.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
fn main() {
44
enum Test {
55
Very
6-
Bad //~ ERROR found `Bad`
7-
Stuff
6+
//~^ ERROR missing comma
7+
Bad(usize)
8+
//~^ ERROR missing comma
9+
Stuff { a: usize }
10+
//~^ ERROR missing comma
11+
Here
812
}
913
}
+16-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
error: expected one of `(`, `,`, `=`, `{`, or `}`, found `Bad`
2-
--> $DIR/recover-enum.rs:6:9
1+
error: missing comma
2+
--> $DIR/recover-enum.rs:5:13
33
|
44
LL | Very
5-
| - expected one of `(`, `,`, `=`, `{`, or `}` here
6-
LL | Bad
7-
| ^^^ unexpected token
5+
| ^ help: missing comma
86

9-
error: aborting due to previous error
7+
error: missing comma
8+
--> $DIR/recover-enum.rs:7:19
9+
|
10+
LL | Bad(usize)
11+
| ^ help: missing comma
12+
13+
error: missing comma
14+
--> $DIR/recover-enum.rs:9:27
15+
|
16+
LL | Stuff { a: usize }
17+
| ^ help: missing comma
18+
19+
error: aborting due to 3 previous errors
1020

0 commit comments

Comments
 (0)