Skip to content

Commit

Permalink
Expect extern fn with no body when parsing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
notriddle committed Dec 6, 2021
1 parent 6199592 commit 6611567
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ impl<'a> Parser<'a> {
&mut self,
force_collect: ForceCollect,
) -> PResult<'a, Option<Option<P<ForeignItem>>>> {
let fn_parse_mode = FnParseMode { req_name: |_| true, req_body: true };
let fn_parse_mode = FnParseMode { req_name: |_| true, req_body: false };
Ok(self.parse_item_(fn_parse_mode, force_collect)?.map(
|Item { attrs, id, span, vis, ident, kind, tokens }| {
let kind = match ForeignItemKind::try_from(kind) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// run-rustfix

#[allow(dead_code)]

extern "C" {
fn foo(); //~ERROR expected `;`
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// run-rustfix

#[allow(dead_code)]

extern "C" {
fn foo() //~ERROR expected `;`
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: expected `;`, found `}`
--> $DIR/suggest-semicolon-for-fn-in-extern-block.rs:6:11
|
LL | fn foo()
| ^ help: add `;` here
LL | }
| - unexpected token

error: aborting due to previous error

0 comments on commit 6611567

Please sign in to comment.