Skip to content

Commit 108bca5

Browse files
committed
Make the parser’s ‘expected <foo>, found <bar>’ errors more accurate
As an example of what this changes, the following code: let x: [int ..4]; Currently spits out ‘expected `]`, found `..`’. However, a comma would also be valid there, as would a number of other tokens. This change adjusts the parser to produce more accurate errors, so that that example now produces ‘expected one of `(`, `+`, `,`, `::`, or `]`, found `..`’.
1 parent 3a325c6 commit 108bca5

29 files changed

+155
-95
lines changed

src/libsyntax/parse/parser.rs

+115-68
Large diffs are not rendered by default.
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let x: [int ..3]; //~ ERROR expected one of `(`, `+`, `,`, `::`, or `]`, found `..`
13+
}

src/test/compile-fail/column-offset-1-based.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
# //~ ERROR 11:1: 11:2 error: expected `[`, found `<eof>`
11+
# //~ ERROR 11:1: 11:2 error: expected one of `!` or `[`, found `<eof>`

src/test/compile-fail/empty-impl-semicolon.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
impl Foo; //~ ERROR expected `{`, found `;`
11+
impl Foo; //~ ERROR expected one of `(`, `+`, `::`, or `{`, found `;`

src/test/compile-fail/issue-1655.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern:expected `[`, found `vec`
11+
// error-pattern:expected one of `!` or `[`, found `vec`
1212
mod blade_runner {
1313
#vec[doc(
1414
brief = "Blade Runner is probably the best movie ever",

src/test/compile-fail/issue-19096.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313
fn main() {
1414
let t = (42i, 42i);
15-
t.0::<int>; //~ ERROR expected one of `;`, `}`, found `::`
15+
t.0::<int>; //~ ERROR expected one of `.`, `;`, `}`, or an operator, found `::`
1616
}

src/test/compile-fail/issue-3036.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
fn main()
1414
{
1515
let x = 3
16-
} //~ ERROR: expected `;`, found `}`
16+
} //~ ERROR: expected one of `.`, `;`, or an operator, found `}`

src/test/compile-fail/match-vec-invalid.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
fn main() {
1212
let a = Vec::new();
1313
match a {
14-
[1, tail.., tail..] => {}, //~ ERROR: expected `,`, found `..`
14+
[1, tail.., tail..] => {}, //~ ERROR: expected one of `!`, `,`, or `@`, found `..`
1515
_ => ()
1616
}
1717
}

src/test/compile-fail/multitrait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct S {
1212
y: int
1313
}
1414

15-
impl Cmp, ToString for S { //~ ERROR: expected `{`, found `,`
15+
impl Cmp, ToString for S { //~ ERROR: expected one of `(`, `+`, `::`, or `{`, found `,`
1616
fn eq(&&other: S) { false }
1717
fn to_string(&self) -> String { "hi".to_string() }
1818
}

src/test/compile-fail/mut-patterns.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313
pub fn main() {
1414
struct Foo { x: int }
15-
let mut Foo { x: x } = Foo { x: 3 }; //~ ERROR: expected `;`, found `{`
15+
let mut Foo { x: x } = Foo { x: 3 }; //~ ERROR: expected one of `:`, `;`, `=`, or `@`, found `{`
1616
}

src/test/compile-fail/omitted-arg-in-item-fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
fn foo(x) { //~ ERROR expected `:`, found `)`
11+
fn foo(x) { //~ ERROR expected one of `!`, `:`, or `@`, found `)`
1212
}

src/test/compile-fail/pat-range-bad-dots.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
pub fn main() {
1212
match 22i {
13-
0 .. 3 => {} //~ ERROR expected `=>`, found `..`
13+
0 .. 3 => {} //~ ERROR expected one of `...`, `=>`, or `|`, found `..`
1414
_ => {}
1515
}
1616
}

src/test/compile-fail/raw-str-unbalanced.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
static s: &'static str =
1212
r#"
13-
"## //~ ERROR expected `;`, found `#`
13+
"## //~ ERROR expected one of `.`, `;`, or an operator, found `#`
1414
;

src/test/compile-fail/removed-syntax-closure-lifetime.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
type closure = Box<lt/fn()>; //~ ERROR expected `,`, found `/`
11+
type closure = Box<lt/fn()>; //~ ERROR expected one of `(`, `+`, `,`, `::`, or `>`, found `/`

src/test/compile-fail/removed-syntax-enum-newtype.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
enum e = int; //~ ERROR expected `{`, found `=`
11+
enum e = int; //~ ERROR expected one of `<` or `{`, found `=`

src/test/compile-fail/removed-syntax-fixed-vec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
type v = [int * 3]; //~ ERROR expected `]`, found `*`
11+
type v = [int * 3]; //~ ERROR expected one of `(`, `+`, `,`, `::`, or `]`, found `*`

src/test/compile-fail/removed-syntax-larrow-init.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
fn removed_moves() {
1212
let mut x = 0;
1313
let y <- x;
14-
//~^ ERROR expected `;`, found `<-`
14+
//~^ ERROR expected one of `!`, `:`, `;`, `=`, or `@`, found `<-`
1515
}

src/test/compile-fail/removed-syntax-larrow-move.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ fn removed_moves() {
1212
let mut x = 0;
1313
let y = 0;
1414
y <- x;
15-
//~^ ERROR expected one of `;`, `}`, found `<-`
15+
//~^ ERROR expected one of `!`, `.`, `::`, `;`, `{`, `}`, or an operator, found `<-`
1616
}

src/test/compile-fail/removed-syntax-mut-vec-expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
fn f() {
1212
let v = [mut 1, 2, 3, 4];
1313
//~^ ERROR expected identifier, found keyword `mut`
14-
//~^^ ERROR expected `]`, found `1`
14+
//~^^ ERROR expected one of `!`, `,`, `.`, `::`, `]`, `{`, or an operator, found `1`
1515
}

src/test/compile-fail/removed-syntax-mut-vec-ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
type v = [mut int];
1212
//~^ ERROR expected identifier, found keyword `mut`
13-
//~^^ ERROR expected `]`, found `int`
13+
//~^^ ERROR expected one of `(`, `+`, `,`, `::`, or `]`, found `int`

src/test/compile-fail/removed-syntax-ptr-lifetime.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
type bptr = &lifetime/int; //~ ERROR expected `;`, found `/`
11+
type bptr = &lifetime/int; //~ ERROR expected one of `(`, `+`, `::`, or `;`, found `/`

src/test/compile-fail/removed-syntax-record.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
type t = { f: () }; //~ ERROR expected type, found token OpenDelim(Brace)
11+
type t = { f: () }; //~ ERROR expected type, found `{`

src/test/compile-fail/removed-syntax-uniq-mut-expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
fn f() {
1212
let a_box = box mut 42;
1313
//~^ ERROR expected identifier, found keyword `mut`
14-
//~^^ ERROR expected `;`, found `42`
14+
//~^^ ERROR expected one of `!`, `.`, `::`, `;`, `{`, or an operator, found `42`
1515
}

src/test/compile-fail/removed-syntax-uniq-mut-ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
type mut_box = Box<mut int>;
1212
//~^ ERROR expected identifier, found keyword `mut`
13-
//~^^ ERROR expected `,`, found `int`
13+
//~^^ ERROR expected one of `(`, `+`, `,`, `::`, or `>`, found `int`

src/test/compile-fail/removed-syntax-with-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ fn removed_with() {
1616

1717
let a = S { foo: (), bar: () };
1818
let b = S { foo: () with a };
19-
//~^ ERROR expected one of `,`, `}`, found `with`
19+
//~^ ERROR expected one of `,`, `.`, `}`, or an operator, found `with`
2020
}

src/test/compile-fail/struct-literal-in-for.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl Foo {
2020

2121
fn main() {
2222
for x in Foo {
23-
x: 3 //~ ERROR expected one of `;`, `}`
23+
x: 3 //~ ERROR expected one of `!`, `.`, `::`, `;`, `{`, `}`, or an operator, found `:`
2424
}.hi() {
2525
println!("yo");
2626
}

src/test/compile-fail/struct-literal-in-if.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl Foo {
2020

2121
fn main() {
2222
if Foo {
23-
x: 3 //~ ERROR expected one of `;`, `}`
23+
x: 3 //~ ERROR expected one of `!`, `.`, `::`, `;`, `{`, `}`, or an operator, found `:`
2424
}.hi() {
2525
println!("yo");
2626
}

src/test/compile-fail/struct-literal-in-match-discriminant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct Foo {
1414

1515
fn main() {
1616
match Foo {
17-
x: 3 //~ ERROR expected `=>`
17+
x: 3 //~ ERROR expected one of `!`, `=>`, `@`, or `|`, found `:`
1818
} {
1919
Foo {
2020
x: x

src/test/compile-fail/struct-literal-in-while.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl Foo {
2020

2121
fn main() {
2222
while Foo {
23-
x: 3 //~ ERROR expected one of `;`, `}`
23+
x: 3 //~ ERROR expected one of `!`, `.`, `::`, `;`, `{`, `}`, or an operator, found `:`
2424
}.hi() {
2525
println!("yo");
2626
}

0 commit comments

Comments
 (0)