]> git.lizzy.rs Git - rust.git/commit - src/tools/miri
Rollup merge of #99915 - WaffleLapkin:recover_keyword_bounds, r=compiler-errors
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>
Mon, 22 Aug 2022 06:15:42 +0000 (11:45 +0530)
committerGitHub <noreply@github.com>
Mon, 22 Aug 2022 06:15:42 +0000 (11:45 +0530)
commit3842117ef22287decfec9113a4b0180250cfbe79
treeb25465ca27dde98259882b7f508cef0ab24c50c3
parent33b5ce6433c58d4101118d58c3ae6aae0b9b1a5f
parent5d5e451618d3f364ce4a19d1d2c4f8903bc30dde
Rollup merge of #99915 - WaffleLapkin:recover_keyword_bounds, r=compiler-errors

Recover keywords in trait bounds

(_this pr was inspired by [this tweet](https://twitter.com/Azumanga/status/1552982326409367561)_)

Recover keywords in trait bound, motivational example:
```rust
fn f(_: impl fn()) {} // mistyped, meant `Fn`
```

<details><summary>Current nightly (3 needless and confusing errors!)</summary>
<p>

```text
error: expected identifier, found keyword `fn`
 --> ./t.rs:1:15
  |
1 | fn _f(_: impl fn()) {}
  |               ^^ expected identifier, found keyword
  |
help: escape `fn` to use it as an identifier
  |
1 | fn _f(_: impl r#fn()) {}
  |               ++

error: expected one of `:` or `|`, found `)`
 --> ./t.rs:1:19
  |
1 | fn _f(_: impl fn()) {}
  |                   ^ expected one of `:` or `|`

error: expected one of `!`, `(`, `)`, `,`, `?`, `for`, `~`, lifetime, or path, found keyword `fn`
 --> ./t.rs:1:15
  |
1 | fn _f(_: impl fn()) {}
  |              -^^ expected one of 9 possible tokens
  |              |
  |              help: missing `,`

error: at least one trait must be specified
 --> ./t.rs:1:10
  |
1 | fn _f(_: impl fn()) {}
  |          ^^^^
```

</p>
</details>

This PR:
```text
error: expected identifier, found keyword `fn`
 --> ./t.rs:1:15
  |
1 | fn _f(_: impl fn()) {}
  |               ^^ expected identifier, found keyword
  |
help: escape `fn` to use it as an identifier
  |
1 | fn _f(_: impl r#fn()) {}
  |               ++

error[E0405]: cannot find trait `r#fn` in this scope
  --> ./t.rs:1:15
   |
1  | fn _f(_: impl fn()) {}
   |               ^^ help: a trait with a similar name exists (notice the capitalization): `Fn`
   |
  ::: /home/waffle/projects/repos/rust/library/core/src/ops/function.rs:74:1
   |
74 | pub trait Fn<Args>: FnMut<Args> {
   | ------------------------------- similarly named trait `Fn` defined here
```

It would be nice to have suggestion in the first error like "have you meant `Fn` trait", instead of a separate error, but the recovery is deep inside ident parsing, which makes it a lot harder to do.

r? `@compiler-errors`
compiler/rustc_parse/src/parser/ty.rs