Skip to content

Improve recovery when users write where: #143065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ impl Token {
|| self.is_qpath_start()
|| matches!(self.is_metavar_seq(), Some(MetaVarKind::Path))
|| self.is_path_segment_keyword()
|| self.is_ident() && !self.is_reserved_ident()
|| self.is_non_reserved_ident()
}

/// Returns `true` if the token is a given keyword, `kw`.
Expand Down Expand Up @@ -937,6 +937,10 @@ impl Token {
self.is_non_raw_ident_where(Ident::is_reserved)
}

pub fn is_non_reserved_ident(&self) -> bool {
self.ident().is_some_and(|(id, raw)| raw == IdentIsRaw::Yes || !Ident::is_reserved(id))
}

/// Returns `true` if the token is the identifier `true` or `false`.
pub fn is_bool_lit(&self) -> bool {
self.is_non_raw_ident_where(|id| id.name.is_bool_lit())
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_ast/src/tokenstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,10 +634,8 @@ impl TokenStream {
(
TokenTree::Token(token_left, Spacing::Alone),
TokenTree::Token(token_right, _),
) if ((token_left.is_ident() && !token_left.is_reserved_ident())
|| token_left.is_lit())
&& ((token_right.is_ident() && !token_right.is_reserved_ident())
|| token_right.is_lit()) =>
) if (token_left.is_non_reserved_ident() || token_left.is_lit())
&& (token_right.is_non_reserved_ident() || token_right.is_lit()) =>
{
token_left.span
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2731,7 +2731,7 @@ impl<'a> Parser<'a> {
return first_pat;
}
if !matches!(first_pat.kind, PatKind::Ident(_, _, None) | PatKind::Path(..))
|| !self.look_ahead(1, |token| token.is_ident() && !token.is_reserved_ident())
|| !self.look_ahead(1, |token| token.is_non_reserved_ident())
{
let mut snapshot_type = self.create_snapshot_for_diagnostic();
snapshot_type.bump(); // `:`
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3875,8 +3875,7 @@ impl<'a> Parser<'a> {
// Check if a colon exists one ahead. This means we're parsing a fieldname.
let is_shorthand = !this.look_ahead(1, |t| t == &token::Colon || t == &token::Eq);
// Proactively check whether parsing the field will be incorrect.
let is_wrong = this.token.is_ident()
&& !this.token.is_reserved_ident()
let is_wrong = this.token.is_non_reserved_ident()
&& !this.look_ahead(1, |t| {
t == &token::Colon
|| t == &token::Eq
Expand Down
14 changes: 14 additions & 0 deletions compiler/rustc_parse/src/parser/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1552,7 +1552,8 @@ impl<'a> Parser<'a> {
})
.map_err(|mut err| {
err.span_label(ident.span, "while parsing this enum");
if self.token == token::Colon {
// Try to recover `enum Foo { ident : Ty }`.
if self.prev_token.is_non_reserved_ident() && self.token == token::Colon {
let snapshot = self.create_snapshot_for_diagnostic();
self.bump();
match self.parse_ty() {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ impl<'a> Parser<'a> {

/// Is the given keyword `kw` followed by a non-reserved identifier?
fn is_kw_followed_by_ident(&self, kw: Symbol) -> bool {
self.token.is_keyword(kw) && self.look_ahead(1, |t| t.is_ident() && !t.is_reserved_ident())
self.token.is_keyword(kw) && self.look_ahead(1, |t| t.is_non_reserved_ident())
}

#[inline]
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_parse/src/parser/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<'a> Parser<'a> {
/// ```
fn recover_colon_before_qpath_proj(&mut self) -> bool {
if !self.check_noexpect(&TokenKind::Colon)
|| self.look_ahead(1, |t| !t.is_ident() || t.is_reserved_ident())
|| self.look_ahead(1, |t| !t.is_non_reserved_ident())
{
return false;
}
Expand Down Expand Up @@ -260,7 +260,7 @@ impl<'a> Parser<'a> {
if self.may_recover()
&& style == PathStyle::Expr // (!)
&& self.token == token::Colon
&& self.look_ahead(1, |token| token.is_ident() && !token.is_reserved_ident())
&& self.look_ahead(1, |token| token.is_non_reserved_ident())
{
// Emit a special error message for `a::b:c` to help users
// otherwise, `a: c` might have meant to introduce a new binding
Expand Down Expand Up @@ -334,9 +334,7 @@ impl<'a> Parser<'a> {
self.expect_gt().map_err(|mut err| {
// Try to recover a `:` into a `::`
if self.token == token::Colon
&& self.look_ahead(1, |token| {
token.is_ident() && !token.is_reserved_ident()
})
&& self.look_ahead(1, |token| token.is_non_reserved_ident())
{
err.cancel();
err = self.dcx().create_err(PathSingleColon {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ impl<'a> Parser<'a> {
}
if self.prev_token.is_reserved_ident() && self.prev_token.is_ident_named(kw::Await) {
// Likely `foo.await bar`
} else if !self.prev_token.is_reserved_ident() && self.prev_token.is_ident() {
} else if self.prev_token.is_non_reserved_ident() {
// Likely `foo bar`
} else if self.prev_token.kind == token::Question {
// `foo? bar`
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/structs-enums/recover-enum-with-bad-where.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub enum Foo<T>
where:
//~^ 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() {}
15 changes: 15 additions & 0 deletions tests/ui/structs-enums/recover-enum-with-bad-where.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error: unexpected colon after `where`
--> $DIR/recover-enum-with-bad-where.rs:2:6
|
LL | where:
| ^ help: remove the colon

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`.
Loading