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`
```
This commit is contained in:
Esteban Küber 2017-10-22 09:19:30 -07:00
parent 5ce3d482e2
commit 9dc7abe06d
8 changed files with 121 additions and 76 deletions

View file

@ -17,7 +17,6 @@ impl S {
fn g(&self::S: &S) {}
fn h(&mut self::S: &mut S) {}
fn i(&'a self::S: &S) {} //~ ERROR unexpected lifetime `'a` in pattern
//~^ ERROR expected one of `)` or `mut`, found `'a`
}
fn main() {}

View file

@ -12,5 +12,4 @@
impl S {
fn f(*, a: u8) -> u8 {} //~ ERROR expected pattern, found `*`
//~^ ERROR expected one of `)`, `-`, `box`, `false`, `mut`, `ref`, or `true`, found `*`
}

View file

@ -13,14 +13,5 @@ error: expected type, found keyword `true`
18 | foo!(true);
| ^^^^ expecting a type here because of type ascription
error: expected one of `!`, `&&`, `&`, `(`, `*`, `.`, `;`, `<`, `?`, `[`, `_`, `dyn`, `extern`, `fn`, `for`, `impl`, `unsafe`, `}`, an operator, or lifetime, found `true`
--> $DIR/issue-44406.rs:18:10
|
13 | bar(baz: $rest)
| - expected one of 20 possible tokens here
...
18 | foo!(true);
| ^^^^ unexpected token
error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

View file

@ -28,29 +28,11 @@ error: expected expression, found `;`
14 | foo(bar(;
| ^
error: expected one of `)`, `,`, `.`, `<`, `?`, `break`, `continue`, `false`, `for`, `if`, `loop`, `match`, `move`, `return`, `true`, `unsafe`, `while`, `yield`, or an operator, found `;`
--> $DIR/token-error-correct.rs:14:13
|
14 | foo(bar(;
| ^ expected one of 19 possible tokens here
error: expected expression, found `)`
--> $DIR/token-error-correct.rs:23:1
|
23 | }
| ^
error[E0425]: cannot find function `foo` in this scope
--> $DIR/token-error-correct.rs:14:5
|
14 | foo(bar(;
| ^^^ not found in this scope
error[E0425]: cannot find function `bar` in this scope
--> $DIR/token-error-correct.rs:14:9
|
14 | foo(bar(;
| ^^^ not found in this scope
error: aborting due to 7 previous errors
error: aborting due to 4 previous errors

View file

@ -0,0 +1,13 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn main() {
let x: Vec::with_capacity(10, 20);
}

View file

@ -0,0 +1,17 @@
error: expected type, found `10`
--> $DIR/type-ascription-instead-of-initializer.rs:12:31
|
12 | let x: Vec::with_capacity(10, 20);
| -- ^^
| ||
| |help: use `=` if you meant to assign
| while parsing the type for `x`
error[E0061]: this function takes 1 parameter but 2 parameters were supplied
--> $DIR/type-ascription-instead-of-initializer.rs:12:31
|
12 | let x: Vec::with_capacity(10, 20);
| ^^^^^^ expected 1 parameter
error: aborting due to 2 previous errors