Skip to content

Commit 4d9c6cd

Browse files
committed
Auto merge of #60132 - davidtwco:issue-60075, r=estebank
Fix fn front matter parsing ICE from invalid code. Fixes #60075. This PR 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. r? @estebank (thanks for the minimized test case)
2 parents 9224be5 + 60c6ed9 commit 4d9c6cd

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
@@ -6618,7 +6618,12 @@ impl<'a> Parser<'a> {
66186618
};
66196619
(respan(self.prev_span, Constness::NotConst), unsafety, abi)
66206620
};
6621-
self.expect_keyword(keywords::Fn)?;
6621+
if !self.eat_keyword(keywords::Fn) {
6622+
// It is possible for `expect_one_of` to recover given the contents of
6623+
// `self.expected_tokens`, therefore, do not use `self.unexpected()` which doesn't
6624+
// account for this.
6625+
if !self.expect_one_of(&[], &[])? { unreachable!() }
6626+
}
66226627
Ok((constness, unsafety, asyncness, abi))
66236628
}
66246629

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)