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

Better diagnostics for missing iterator in for-loop #78537

Closed
jyn514 opened this issue Oct 29, 2020 · 5 comments · Fixed by #107526
Closed

Better diagnostics for missing iterator in for-loop #78537

jyn514 opened this issue Oct 29, 2020 · 5 comments · Fixed by #107526
Assignees
Labels
A-control-flow Area: Control flow A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jyn514
Copy link
Member

jyn514 commented Oct 29, 2020

Spot the bug:

fn f() -> usize {
    for i in {
        println!("{}", i);
    }
    1
}

Unfortunately, instead of pointing to the missing iterator, rust points to the 1:

error: expected `{`, found `1`
 --> src/lib.rs:5:5
  |
5 |     1
  |     ^
  |     |
  |     expected `{`
  |     help: try placing this code inside a block: `{ 1 }`

I'd love it to instead say 'missing iterator for loop':

error: expected iterator, found block
 --> src/lib.rs
  |
2 |    for i in {
  |             ^ this starts a block expression ...
5 |     1
  |     ^ ... so there is no body for the loop
  =     help: try adding an iterator to the for-loop: `for i in iter {`

Other variants of this with less than ideal errors:

fn f() {
    for i in {
        println!("{}", i);
    }
}
error: expected `{`, found `}`
 --> src/lib.rs:5:1
  |
5 | }
  | ^ expected `{`
fn f() -> usize {
    for i in {
        println!("{}", i);
    }
    return 1;
}
error: expected `{`, found keyword `return`
 --> src/lib.rs:5:5
  |
5 |     return 1;
  |     ^^^^^^---
  |     |
  |     expected `{`
  |     help: try placing this code inside a block: `{ return 1; }`
@jyn514 jyn514 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. D-papercut Diagnostics: An error or lint that needs small tweaks. A-control-flow Area: Control flow labels Oct 29, 2020
@camelid camelid added the A-parser Area: The parsing of Rust source code to an AST label Oct 29, 2020
@camelid
Copy link
Member

camelid commented Oct 29, 2020

I'd love it to instead say 'missing iterator for loop':

error: expected iterator, found block
 --> src/lib.rs
  |
2 |    for i in {
  |             ^ this starts a block expression ...
5 |     1
  |     ^ ... so there is no body for the loop
  =     help: try adding an iterator to the for-loop: `for i in iter {`

Well, it thinks the block is the iterator.

@jyn514
Copy link
Member Author

jyn514 commented Oct 29, 2020

Yes, I understand, but a) () is not an iterator, and b) since there's no body of the loop I was hoping the parser could recover somehow and understand that the block expression was intended to be the body.

@camelid
Copy link
Member

camelid commented Oct 29, 2020

Yes, I just wanted to make sure we were on the same page :)

@estebank
Copy link
Contributor

I could have sworn there was an old [A-diagnostics] ticket for this already but I can't find it 😅

@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Oct 30, 2020
@obeis
Copy link
Contributor

obeis commented Jan 30, 2023

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-control-flow Area: Control flow A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants