Skip to content

Confusing error when using a struct literal as an iterator #47360

Closed
@varkor

Description

@varkor
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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-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.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

    No branches or pull requests

    Issue actions