Point at last valid token on failed expect_one_of

```rust
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `)`
  --> $DIR/token-error-correct-3.rs:29:9
   |
25 |         foo()
   |             - expected one of `.`, `;`, `?`, `}`, or an operator after this
...
29 |     } else {
   |     ^ unexpected token
```
This commit is contained in:
Esteban Küber 2017-03-24 19:14:58 -07:00
parent c62e532f3d
commit 03eca71381
8 changed files with 37 additions and 20 deletions

View File

@ -548,20 +548,20 @@ fn tokens_to_string(tokens: &[TokenType]) -> String {
expected.dedup();
let expect = tokens_to_string(&expected[..]);
let actual = self.this_token_to_string();
Err(self.fatal(
&(if expected.len() > 1 {
(format!("expected one of {}, found `{}`",
expect,
actual))
} else if expected.is_empty() {
(format!("unexpected token: `{}`",
actual))
} else {
(format!("expected {}, found `{}`",
expect,
actual))
})[..]
))
let (msg_exp, label_exp) = if expected.len() > 1 {
(format!("expected one of {}, found `{}`", expect, actual),
format!("expected one of {} after this", expect))
} else if expected.is_empty() {
(format!("unexpected token: `{}`", actual),
"unexpected token after this".to_string())
} else {
(format!("expected {}, found `{}`", expect, actual),
format!("expected {} after this", expect))
};
let mut err = self.fatal(&msg_exp);
err.span_label(self.prev_span, &label_exp);
err.span_label(self.span, &"unexpected token");
Err(err)
}
}

View File

@ -14,5 +14,7 @@
pub fn trace_option(option: Option<isize>) {
option.map(|some| 42; //~ NOTE: unclosed delimiter
//~^ ERROR: expected one of
//~| NOTE: expected one of
//~| NOTE: unexpected token
} //~ ERROR: incorrect close delimiter
//~^ ERROR: expected expression, found `)`

View File

@ -20,6 +20,8 @@ fn bar() {}
macro_rules! ignored_expr {
() => ( 1, //~ ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `,`
//~^ NOTE expected one of `.`, `;`, `?`, `}`, or an operator after this
//~| NOTE unexpected token
2 )
}

View File

@ -12,4 +12,6 @@
type A = Box<(Fn(D::Error) -> E) + 'static + Send + Sync>; // OK (but see #39318)
FAIL //~ ERROR
FAIL
//~^ ERROR
//~| ERROR

View File

@ -14,7 +14,9 @@ fn main() {
let foo =
match //~ NOTE did you mean to remove this `match` keyword?
Some(4).unwrap_or_else(5)
; //~ ERROR expected one of `.`, `?`, `{`, or an operator, found `;`
//~^ NOTE expected one of `.`, `?`, `{`, or an operator after this
; //~ NOTE unexpected token
//~^ ERROR expected one of `.`, `?`, `{`, or an operator, found `;`
println!("{}", foo)
}

View File

@ -16,4 +16,6 @@ fn main() {
let x: Box<Debug+> = box 3 as Box<Debug+>; // Trailing `+` is OK
}
FAIL //~ ERROR
FAIL
//~^ ERROR
//~| ERROR

View File

@ -14,13 +14,18 @@ error: expected one of `,`, `.`, `?`, or an operator, found `;`
--> $DIR/token-error-correct-3.rs:23:35
|
23 | callback(path.as_ref(); //~ NOTE: unclosed delimiter
| ^
| -^ unexpected token
| |
| expected one of `,`, `.`, `?`, or an operator after this
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `)`
--> $DIR/token-error-correct-3.rs:29:9
|
25 | fs::create_dir_all(path.as_ref()).map(|()| true) //~ ERROR: mismatched types
| - expected one of `.`, `;`, `?`, `}`, or an operator after this
...
29 | } else { //~ ERROR: incorrect close delimiter: `}`
| ^
| ^ unexpected token
error[E0425]: cannot find function `is_directory` in this scope
--> $DIR/token-error-correct-3.rs:21:13

View File

@ -32,7 +32,9 @@ error: expected one of `)`, `,`, `.`, `<`, `?`, `break`, `continue`, `false`, `f
--> $DIR/token-error-correct.rs:14:13
|
14 | foo(bar(;
| ^
| -^ unexpected token
| |
| expected one of `)`, `,`, `.`, `<`, `?`, `break`, `continue`, `false`, `for`, `if`, `loop`, `match`, `move`, `return`, `true`, `unsafe`, `while`, or an operator after this
error: expected expression, found `)`
--> $DIR/token-error-correct.rs:23:1