Skip to content

Better diagnostics for missing iterator in for-loop #78537

Closed
@jyn514

Description

@jyn514
Member

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; }`

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
D-papercutDiagnostics: An error or lint that needs small tweaks.
on Oct 29, 2020
added
A-parserArea: The lexing & parsing of Rust source code to an AST
on Oct 29, 2020
camelid

camelid commented on Oct 29, 2020

@camelid
Member

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

jyn514 commented on Oct 29, 2020

@jyn514
MemberAuthor

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

camelid commented on Oct 29, 2020

@camelid
Member

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

estebank

estebank commented on Oct 29, 2020

@estebank
Contributor

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

added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
on Oct 30, 2020
obeis

obeis commented on Jan 30, 2023

@obeis
Contributor

@rustbot claim

added a commit that references this issue on Feb 5, 2023

Auto merge of rust-lang#107526 - obeis:for-missing-iterator, r=esteba…

75a0be9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-control-flowArea: Control flowA-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @estebank@jyn514@JohnTitor@camelid@obeis

    Issue actions

      Better diagnostics for missing iterator in for-loop · Issue #78537 · rust-lang/rust