From 4e51e67a24d940d3c8069062d07f663ee2034ac3 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 26 Jun 2025 18:08:15 +0000 Subject: [PATCH] Better recovery --- compiler/rustc_parse/src/parser/generics.rs | 14 ++++++++++++++ .../structs-enums/recover-enum-with-bad-where.rs | 4 +++- .../recover-enum-with-bad-where.stderr | 15 ++++++++++----- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_parse/src/parser/generics.rs b/compiler/rustc_parse/src/parser/generics.rs index c05479feb611..af1d1a1ec669 100644 --- a/compiler/rustc_parse/src/parser/generics.rs +++ b/compiler/rustc_parse/src/parser/generics.rs @@ -353,6 +353,20 @@ impl<'a> Parser<'a> { if !self.eat_keyword(exp!(Where)) { return Ok((where_clause, None)); } + + if self.eat_noexpect(&token::Colon) { + let colon_span = self.prev_token.span; + self.dcx() + .struct_span_err(colon_span, "unexpected colon after `where`") + .with_span_suggestion_short( + colon_span, + "remove the colon", + "", + Applicability::MachineApplicable, + ) + .emit(); + } + where_clause.has_where_token = true; let where_lo = self.prev_token.span; diff --git a/tests/ui/structs-enums/recover-enum-with-bad-where.rs b/tests/ui/structs-enums/recover-enum-with-bad-where.rs index 850ee6caa1a7..cf7747d710b5 100644 --- a/tests/ui/structs-enums/recover-enum-with-bad-where.rs +++ b/tests/ui/structs-enums/recover-enum-with-bad-where.rs @@ -1,6 +1,8 @@ pub enum Foo where: -//~^ ERROR expected one of `#`, `{`, lifetime, or type, found `:` +//~^ ERROR unexpected colon after `where` T: Missing, {} +//~^ ERROR cannot find trait `Missing` in this scope +// (evidence that we continue parsing after the erroneous colon) fn main() {} diff --git a/tests/ui/structs-enums/recover-enum-with-bad-where.stderr b/tests/ui/structs-enums/recover-enum-with-bad-where.stderr index 6f28ff56bab7..30b73f59e8c0 100644 --- a/tests/ui/structs-enums/recover-enum-with-bad-where.stderr +++ b/tests/ui/structs-enums/recover-enum-with-bad-where.stderr @@ -1,10 +1,15 @@ -error: expected one of `#`, `{`, lifetime, or type, found `:` +error: unexpected colon after `where` --> $DIR/recover-enum-with-bad-where.rs:2:6 | -LL | pub enum Foo - | --- while parsing this enum LL | where: - | ^ expected one of `#`, `{`, lifetime, or type + | ^ help: remove the colon -error: aborting due to 1 previous error +error[E0405]: cannot find trait `Missing` in this scope + --> $DIR/recover-enum-with-bad-where.rs:4:8 + | +LL | T: Missing, {} + | ^^^^^^^ not found in this scope +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0405`.