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

Some errors could be better handled by detecting a pattern #101291

Closed
Canop opened this issue Sep 1, 2022 · 6 comments
Closed

Some errors could be better handled by detecting a pattern #101291

Canop opened this issue Sep 1, 2022 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Canop
Copy link
Contributor

Canop commented Sep 1, 2022

Exemple Use Case:

It's possibly common enough for people using several languages to apply the wrong, easy to recognize, construct.

Recognizing this construct in case an error is detected would avoid a sequence of suboptimal hints.

Here's the exemple of typing for let thing of some_iterator (JavaScript inspired):

image

A more relevant hint would suggest the typical Rust iteration.

Triage note:

@estebank suggested I create an issue here

@compiler-errors
Copy link
Member

I think this issue could be phrase a bit more specifically, i.e. "parse JS style for-loop iteration and suggest fixing it", unless you're suggesting that we try to parse all sorts of different for loop concepts from a diverse set of languages, which I think is going to be significantly harder.

@compiler-errors
Copy link
Member

Alternatively, this could be fixed with a few different error recoveries. First, we should recover from this:

for let ident in collection { .. }
//  ^^^ suggest removing this

Then we could also recover from:

for pattern of collection { .. }
         // ^^ suggest replacing this with `in`

@estebank
Copy link
Contributor

estebank commented Sep 1, 2022

Detecting let pattern after for is indeed one recovery. I believe we already have a of -> in check, but if not we can add it. Finally, I think it'd be nice if we could detect that we don't know for sure and "just" swallow the rest of the expression (find the next ; accounting for nested braces) or the end of the item (easier).

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Sep 5, 2022
…=cjgillot

Suggest removing unnecessary prefix let in patterns

Helps with rust-lang#101291, though I think `@estebank` probably wants this:

> Finally, I think it'd be nice if we could detect that we don't know for sure and "just" swallow the rest of the expression (find the next ; accounting for nested braces) or the end of the item (easier).

... to be implemented before we close that issue out completely.
bors added a commit to rust-lang-ci/rust that referenced this issue Sep 6, 2022
…jgillot

Suggest removing unnecessary prefix let in patterns

Helps with rust-lang#101291, though I think `@estebank` probably wants this:

> Finally, I think it'd be nice if we could detect that we don't know for sure and "just" swallow the rest of the expression (find the next ; accounting for nested braces) or the end of the item (easier).

... to be implemented before we close that issue out completely.
@BGR360
Copy link
Contributor

BGR360 commented Sep 12, 2022

@rustbot label A-diagnostics T-compiler

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 12, 2022
@veera-sivarajan
Copy link
Contributor

veera-sivarajan commented Sep 27, 2024

Output on rustc 1.81.0 without warnings:

error: expected pattern, found `let`
 --> <source>:3:9
  |
3 |     for let thing of collection {
  |         ^^^
  |
help: remove the unnecessary `let` keyword
  |
3 -     for let thing of collection {
3 +     for  thing of collection {
  |

error: missing `in` in `for` loop
 --> <source>:3:19
  |
3 |     for let thing of collection {
  |                   ^^
  |
help: try using `in` here instead
  |
3 |     for let thing in collection {
  |                   ~~

error: aborting due to 2 previous errors; 1 warning emitted

@estebank
Copy link
Contributor

estebank commented Oct 2, 2024

Given the current output, I feel like we can close this ticket, while also acknowledging that the space of possible parses of mistaken code hasn't been exhausted yet.

@estebank estebank closed this as completed Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants