Skip to content

Commit

Permalink
Rollup merge of rust-lang#111181 - bvanjoi:fix-issue-111148, r=davidtwco
Browse files Browse the repository at this point in the history
fix(parse): return unpected when current token is EOF

close rust-lang#111148

rust-lang#111148 panic occurred because [FatalError.raise()](https://github.com/bvanjoi/rust/blob/master/compiler/rustc_parse/src/parser/mod.rs#LL540C3-L540C3) was encountered which caused by `Eof` and `Pound`(the last token) had same span, when parsing `#` in `fn a<<i<Y<w<>#`.

<img width="825" alt="image" src="https://user-images.githubusercontent.com/30187863/236612589-9e2c6a0b-18cd-408c-b636-c12a51cbcf1c.png">

There are a few ways to solve this problem:

- Change the action assign for [self.last_unexpected_token_span](https://github.com/rust-lang/rust/blob/master/compiler/rustc_parse/src/parser/diagnostics.rs#L592), for example, if current token is `Eof`, then return Error directly.
- Avoid triggering the `FatalError` when the current token is `Eof`.

I have chosen the second option because executing `expected_one_of_not_found` when the token is `Eof` but not in `ediable` seems reasonable.
  • Loading branch information
matthiaskrgr authored May 27, 2023
2 parents 0b300a7 + 272dc5a commit 97fae38
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,9 @@ impl<'a> Parser<'a> {
} else if inedible.contains(&self.token.kind) {
// leave it in the input
Ok(false)
} else if self.last_unexpected_token_span == Some(self.token.span) {
} else if self.token.kind != token::Eof
&& self.last_unexpected_token_span == Some(self.token.span)
{
FatalError.raise();
} else {
self.expected_one_of_not_found(edible, inedible)
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/parser/issues/issue-111148.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fn a<<i<Y<w<>#
//~^ ERROR expected one of `#`, `>`, `const`, identifier, or lifetime, found `<`
8 changes: 8 additions & 0 deletions tests/ui/parser/issues/issue-111148.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: expected one of `#`, `>`, `const`, identifier, or lifetime, found `<`
--> $DIR/issue-111148.rs:1:6
|
LL | fn a<<i<Y<w<>#
| ^ expected one of `#`, `>`, `const`, identifier, or lifetime

error: aborting due to previous error

0 comments on commit 97fae38

Please sign in to comment.