Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
varkor opened this issue Jan 11, 2018 · 1 comment
Closed

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

varkor opened this issue Jan 11, 2018 · 1 comment
Labels
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@varkor
Copy link
Member

varkor commented Jan 11, 2018

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

@scottmcm
Copy link
Member

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

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST labels Jan 12, 2018
@jkordish jkordish added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 10, 2018
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Jun 8, 2018
…cts, r=nikomatsakis

Suggest parentheses when a struct literal needs them

When writing a struct literal in an expression that expects a block to
be started afterwards (like an `if` statement), do not suggest using the
same struct literal:

```
did you mean `S { /* fields * /}`?
```

Instead, suggest surrounding the expression with parentheses:

```
did you mean `(S { /* fields * /})`?
```

Fix rust-lang#47360, rust-lang#50090. Leaving rust-lang#42982 open to come back to this problem with a better solution.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants