Skip to content
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

Recover impl<T ?Sized> correctly #111449

Merged
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
5 changes: 5 additions & 0 deletions compiler/rustc_parse/src/parser/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ impl<'a> Parser<'a> {
// `<` (LIFETIME|IDENT) `:` - generic parameter with bounds
// `<` (LIFETIME|IDENT) `=` - generic parameter with a default
// `<` const - generic const parameter
// `<` IDENT `?` - RECOVERY for `impl<T ?Bound` missing a `:`, meant to
// avoid the `T?` to `Option<T>` recovery for types.
// The only truly ambiguous case is
// `<` IDENT `>` `::` IDENT ...
// we disambiguate it in favor of generics (`impl<T> ::absolute::Path<T> { ... }`)
Expand All @@ -463,6 +465,9 @@ impl<'a> Parser<'a> {
|| self.look_ahead(start + 1, |t| t.is_lifetime() || t.is_ident())
&& self.look_ahead(start + 2, |t| {
matches!(t.kind, token::Gt | token::Comma | token::Colon | token::Eq)
// Recovery-only branch -- this could be removed,
// since it only affects diagnostics currently.
|| matches!(t.kind, token::Question)
})
|| self.is_keyword_ahead(start + 1, &[kw::Const]))
}
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/parser/impl-on-unsized-typo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
trait Tr {}

impl<T ?Sized> Tr for T {}
//~^ ERROR expected one of `,`, `:`, `=`, or `>`, found `?`

fn main() {}
8 changes: 8 additions & 0 deletions tests/ui/parser/impl-on-unsized-typo.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: expected one of `,`, `:`, `=`, or `>`, found `?`
--> $DIR/impl-on-unsized-typo.rs:3:8
|
LL | impl<T ?Sized> Tr for T {}
| ^ expected one of `,`, `:`, `=`, or `>`

error: aborting due to previous error