Skip to content

Conversation

WaffleLapkin
Copy link
Member

(this pr was inspired by this tweet)

Recover keywords in trait bound, motivational example:

fn f(_: impl fn()) {} // mistyped, meant `Fn`
Current nightly (3 needless and confusing errors!)

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()) {}
  |          ^^^^

This PR:

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

For example, this fixes a error for `impl fn()` (notice the capitalization)
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jul 29, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 29, 2022
@WaffleLapkin WaffleLapkin force-pushed the recover_keyword_bounds branch from e659dba to e6a4f79 Compare July 29, 2022 18:11
@rust-log-analyzer

This comment has been minimized.

@WaffleLapkin WaffleLapkin force-pushed the recover_keyword_bounds branch from e6a4f79 to 798016c Compare July 29, 2022 19:29
@compiler-errors compiler-errors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 11, 2022
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 `~`")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if ~const Trait is called a "const" bound technically-- I would call it a "const if const" bound. But whatever, should be fine.

@compiler-errors
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 22, 2022

📌 Commit 5d5e451 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 22, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 22, 2022
Rollup of 5 pull requests

Successful merges:

 - rust-lang#93162 (Std module docs improvements)
 - rust-lang#99386 (Add tests that check `Vec::retain` predicate execution order.)
 - rust-lang#99915 (Recover keywords in trait bounds)
 - rust-lang#100694 (Migrate rustc_ast_passes diagnostics to `SessionDiagnostic` and translatable messages (first part))
 - rust-lang#100757 (Catch overflow early)

Failed merges:

 - rust-lang#99917 (Move Error trait into core)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 3842117 into rust-lang:master Aug 22, 2022
@rustbot rustbot added this to the 1.65.0 milestone Aug 22, 2022
compiler-errors added a commit to compiler-errors/rust that referenced this pull request Feb 9, 2023
…macro-is-ok, r=estebank

Do not eagerly recover for bad `impl Trait` types in macros

Fixes rust-lang#107796

cc rust-lang#106712, `@estebank` and `@Ezrashaw` please make sure to use [`Parser::may_recover`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/parser/struct.Parser.html#method.may_recover) for all eager-token-consuming parser recoveries.

This also fixes a separate regression from rust-lang#99915, that was introduced before we added `may_recover` though.
compiler-errors added a commit to compiler-errors/rust that referenced this pull request Feb 9, 2023
…macro-is-ok, r=estebank

Do not eagerly recover for bad `impl Trait` types in macros

Fixes rust-lang#107796

cc rust-lang#106712, ``@estebank`` and ``@Ezrashaw`` please make sure to use [`Parser::may_recover`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/parser/struct.Parser.html#method.may_recover) for all eager-token-consuming parser recoveries.

This also fixes a separate regression from rust-lang#99915, that was introduced before we added `may_recover` though.
compiler-errors added a commit to compiler-errors/rust that referenced this pull request Feb 9, 2023
…macro-is-ok, r=estebank

Do not eagerly recover for bad `impl Trait` types in macros

Fixes rust-lang#107796

cc rust-lang#106712, ```@estebank``` and ```@Ezrashaw``` please make sure to use [`Parser::may_recover`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/parser/struct.Parser.html#method.may_recover) for all eager-token-consuming parser recoveries.

This also fixes a separate regression from rust-lang#99915, that was introduced before we added `may_recover` though.
@WaffleLapkin WaffleLapkin deleted the recover_keyword_bounds branch February 6, 2025 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants