Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Error message for "expression ascription" does not highlight the entire expression #43703

Closed
Ixrec opened this issue Aug 6, 2017 · 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. WG-diagnostics Working group: Diagnostics

Comments

@Ixrec
Copy link
Contributor

Ixrec commented Aug 6, 2017

This came up in #rust today and I couldn't find any similar A-diagnostics issues. A simple example is:

use std::vec::Vec;
fn main() {
    let x: Vec::with_capacity(10);
}

This correctly fails with a compiler error, since it's using : for type ascription where it meant to use = for initialization. But our current error message makes it sound like the problem is near 10, instead of near the :.

error: expected type, found `10`
 --> src/main.rs:4:31
  |
4 |     let x: Vec::with_capacity(10);
  |                               ^^

error: expected one of `!`, `+`, `->`, `::`, `;`, or `=`, found `)`
 --> src/main.rs:4:33
  |
4 |     let x: Vec::with_capacity(10);
  |                                 ^ expected one of `!`, `+`, `->`, `::`, `;`, or `=` here

error: aborting due to previous error(s)

Ideally the message would be "expected type, found Vec::with_capacity(10)".

@Mark-Simulacrum Mark-Simulacrum added 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. labels Aug 10, 2017
@estebank
Copy link
Contributor

The output could be close to the following:

error: expected type, found `10`
 --> src/main.rs:4:31
  |
4 |     let x: Vec::with_capacity(10);
  |          -                    ^^
  |          |
  |          expected type due to type ascription here

@estebank estebank added E-needs-mentor WG-diagnostics Working group: Diagnostics labels Oct 13, 2017
bors added a commit that referenced this issue Nov 8, 2017
Detect `=` -> `:` typo in let bindings

When encountering a let binding type error, attempt to parse as
initializer instead. If successful, it is likely just a typo:

```rust
fn main() {
    let x: Vec::with_capacity(10);
}
```

```
error: expected type, found `10`
 --> file.rs:3:31
  |
3 |     let x: Vec::with_capacity(10, 20);
  |         --                    ^^
  |         ||
  |         |help: did you mean assign here?: `=`
  |         while parsing the type for `x`
```

Fix #43703.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in 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. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

No branches or pull requests

3 participants