Skip to content

Commit f7aa5c2

Browse files
authored
Rollup merge of rust-lang#111449 - compiler-errors:recover-impl-generics-correctly, r=Nilstrieb
Recover `impl<T ?Sized>` correctly Fixes rust-lang#111327 r? `@Nilstrieb` but you can re-roll Alternatively, happy to close this if we're okay with just saying "sorry rust-lang#111327 is just a poor side-effect of parser ambiguity" 🤷
2 parents d990e43 + a5763ff commit f7aa5c2

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

compiler/rustc_parse/src/parser/generics.rs

+5
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ impl<'a> Parser<'a> {
453453
// `<` (LIFETIME|IDENT) `:` - generic parameter with bounds
454454
// `<` (LIFETIME|IDENT) `=` - generic parameter with a default
455455
// `<` const - generic const parameter
456+
// `<` IDENT `?` - RECOVERY for `impl<T ?Bound` missing a `:`, meant to
457+
// avoid the `T?` to `Option<T>` recovery for types.
456458
// The only truly ambiguous case is
457459
// `<` IDENT `>` `::` IDENT ...
458460
// we disambiguate it in favor of generics (`impl<T> ::absolute::Path<T> { ... }`)
@@ -463,6 +465,9 @@ impl<'a> Parser<'a> {
463465
|| self.look_ahead(start + 1, |t| t.is_lifetime() || t.is_ident())
464466
&& self.look_ahead(start + 2, |t| {
465467
matches!(t.kind, token::Gt | token::Comma | token::Colon | token::Eq)
468+
// Recovery-only branch -- this could be removed,
469+
// since it only affects diagnostics currently.
470+
|| matches!(t.kind, token::Question)
466471
})
467472
|| self.is_keyword_ahead(start + 1, &[kw::Const]))
468473
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
trait Tr {}
2+
3+
impl<T ?Sized> Tr for T {}
4+
//~^ ERROR expected one of `,`, `:`, `=`, or `>`, found `?`
5+
6+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected one of `,`, `:`, `=`, or `>`, found `?`
2+
--> $DIR/impl-on-unsized-typo.rs:3:8
3+
|
4+
LL | impl<T ?Sized> Tr for T {}
5+
| ^ expected one of `,`, `:`, `=`, or `>`
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)