Skip to content

Commit e3918cf

Browse files
committed
Recover from parse error in tuple syntax
1 parent 33ef0ba commit e3918cf

13 files changed

+196
-17
lines changed

src/libsyntax/parse/parser.rs

+31-8
Original file line numberDiff line numberDiff line change
@@ -2637,7 +2637,15 @@ impl<'a> Parser<'a> {
26372637
let mut trailing_comma = false;
26382638
let mut recovered = false;
26392639
while self.token != token::CloseDelim(token::Paren) {
2640-
es.push(self.parse_expr()?);
2640+
es.push(match self.parse_expr() {
2641+
Ok(es) => es,
2642+
Err(mut err) => { // recover from parse error in tuple list
2643+
err.emit();
2644+
self.consume_block(token::Paren);
2645+
hi = self.prev_span;
2646+
return Ok(self.mk_expr(lo.to(hi), ExprKind::Err, ThinVec::new()));
2647+
}
2648+
});
26412649
recovered = self.expect_one_of(
26422650
&[],
26432651
&[token::Comma, token::CloseDelim(token::Paren)],
@@ -3248,16 +3256,24 @@ impl<'a> Parser<'a> {
32483256
match self.token {
32493257
// expr(...)
32503258
token::OpenDelim(token::Paren) => {
3251-
let es = self.parse_unspanned_seq(
3259+
match self.parse_unspanned_seq(
32523260
&token::OpenDelim(token::Paren),
32533261
&token::CloseDelim(token::Paren),
32543262
SeqSep::trailing_allowed(token::Comma),
32553263
|p| Ok(p.parse_expr()?)
3256-
)?;
3257-
hi = self.prev_span;
3258-
3259-
let nd = self.mk_call(e, es);
3260-
e = self.mk_expr(lo.to(hi), nd, ThinVec::new());
3264+
) {
3265+
Ok(es) => {
3266+
let nd = self.mk_call(e, es);
3267+
hi = self.prev_span;
3268+
e = self.mk_expr(lo.to(hi), nd, ThinVec::new());
3269+
}
3270+
Err(mut err) => { // recover from parse error in argument list
3271+
err.emit();
3272+
self.consume_block(token::Paren);
3273+
hi = self.prev_span;
3274+
e = self.mk_expr(lo.to(hi), ExprKind::Err, ThinVec::new());
3275+
}
3276+
}
32613277
}
32623278

32633279
// expr[...]
@@ -4262,7 +4278,14 @@ impl<'a> Parser<'a> {
42624278
// Trailing commas are significant because (p) and (p,) are different patterns.
42634279
fn parse_parenthesized_pat_list(&mut self) -> PResult<'a, (Vec<P<Pat>>, Option<usize>, bool)> {
42644280
self.expect(&token::OpenDelim(token::Paren))?;
4265-
let result = self.parse_pat_list()?;
4281+
let result = match self.parse_pat_list() {
4282+
Ok(result) => result,
4283+
Err(mut err) => { // recover from parse error in tuple pattern list
4284+
err.emit();
4285+
self.consume_block(token::Paren);
4286+
return Ok((vec![], Some(0), false));
4287+
}
4288+
};
42664289
self.expect(&token::CloseDelim(token::Paren))?;
42674290
Ok(result)
42684291
}

src/test/ui/issues/issue-34334.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
fn main () {
2-
let sr: Vec<(u32, _, _) = vec![]; //~ ERROR expected one of `,` or `>`, found `=`
2+
let sr: Vec<(u32, _, _) = vec![];
3+
//~^ ERROR expected one of `,` or `>`, found `=`
4+
//~| ERROR expected value, found struct `Vec`
5+
//~| ERROR mismatched types
6+
//~| ERROR invalid left-hand side expression
7+
//~| ERROR expected expression, found reserved identifier `_`
38
let sr2: Vec<(u32, _, _)> = sr.iter().map(|(faction, th_sender, th_receiver)| {}).collect();
9+
//~^ ERROR no method named `iter` found for type `()` in the current scope
410
}

src/test/ui/issues/issue-34334.stderr

+40-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,47 @@
1+
error: expected expression, found reserved identifier `_`
2+
--> $DIR/issue-34334.rs:2:23
3+
|
4+
LL | let sr: Vec<(u32, _, _) = vec![];
5+
| ^ expected expression
6+
17
error: expected one of `,` or `>`, found `=`
28
--> $DIR/issue-34334.rs:2:29
39
|
410
LL | let sr: Vec<(u32, _, _) = vec![];
5-
| -- ^ expected one of `,` or `>` here
6-
| |
11+
| --- ^ expected one of `,` or `>` here
12+
| | |
13+
| | help: use `=` if you meant to assign
714
| while parsing the type for `sr`
815

9-
error: aborting due to previous error
16+
error[E0423]: expected value, found struct `Vec`
17+
--> $DIR/issue-34334.rs:2:13
18+
|
19+
LL | let sr: Vec<(u32, _, _) = vec![];
20+
| ^^^ did you mean `Vec { /* fields */ }`?
21+
22+
error[E0308]: mismatched types
23+
--> $DIR/issue-34334.rs:2:31
24+
|
25+
LL | let sr: Vec<(u32, _, _) = vec![];
26+
| ^^^^^^ expected bool, found struct `std::vec::Vec`
27+
|
28+
= note: expected type `bool`
29+
found type `std::vec::Vec<_>`
30+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
31+
32+
error[E0070]: invalid left-hand side expression
33+
--> $DIR/issue-34334.rs:2:13
34+
|
35+
LL | let sr: Vec<(u32, _, _) = vec![];
36+
| ^^^^^^^^^^^^^^^^^^^^^^^^ left-hand of expression not valid
37+
38+
error[E0599]: no method named `iter` found for type `()` in the current scope
39+
--> $DIR/issue-34334.rs:8:36
40+
|
41+
LL | let sr2: Vec<(u32, _, _)> = sr.iter().map(|(faction, th_sender, th_receiver)| {}).collect();
42+
| ^^^^
43+
44+
error: aborting due to 6 previous errors
1045

46+
Some errors occurred: E0070, E0308, E0423, E0599.
47+
For more information about an error, try `rustc --explain E0070`.

src/test/ui/parser/pat-tuple-1.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
fn main() {
2-
match 0 {
3-
(, ..) => {} //~ ERROR expected pattern, found `,`
2+
match (0, 1) {
3+
(, ..) => {}
4+
//~^ ERROR expected pattern, found `,`
45
}
56
}

src/test/ui/parser/pat-tuple-5.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn main() {
2-
match 0 {
2+
match (0, 1) {
33
(pat ..) => {} //~ ERROR unexpected token: `)`
44
}
55
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
enum Enum {
2+
Foo { a: usize, b: usize },
3+
Bar(usize, usize),
4+
}
5+
6+
fn main() {
7+
let x = Enum::Foo(a: 3, b: 4);
8+
//~^ ERROR expected type, found `3`
9+
match x {
10+
Enum::Foo(a, b) => {}
11+
//~^ ERROR expected tuple struct/variant, found struct variant `Enum::Foo`
12+
Enum::Bar(a, b) => {}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: expected type, found `3`
2+
--> $DIR/recover-from-bad-variant.rs:7:26
3+
|
4+
LL | let x = Enum::Foo(a: 3, b: 4);
5+
| ^ expecting a type here because of type ascription
6+
7+
error[E0532]: expected tuple struct/variant, found struct variant `Enum::Foo`
8+
--> $DIR/recover-from-bad-variant.rs:10:9
9+
|
10+
LL | Enum::Foo(a, b) => {}
11+
| ^^^^^^^^^ did you mean `Enum::Foo { /* fields */ }`?
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0532`.
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn main() {
2+
let x = (1, 2, 3, 4);
3+
match x {
4+
(1, .., 4) => {}
5+
(1, .=., 4) => { let _: usize = ""; }
6+
//~^ ERROR expected pattern, found `.`
7+
//~| ERROR mismatched types
8+
(.=., 4) => {}
9+
//~^ ERROR expected pattern, found `.`
10+
(1, 2, 3, 4) => {}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error: expected pattern, found `.`
2+
--> $DIR/recover-tuple-pat.rs:5:13
3+
|
4+
LL | (1, .=., 4) => { let _: usize = ""; }
5+
| ^ expected pattern
6+
7+
error: expected pattern, found `.`
8+
--> $DIR/recover-tuple-pat.rs:8:10
9+
|
10+
LL | (.=., 4) => {}
11+
| ^ expected pattern
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/recover-tuple-pat.rs:5:41
15+
|
16+
LL | (1, .=., 4) => { let _: usize = ""; }
17+
| ^^ expected usize, found reference
18+
|
19+
= note: expected type `usize`
20+
found type `&'static str`
21+
22+
error: aborting due to 3 previous errors
23+
24+
For more information about this error, try `rustc --explain E0308`.

src/test/ui/parser/recover-tuple.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn main() {
2+
// no complaints about the tuple not matching the expected type
3+
let x: (usize, usize, usize) = (3, .=.);
4+
//~^ ERROR expected expression, found `.`
5+
// verify that the parser recovers:
6+
let y: usize = ""; //~ ERROR mismatched types
7+
// no complaints about the type
8+
foo(x);
9+
}
10+
11+
fn foo(_: (usize, usize, usize)) {}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: expected expression, found `.`
2+
--> $DIR/recover-tuple.rs:3:40
3+
|
4+
LL | let x: (usize, usize, usize) = (3, .=.);
5+
| ^ expected expression
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/recover-tuple.rs:6:20
9+
|
10+
LL | let y: usize = "";
11+
| ^^ expected usize, found reference
12+
|
13+
= note: expected type `usize`
14+
found type `&'static str`
15+
16+
error: aborting due to 2 previous errors
17+
18+
For more information about this error, try `rustc --explain E0308`.

src/test/ui/parser/trait-object-lifetime-parens.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ fn f<'a, T: Trait + ('a)>() {} //~ ERROR parenthesized lifetime bounds are not s
66

77
fn check<'a>() {
88
let _: Box<Trait + ('a)>; //~ ERROR parenthesized lifetime bounds are not supported
9-
let _: Box<('a) + Trait>; //~ ERROR expected type, found `'a`
9+
let _: Box<('a) + Trait>;
10+
//~^ ERROR expected type, found `'a`
11+
//~| ERROR expected `:`, found `)`
12+
//~| ERROR chained comparison operators require parentheses
1013
}
1114

1215
fn main() {}

src/test/ui/parser/trait-object-lifetime-parens.stderr

+16-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ error: parenthesized lifetime bounds are not supported
1010
LL | let _: Box<Trait + ('a)>;
1111
| ^^^^ help: remove the parentheses
1212

13+
error: expected `:`, found `)`
14+
--> $DIR/trait-object-lifetime-parens.rs:9:19
15+
|
16+
LL | let _: Box<('a) + Trait>;
17+
| ^ expected `:`
18+
19+
error: chained comparison operators require parentheses
20+
--> $DIR/trait-object-lifetime-parens.rs:9:15
21+
|
22+
LL | let _: Box<('a) + Trait>;
23+
| ^^^^^^^^^^^^^^^
24+
|
25+
= help: use `::<...>` instead of `<...>` if you meant to specify type arguments
26+
= help: or use `(...)` if you meant to specify fn arguments
27+
1328
error: expected type, found `'a`
1429
--> $DIR/trait-object-lifetime-parens.rs:9:17
1530
|
@@ -18,5 +33,5 @@ LL | let _: Box<('a) + Trait>;
1833
| |
1934
| while parsing the type for `_`
2035

21-
error: aborting due to 3 previous errors
36+
error: aborting due to 5 previous errors
2237

0 commit comments

Comments
 (0)