Skip to content

Commit 4e51e67

Browse files
Better recovery
1 parent e639212 commit 4e51e67

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

compiler/rustc_parse/src/parser/generics.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,20 @@ impl<'a> Parser<'a> {
353353
if !self.eat_keyword(exp!(Where)) {
354354
return Ok((where_clause, None));
355355
}
356+
357+
if self.eat_noexpect(&token::Colon) {
358+
let colon_span = self.prev_token.span;
359+
self.dcx()
360+
.struct_span_err(colon_span, "unexpected colon after `where`")
361+
.with_span_suggestion_short(
362+
colon_span,
363+
"remove the colon",
364+
"",
365+
Applicability::MachineApplicable,
366+
)
367+
.emit();
368+
}
369+
356370
where_clause.has_where_token = true;
357371
let where_lo = self.prev_token.span;
358372

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
pub enum Foo<T>
22
where:
3-
//~^ ERROR expected one of `#`, `{`, lifetime, or type, found `:`
3+
//~^ ERROR unexpected colon after `where`
44
T: Missing, {}
5+
//~^ ERROR cannot find trait `Missing` in this scope
6+
// (evidence that we continue parsing after the erroneous colon)
57

68
fn main() {}
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
error: expected one of `#`, `{`, lifetime, or type, found `:`
1+
error: unexpected colon after `where`
22
--> $DIR/recover-enum-with-bad-where.rs:2:6
33
|
4-
LL | pub enum Foo<T>
5-
| --- while parsing this enum
64
LL | where:
7-
| ^ expected one of `#`, `{`, lifetime, or type
5+
| ^ help: remove the colon
86

9-
error: aborting due to 1 previous error
7+
error[E0405]: cannot find trait `Missing` in this scope
8+
--> $DIR/recover-enum-with-bad-where.rs:4:8
9+
|
10+
LL | T: Missing, {}
11+
| ^^^^^^^ not found in this scope
12+
13+
error: aborting due to 2 previous errors
1014

15+
For more information about this error, try `rustc --explain E0405`.

0 commit comments

Comments
 (0)