Rust: Confusing error when using a struct literal as an iterator

Created on 11 Jan 2018  ·  1Comment  ·  Source: rust-lang/rust

fn main() {
    for _ in std::ops::Range { start: 0, end: 10 } {}
}

// error: expected type, found `0`
//  --> src/main.rs:2:39
//   |
// 2 |     for _ in std::ops::Range { start: 0, end: 10 } {}
//   |                                       ^ expecting a type here because of type ascription

// error[E0423]: expected value, found struct `std::ops::Range`
//  --> src/main.rs:2:14
//   |
// 2 |     for _ in std::ops::Range { start: 0, end: 10 } {}
//   |              ^^^^^^^^^^^^^^^ did you mean `std::ops::Range { /* fields */ }`?

It's confusing here, as it suggests using exactly what the user has typed. It should instead suggest the user parenthesise the struct literal.
(There's probably a reason this doesn't parse as-is, but if not, it seems like a nice case to handle.)

A-diagnostics A-parser C-enhancement T-compiler

Most helpful comment

👍 on improving the diagnostics here.

There's probably a reason this doesn't parse as-is

The problem is that when the parser sees the { after Range it doesn't know whether it's a struct literal or the start of the loop body, and at parse time it has no idea that Range is a struct, not a constant.

>All comments

👍 on improving the diagnostics here.

There's probably a reason this doesn't parse as-is

The problem is that when the parser sees the { after Range it doesn't know whether it's a struct literal or the start of the loop body, and at parse time it has no idea that Range is a struct, not a constant.

Was this page helpful?
0 / 5 - 0 ratings