Skip to content

Commit 60c6ed9

Browse files
committed
Fix fn front matter parsing ICE from invalid code.
This commit fixes an "unreachable code" ICE that results from parsing invalid code where the compiler is expecting the next trait item declaration in the middle of the previous trait item due to extra closing braces.
1 parent d6f513e commit 60c6ed9

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

src/libsyntax/parse/parser.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -6575,7 +6575,12 @@ impl<'a> Parser<'a> {
65756575
};
65766576
(respan(self.prev_span, Constness::NotConst), unsafety, abi)
65776577
};
6578-
self.expect_keyword(keywords::Fn)?;
6578+
if !self.eat_keyword(keywords::Fn) {
6579+
// It is possible for `expect_one_of` to recover given the contents of
6580+
// `self.expected_tokens`, therefore, do not use `self.unexpected()` which doesn't
6581+
// account for this.
6582+
if !self.expect_one_of(&[], &[])? { unreachable!() }
6583+
}
65796584
Ok((constness, unsafety, asyncness, abi))
65806585
}
65816586

src/test/ui/issue-60075.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn main() {}
2+
3+
trait T {
4+
fn qux() -> Option<usize> {
5+
let _ = if true {
6+
});
7+
//~^ ERROR expected one of `async`, `const`, `extern`, `fn`, `type`, `unsafe`, or `}`, found `;`
8+
//~^^ ERROR expected one of `.`, `;`, `?`, `else`, or an operator, found `}`
9+
//~^^^ ERROR 6:11: 6:12: expected identifier, found `;`
10+
//~^^^^ ERROR missing `fn`, `type`, or `const` for trait-item declaration
11+
Some(4)
12+
}

src/test/ui/issue-60075.stderr

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `}`
2+
--> $DIR/issue-60075.rs:6:10
3+
|
4+
LL | });
5+
| ^ expected one of `.`, `;`, `?`, `else`, or an operator here
6+
7+
error: expected one of `async`, `const`, `extern`, `fn`, `type`, `unsafe`, or `}`, found `;`
8+
--> $DIR/issue-60075.rs:6:11
9+
|
10+
LL | fn qux() -> Option<usize> {
11+
| - unclosed delimiter
12+
LL | let _ = if true {
13+
LL | });
14+
| ^
15+
| |
16+
| help: `}` may belong here
17+
18+
error: expected identifier, found `;`
19+
--> $DIR/issue-60075.rs:6:11
20+
|
21+
LL | });
22+
| ^ expected identifier
23+
24+
error: missing `fn`, `type`, or `const` for trait-item declaration
25+
--> $DIR/issue-60075.rs:6:12
26+
|
27+
LL | });
28+
| ____________^
29+
LL | |
30+
LL | |
31+
LL | |
32+
LL | |
33+
LL | | Some(4)
34+
| |________^ missing `fn`, `type`, or `const`
35+
36+
error: aborting due to 4 previous errors
37+

0 commit comments

Comments
 (0)