Skip to content

Commit

Permalink
Auto merge of #40811 - estebank:issue-32540, r=jonathandturner
Browse files Browse the repository at this point in the history
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
```

Fix #32540.
  • Loading branch information
bors committed Apr 5, 2017
2 parents 2564711 + dedb7bb commit ad5dfec
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 20 deletions.
39 changes: 26 additions & 13 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,20 +551,33 @@ impl<'a> Parser<'a> {
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))
let (msg_exp, (label_sp, label_exp)) = if expected.len() > 1 {
let short_expect = if expected.len() > 6 {
format!("{} possible tokens", expected.len())
} else {
(format!("expected {}, found `{}`",
expect,
actual))
})[..]
))
expect.clone()
};
(format!("expected one of {}, found `{}`", expect, actual),
(self.prev_span.next_point(), format!("expected one of {} here", short_expect)))
} else if expected.is_empty() {
(format!("unexpected token: `{}`", actual),
(self.prev_span, "unexpected token after this".to_string()))
} else {
(format!("expected {}, found `{}`", expect, actual),
(self.prev_span.next_point(), format!("expected {} here", expect)))
};
let mut err = self.fatal(&msg_exp);
let sp = if self.token == token::Token::Eof {
// This is EOF, don't want to point at the following char, but rather the last token
self.prev_span
} else {
label_sp
};
err.span_label(sp, &label_exp);
if !sp.source_equal(&self.span) {
err.span_label(self.span, &"unexpected token");
}
Err(err)
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/libsyntax_pos/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ impl Span {
/// Returns a new span representing just the end-point of this span
pub fn end_point(self) -> Span {
let lo = cmp::max(self.hi.0 - 1, self.lo.0);
Span { lo: BytePos(lo), hi: self.hi, ctxt: self.ctxt }
Span { lo: BytePos(lo), ..self }
}

/// Returns a new span representing the next character after the end-point of this span
pub fn next_point(self) -> Span {
let lo = cmp::max(self.hi.0, self.lo.0 + 1);
Span { lo: BytePos(lo), hi: BytePos(lo + 1), ..self }
}

/// Returns `self` if `self` is not the dummy span, and `other` otherwise.
Expand Down
4 changes: 3 additions & 1 deletion src/test/parse-fail/match-refactor-to-expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 here
; //~ NOTE unexpected token
//~^ ERROR expected one of `.`, `?`, `{`, or an operator, found `;`

println!("{}", foo)
}
7 changes: 5 additions & 2 deletions src/test/ui/resolve/token-error-correct-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ error: expected one of `,`, `.`, `?`, or an operator, found `;`
--> $DIR/token-error-correct-3.rs:23:35
|
23 | callback(path.as_ref(); //~ NOTE: unclosed delimiter
| ^
| ^ expected one of `,`, `.`, `?`, or an operator here

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 here
...
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
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/resolve/token-error-correct.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ error: expected one of `)`, `,`, `.`, `<`, `?`, `break`, `continue`, `false`, `f
--> $DIR/token-error-correct.rs:14:13
|
14 | foo(bar(;
| ^
| ^ expected one of 18 possible tokens here

error: expected expression, found `)`
--> $DIR/token-error-correct.rs:23:1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@

type A = Box<(Fn(D::Error) -> E) + 'static + Send + Sync>; // OK (but see #39318)

FAIL //~ ERROR
FAIL
//~^ ERROR
//~| ERROR
8 changes: 8 additions & 0 deletions src/test/ui/token/bounds-obj-parens.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: expected one of `!` or `::`, found `<eof>`
--> $DIR/bounds-obj-parens.rs:15:1
|
15 | FAIL
| ^^^^ expected one of `!` or `::` here

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -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 `)`
28 changes: 28 additions & 0 deletions src/test/ui/token/issue-10636-2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
error: incorrect close delimiter: `}`
--> $DIR/issue-10636-2.rs:19:1
|
19 | } //~ ERROR: incorrect close delimiter
| ^
|
note: unclosed delimiter
--> $DIR/issue-10636-2.rs:15:15
|
15 | option.map(|some| 42; //~ NOTE: unclosed delimiter
| ^

error: expected one of `,`, `.`, `?`, or an operator, found `;`
--> $DIR/issue-10636-2.rs:15:25
|
15 | option.map(|some| 42; //~ NOTE: unclosed delimiter
| ^ expected one of `,`, `.`, `?`, or an operator here

error: expected expression, found `)`
--> $DIR/issue-10636-2.rs:19:1
|
19 | } //~ ERROR: incorrect close delimiter
| ^

error: main function not found

error: aborting due to 4 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ macro_rules! ignored_item {

macro_rules! ignored_expr {
() => ( 1, //~ ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `,`
//~^ NOTE expected one of `.`, `;`, `?`, `}`, or an operator here
//~| NOTE unexpected token
2 )
}

Expand Down
32 changes: 32 additions & 0 deletions src/test/ui/token/macro-incomplete-parse.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
error: macro expansion ignores token `,` and any following
--> $DIR/macro-incomplete-parse.rs:17:9
|
17 | , //~ ERROR macro expansion ignores token `,`
| ^
|
note: caused by the macro expansion here; the usage of `ignored_item!` is likely invalid in item context
--> $DIR/macro-incomplete-parse.rs:32:1
|
32 | ignored_item!(); //~ NOTE caused by the macro expansion here
| ^^^^^^^^^^^^^^^^

error: expected one of `.`, `;`, `?`, `}`, or an operator, found `,`
--> $DIR/macro-incomplete-parse.rs:22:14
|
22 | () => ( 1, //~ ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `,`
| ^ expected one of `.`, `;`, `?`, `}`, or an operator here

error: macro expansion ignores token `,` and any following
--> $DIR/macro-incomplete-parse.rs:29:14
|
29 | () => ( 1, 2 ) //~ ERROR macro expansion ignores token `,`
| ^
|
note: caused by the macro expansion here; the usage of `ignored_pat!` is likely invalid in pattern context
--> $DIR/macro-incomplete-parse.rs:37:9
|
37 | ignored_pat!() => (), //~ NOTE caused by the macro expansion here
| ^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ fn main() {
let x: Box<Debug+> = box 3 as Box<Debug+>; // Trailing `+` is OK
}

FAIL //~ ERROR
FAIL
//~^ ERROR
//~| ERROR
8 changes: 8 additions & 0 deletions src/test/ui/token/trailing-plus-in-bounds.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: expected one of `!` or `::`, found `<eof>`
--> $DIR/trailing-plus-in-bounds.rs:19:1
|
19 | FAIL
| ^^^^ expected one of `!` or `::` here

error: aborting due to previous error

0 comments on commit ad5dfec

Please sign in to comment.