Closed
Description
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; }`
Metadata
Metadata
Assignees
Labels
Area: Control flowArea: Messages for errors, warnings, and lintsArea: The lexing & parsing of Rust source code to an ASTCategory: An issue proposing an enhancement or a PR with one.Diagnostics: An error or lint that needs small tweaks.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
camelid commentedon Oct 29, 2020
Well, it thinks the block is the iterator.
jyn514 commentedon 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 commentedon Oct 29, 2020
Yes, I just wanted to make sure we were on the same page :)
estebank commentedon Oct 29, 2020
I could have sworn there was an old
[A-diagnostics]
ticket for this already but I can't find it 😅obeis commentedon Jan 30, 2023
@rustbot claim
for
loop #107526Auto merge of rust-lang#107526 - obeis:for-missing-iterator, r=esteba…