From c6558c0bc71024b8aca1db317c15cdf701d917d8 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 29 Jul 2022 20:04:28 +0400 Subject: [PATCH 1/3] Recover keywords in bounds For example, this fixes a error for `impl fn()` (notice the capitalization) --- compiler/rustc_parse/src/parser/ty.rs | 8 +++++++- .../ui/rfc-2632-const-trait-impl/without-tilde.rs | 3 ++- .../rfc-2632-const-trait-impl/without-tilde.stderr | 12 +++++++++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index 31b40a83e6052..b76ba8ff2d98c 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -640,7 +640,13 @@ impl<'a> Parser<'a> { let mut bounds = Vec::new(); let mut negative_bounds = Vec::new(); - while self.can_begin_bound() || self.token.is_keyword(kw::Dyn) { + while self.can_begin_bound() + // Continue even if we find a keyword. + // This is necessary for error recover on, for example, `impl fn()`. + // + // The only keyword that can go after generic bounds is `where`, so stop if it's it. + || (self.token.is_reserved_ident() && !self.token.is_keyword(kw::Where)) + { if self.token.is_keyword(kw::Dyn) { // Account for `&dyn Trait + dyn Other`. self.struct_span_err(self.token.span, "invalid `dyn` keyword") diff --git a/src/test/ui/rfc-2632-const-trait-impl/without-tilde.rs b/src/test/ui/rfc-2632-const-trait-impl/without-tilde.rs index e8b261545499f..e0d3e0b497b3f 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/without-tilde.rs +++ b/src/test/ui/rfc-2632-const-trait-impl/without-tilde.rs @@ -3,4 +3,5 @@ #![feature(const_trait_impl)] struct S; -//~^ ERROR expected one of `!`, `(`, `,`, `=`, `>`, `?`, `for`, `~`, lifetime, or path +//~^ ERROR expected identifier, found keyword `const` +//~| ERROR expected one of `(`, `+`, `,`, `::`, `<`, `=`, or `>`, found `Tr` diff --git a/src/test/ui/rfc-2632-const-trait-impl/without-tilde.stderr b/src/test/ui/rfc-2632-const-trait-impl/without-tilde.stderr index b6b77ac4a2fb6..243f0979509bf 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/without-tilde.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/without-tilde.stderr @@ -1,8 +1,14 @@ -error: expected one of `!`, `(`, `,`, `=`, `>`, `?`, `for`, `~`, lifetime, or path, found keyword `const` +error: expected identifier, found keyword `const` --> $DIR/without-tilde.rs:5:13 | LL | struct S; - | ^^^^^ expected one of 10 possible tokens + | ^^^^^ expected identifier, found keyword -error: aborting due to previous error +error: expected one of `(`, `+`, `,`, `::`, `<`, `=`, or `>`, found `Tr` + --> $DIR/without-tilde.rs:5:19 + | +LL | struct S; + | ^^ expected one of 7 possible tokens + +error: aborting due to 2 previous errors From 798016c2df3a15da739e8c6d0968ba0f7b54e303 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Fri, 29 Jul 2022 21:59:08 +0400 Subject: [PATCH 2/3] add a silly test for keywords in trait bounds recovery --- src/test/ui/parser/kw-in-trait-bounds.rs | 47 +++++ src/test/ui/parser/kw-in-trait-bounds.stderr | 171 +++++++++++++++++++ 2 files changed, 218 insertions(+) create mode 100644 src/test/ui/parser/kw-in-trait-bounds.rs create mode 100644 src/test/ui/parser/kw-in-trait-bounds.stderr diff --git a/src/test/ui/parser/kw-in-trait-bounds.rs b/src/test/ui/parser/kw-in-trait-bounds.rs new file mode 100644 index 0000000000000..fa037e5937dca --- /dev/null +++ b/src/test/ui/parser/kw-in-trait-bounds.rs @@ -0,0 +1,47 @@ +// edition:2018 + +fn _f(_: impl fn(), _: &dyn fn()) +//~^ ERROR expected identifier, found keyword `fn` +//~| ERROR expected identifier, found keyword `fn` +//~| ERROR expected identifier, found keyword `fn` +//~| ERROR cannot find trait `r#fn` in this scope +//~| ERROR cannot find trait `r#fn` in this scope +//~| ERROR cannot find trait `r#fn` in this scope +//~| HELP a trait with a similar name exists +//~| HELP a trait with a similar name exists +//~| HELP a trait with a similar name exists +//~| HELP escape `fn` to use it as an identifier +//~| HELP escape `fn` to use it as an identifier +//~| HELP escape `fn` to use it as an identifier +where +G: fn(), + //~^ ERROR expected identifier, found keyword `fn` + //~| ERROR cannot find trait `r#fn` in this scope + //~| HELP a trait with a similar name exists + //~| HELP escape `fn` to use it as an identifier +{} + +fn _g(_: impl struct, _: &dyn struct) +//~^ ERROR expected identifier, found keyword `struct` +//~| ERROR expected identifier, found keyword `struct` +//~| ERROR expected identifier, found keyword `struct` +//~| ERROR cannot find trait `r#struct` in this scope +//~| ERROR cannot find trait `r#struct` in this scope +//~| ERROR cannot find trait `r#struct` in this scope +//~| HELP a trait with a similar name exists +//~| HELP a trait with a similar name exists +//~| HELP a trait with a similar name exists +//~| HELP escape `struct` to use it as an identifier +//~| HELP escape `struct` to use it as an identifier +//~| HELP escape `struct` to use it as an identifier +where + B: struct, + //~^ ERROR expected identifier, found keyword `struct` + //~| ERROR cannot find trait `r#struct` in this scope + //~| HELP a trait with a similar name exists + //~| HELP escape `struct` to use it as an identifier +{} + +trait Struct {} + +fn main() {} diff --git a/src/test/ui/parser/kw-in-trait-bounds.stderr b/src/test/ui/parser/kw-in-trait-bounds.stderr new file mode 100644 index 0000000000000..28196c7ce2de8 --- /dev/null +++ b/src/test/ui/parser/kw-in-trait-bounds.stderr @@ -0,0 +1,171 @@ +error: expected identifier, found keyword `fn` + --> $DIR/kw-in-trait-bounds.rs:3:10 + | +LL | fn _f(_: impl fn(), _: &dyn fn()) + | ^^ expected identifier, found keyword + | +help: escape `fn` to use it as an identifier + | +LL | fn _f(_: impl fn(), _: &dyn fn()) + | ++ + +error: expected identifier, found keyword `fn` + --> $DIR/kw-in-trait-bounds.rs:3:27 + | +LL | fn _f(_: impl fn(), _: &dyn fn()) + | ^^ expected identifier, found keyword + | +help: escape `fn` to use it as an identifier + | +LL | fn _f(_: impl r#fn(), _: &dyn fn()) + | ++ + +error: expected identifier, found keyword `fn` + --> $DIR/kw-in-trait-bounds.rs:3:41 + | +LL | fn _f(_: impl fn(), _: &dyn fn()) + | ^^ expected identifier, found keyword + | +help: escape `fn` to use it as an identifier + | +LL | fn _f(_: impl fn(), _: &dyn r#fn()) + | ++ + +error: expected identifier, found keyword `fn` + --> $DIR/kw-in-trait-bounds.rs:17:4 + | +LL | G: fn(), + | ^^ expected identifier, found keyword + | +help: escape `fn` to use it as an identifier + | +LL | G: r#fn(), + | ++ + +error: expected identifier, found keyword `struct` + --> $DIR/kw-in-trait-bounds.rs:24:10 + | +LL | fn _g(_: impl struct, _: &dyn struct) + | ^^^^^^ expected identifier, found keyword + | +help: escape `struct` to use it as an identifier + | +LL | fn _g(_: impl struct, _: &dyn struct) + | ++ + +error: expected identifier, found keyword `struct` + --> $DIR/kw-in-trait-bounds.rs:24:29 + | +LL | fn _g(_: impl struct, _: &dyn struct) + | ^^^^^^ expected identifier, found keyword + | +help: escape `struct` to use it as an identifier + | +LL | fn _g(_: impl r#struct, _: &dyn struct) + | ++ + +error: expected identifier, found keyword `struct` + --> $DIR/kw-in-trait-bounds.rs:24:45 + | +LL | fn _g(_: impl struct, _: &dyn struct) + | ^^^^^^ expected identifier, found keyword + | +help: escape `struct` to use it as an identifier + | +LL | fn _g(_: impl struct, _: &dyn r#struct) + | ++ + +error: expected identifier, found keyword `struct` + --> $DIR/kw-in-trait-bounds.rs:38:8 + | +LL | B: struct, + | ^^^^^^ expected identifier, found keyword + | +help: escape `struct` to use it as an identifier + | +LL | B: r#struct, + | ++ + +error[E0405]: cannot find trait `r#fn` in this scope + --> $DIR/kw-in-trait-bounds.rs:3:10 + | +LL | fn _f(_: impl fn(), _: &dyn fn()) + | ^^ help: a trait with a similar name exists (notice the capitalization): `Fn` + | + ::: $SRC_DIR/core/src/ops/function.rs:LL:COL + | +LL | pub trait Fn: FnMut { + | ------------------------------- similarly named trait `Fn` defined here + +error[E0405]: cannot find trait `r#fn` in this scope + --> $DIR/kw-in-trait-bounds.rs:17:4 + | +LL | G: fn(), + | ^^ help: a trait with a similar name exists (notice the capitalization): `Fn` + | + ::: $SRC_DIR/core/src/ops/function.rs:LL:COL + | +LL | pub trait Fn: FnMut { + | ------------------------------- similarly named trait `Fn` defined here + +error[E0405]: cannot find trait `r#fn` in this scope + --> $DIR/kw-in-trait-bounds.rs:3:27 + | +LL | fn _f(_: impl fn(), _: &dyn fn()) + | ^^ help: a trait with a similar name exists (notice the capitalization): `Fn` + | + ::: $SRC_DIR/core/src/ops/function.rs:LL:COL + | +LL | pub trait Fn: FnMut { + | ------------------------------- similarly named trait `Fn` defined here + +error[E0405]: cannot find trait `r#fn` in this scope + --> $DIR/kw-in-trait-bounds.rs:3:41 + | +LL | fn _f(_: impl fn(), _: &dyn fn()) + | ^^ help: a trait with a similar name exists (notice the capitalization): `Fn` + | + ::: $SRC_DIR/core/src/ops/function.rs:LL:COL + | +LL | pub trait Fn: FnMut { + | ------------------------------- similarly named trait `Fn` defined here + +error[E0405]: cannot find trait `r#struct` in this scope + --> $DIR/kw-in-trait-bounds.rs:24:10 + | +LL | fn _g(_: impl struct, _: &dyn struct) + | ^^^^^^ help: a trait with a similar name exists (notice the capitalization): `Struct` +... +LL | trait Struct {} + | ------------ similarly named trait `Struct` defined here + +error[E0405]: cannot find trait `r#struct` in this scope + --> $DIR/kw-in-trait-bounds.rs:38:8 + | +LL | B: struct, + | ^^^^^^ help: a trait with a similar name exists (notice the capitalization): `Struct` +... +LL | trait Struct {} + | ------------ similarly named trait `Struct` defined here + +error[E0405]: cannot find trait `r#struct` in this scope + --> $DIR/kw-in-trait-bounds.rs:24:29 + | +LL | fn _g(_: impl struct, _: &dyn struct) + | ^^^^^^ help: a trait with a similar name exists (notice the capitalization): `Struct` +... +LL | trait Struct {} + | ------------ similarly named trait `Struct` defined here + +error[E0405]: cannot find trait `r#struct` in this scope + --> $DIR/kw-in-trait-bounds.rs:24:45 + | +LL | fn _g(_: impl struct, _: &dyn struct) + | ^^^^^^ help: a trait with a similar name exists (notice the capitalization): `Struct` +... +LL | trait Struct {} + | ------------ similarly named trait `Struct` defined here + +error: aborting due to 16 previous errors + +For more information about this error, try `rustc --explain E0405`. From 5d5e451618d3f364ce4a19d1d2c4f8903bc30dde Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 21 Aug 2022 14:58:42 +0400 Subject: [PATCH 3/3] recover `const Tr` bounds (no `~`) --- compiler/rustc_parse/src/parser/ty.rs | 14 ++++++++++++++ .../ui/rfc-2632-const-trait-impl/without-tilde.rs | 3 +-- .../rfc-2632-const-trait-impl/without-tilde.stderr | 14 +++++--------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_parse/src/parser/ty.rs b/compiler/rustc_parse/src/parser/ty.rs index b76ba8ff2d98c..298198f2cf7d7 100644 --- a/compiler/rustc_parse/src/parser/ty.rs +++ b/compiler/rustc_parse/src/parser/ty.rs @@ -809,6 +809,20 @@ impl<'a> Parser<'a> { self.expect_keyword(kw::Const)?; let span = tilde.to(self.prev_token.span); self.sess.gated_spans.gate(sym::const_trait_impl, span); + Some(span) + } else if self.eat_keyword(kw::Const) { + let span = self.prev_token.span; + self.sess.gated_spans.gate(sym::const_trait_impl, span); + + self.struct_span_err(span, "const bounds must start with `~`") + .span_suggestion( + span.shrink_to_lo(), + "add `~`", + "~", + Applicability::MachineApplicable, + ) + .emit(); + Some(span) } else { None diff --git a/src/test/ui/rfc-2632-const-trait-impl/without-tilde.rs b/src/test/ui/rfc-2632-const-trait-impl/without-tilde.rs index e0d3e0b497b3f..d63381b5f2cc9 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/without-tilde.rs +++ b/src/test/ui/rfc-2632-const-trait-impl/without-tilde.rs @@ -3,5 +3,4 @@ #![feature(const_trait_impl)] struct S; -//~^ ERROR expected identifier, found keyword `const` -//~| ERROR expected one of `(`, `+`, `,`, `::`, `<`, `=`, or `>`, found `Tr` +//~^ ERROR const bounds must start with `~` diff --git a/src/test/ui/rfc-2632-const-trait-impl/without-tilde.stderr b/src/test/ui/rfc-2632-const-trait-impl/without-tilde.stderr index 243f0979509bf..31300354a5737 100644 --- a/src/test/ui/rfc-2632-const-trait-impl/without-tilde.stderr +++ b/src/test/ui/rfc-2632-const-trait-impl/without-tilde.stderr @@ -1,14 +1,10 @@ -error: expected identifier, found keyword `const` +error: const bounds must start with `~` --> $DIR/without-tilde.rs:5:13 | LL | struct S; - | ^^^^^ expected identifier, found keyword + | -^^^^ + | | + | help: add `~`: `~` -error: expected one of `(`, `+`, `,`, `::`, `<`, `=`, or `>`, found `Tr` - --> $DIR/without-tilde.rs:5:19 - | -LL | struct S; - | ^^ expected one of 7 possible tokens - -error: aborting due to 2 previous errors +error: aborting due to previous error