Skip to content

Commit 6611567

Browse files
committed
Expect extern fn with no body when parsing
Also add a test case for inserting a semicolon on extern fns. Without this fix, we got an error like this: error: expected one of `->`, `where`, or `{`, found `}` --> chk.rs:3:1 | 2 | fn foo() | --- - expected one of `->`, `where`, or `{` | | | while parsing this `fn` 3 | } | ^ unexpected token Since this is inside an extern block, you're required to write function prototypes with no body. This fixes a regression, and adds a test case for it.
1 parent 6199592 commit 6611567

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

compiler/rustc_parse/src/parser/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ impl<'a> Parser<'a> {
950950
&mut self,
951951
force_collect: ForceCollect,
952952
) -> PResult<'a, Option<Option<P<ForeignItem>>>> {
953-
let fn_parse_mode = FnParseMode { req_name: |_| true, req_body: true };
953+
let fn_parse_mode = FnParseMode { req_name: |_| true, req_body: false };
954954
Ok(self.parse_item_(fn_parse_mode, force_collect)?.map(
955955
|Item { attrs, id, span, vis, ident, kind, tokens }| {
956956
let kind = match ForeignItemKind::try_from(kind) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// run-rustfix
2+
3+
#[allow(dead_code)]
4+
5+
extern "C" {
6+
fn foo(); //~ERROR expected `;`
7+
}
8+
9+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// run-rustfix
2+
3+
#[allow(dead_code)]
4+
5+
extern "C" {
6+
fn foo() //~ERROR expected `;`
7+
}
8+
9+
fn main() {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: expected `;`, found `}`
2+
--> $DIR/suggest-semicolon-for-fn-in-extern-block.rs:6:11
3+
|
4+
LL | fn foo()
5+
| ^ help: add `;` here
6+
LL | }
7+
| - unexpected token
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)