Skip to content

Commit c9a766a

Browse files
authored
Rollup merge of rust-lang#62887 - estebank:issue-62881, r=petrochenkov
Make the parser TokenStream more resilient after mismatched delimiter recovery Fix rust-lang#62881, fix rust-lang#62895.
2 parents a534c37 + fe2b5bb commit c9a766a

File tree

5 files changed

+109
-1
lines changed

5 files changed

+109
-1
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7671,14 +7671,27 @@ impl<'a> Parser<'a> {
76717671
let ret = f(self);
76727672
let last_token = if self.token_cursor.stack.len() == prev {
76737673
&mut self.token_cursor.frame.last_token
7674+
} else if self.token_cursor.stack.get(prev).is_none() {
7675+
// This can happen due to a bad interaction of two unrelated recovery mechanisms with
7676+
// mismatched delimiters *and* recovery lookahead on the likely typo `pub ident(`
7677+
// (#62881).
7678+
return Ok((ret?, TokenStream::new(vec![])));
76747679
} else {
76757680
&mut self.token_cursor.stack[prev].last_token
76767681
};
76777682

76787683
// Pull out the tokens that we've collected from the call to `f` above.
76797684
let mut collected_tokens = match *last_token {
76807685
LastToken::Collecting(ref mut v) => mem::take(v),
7681-
LastToken::Was(_) => panic!("our vector went away?"),
7686+
LastToken::Was(ref was) => {
7687+
let msg = format!("our vector went away? - found Was({:?})", was);
7688+
debug!("collect_tokens: {}", msg);
7689+
self.sess.span_diagnostic.delay_span_bug(self.token.span, &msg);
7690+
// This can happen due to a bad interaction of two unrelated recovery mechanisms
7691+
// with mismatched delimiters *and* recovery lookahead on the likely typo
7692+
// `pub ident(` (#62895, different but similar to the case above).
7693+
return Ok((ret?, TokenStream::new(vec![])));
7694+
}
76827695
};
76837696

76847697
// If we're not at EOF our current token wasn't actually consumed by

src/test/ui/parser/issue-62881.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {}
2+
3+
fn f() -> isize { fn f() -> isize {} pub f<
4+
//~^ ERROR missing `fn` or `struct` for function or struct definition
5+
//~| ERROR mismatched types
6+
//~ ERROR this file contains an un-closed delimiter

src/test/ui/parser/issue-62881.stderr

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: this file contains an un-closed delimiter
2+
--> $DIR/issue-62881.rs:6:53
3+
|
4+
LL | fn f() -> isize { fn f() -> isize {} pub f<
5+
| - un-closed delimiter
6+
...
7+
LL |
8+
| ^
9+
10+
error: missing `fn` or `struct` for function or struct definition
11+
--> $DIR/issue-62881.rs:3:41
12+
|
13+
LL | fn f() -> isize { fn f() -> isize {} pub f<
14+
| ^
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/issue-62881.rs:3:29
18+
|
19+
LL | fn f() -> isize { fn f() -> isize {} pub f<
20+
| - ^^^^^ expected isize, found ()
21+
| |
22+
| this function's body doesn't return
23+
|
24+
= note: expected type `isize`
25+
found type `()`
26+
27+
error: aborting due to 3 previous errors
28+
29+
For more information about this error, try `rustc --explain E0308`.

src/test/ui/parser/issue-62895.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn main() {}
2+
3+
fn v() -> isize { //~ ERROR mismatched types
4+
mod _ { //~ ERROR expected identifier
5+
pub fn g() -> isizee { //~ ERROR cannot find type `isizee` in this scope
6+
mod _ { //~ ERROR expected identifier
7+
pub g() -> is //~ ERROR missing `fn` for function definition
8+
(), w20);
9+
}
10+
(), w20); //~ ERROR expected item, found `;`
11+
}

src/test/ui/parser/issue-62895.stderr

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error: expected identifier, found reserved identifier `_`
2+
--> $DIR/issue-62895.rs:4:5
3+
|
4+
LL | mod _ {
5+
| ^ expected identifier, found reserved identifier
6+
7+
error: expected identifier, found reserved identifier `_`
8+
--> $DIR/issue-62895.rs:6:5
9+
|
10+
LL | mod _ {
11+
| ^ expected identifier, found reserved identifier
12+
13+
error: missing `fn` for function definition
14+
--> $DIR/issue-62895.rs:7:4
15+
|
16+
LL | pub g() -> is
17+
| ^^^^
18+
help: add `fn` here to parse `g` as a public function
19+
|
20+
LL | pub fn g() -> is
21+
| ^^
22+
23+
error: expected item, found `;`
24+
--> $DIR/issue-62895.rs:10:9
25+
|
26+
LL | (), w20);
27+
| ^ help: remove this semicolon
28+
29+
error[E0412]: cannot find type `isizee` in this scope
30+
--> $DIR/issue-62895.rs:5:15
31+
|
32+
LL | pub fn g() -> isizee {
33+
| ^^^^^^ help: a builtin type with a similar name exists: `isize`
34+
35+
error[E0308]: mismatched types
36+
--> $DIR/issue-62895.rs:3:11
37+
|
38+
LL | fn v() -> isize {
39+
| - ^^^^^ expected isize, found ()
40+
| |
41+
| this function's body doesn't return
42+
|
43+
= note: expected type `isize`
44+
found type `()`
45+
46+
error: aborting due to 6 previous errors
47+
48+
Some errors have detailed explanations: E0308, E0412.
49+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)