]> git.lizzy.rs Git - rust.git/commitdiff
Expect extern fn with no body when parsing
authorMichael Howell <michael@notriddle.com>
Mon, 6 Dec 2021 18:07:34 +0000 (11:07 -0700)
committerMichael Howell <michael@notriddle.com>
Mon, 6 Dec 2021 18:16:46 +0000 (11:16 -0700)
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.

compiler/rustc_parse/src/parser/item.rs
src/test/ui/suggestions/suggest-semicolon-for-fn-in-extern-block.fixed [new file with mode: 0644]
src/test/ui/suggestions/suggest-semicolon-for-fn-in-extern-block.rs [new file with mode: 0644]
src/test/ui/suggestions/suggest-semicolon-for-fn-in-extern-block.stderr [new file with mode: 0644]

index 0506a6eba3e06d75ff974b9eb55898a53d2a7b00..831c64e3faf037e3f9ccf8626f076f3b7e97e64e 100644 (file)
@@ -950,7 +950,7 @@ pub fn parse_foreign_item(
         &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) {
diff --git a/src/test/ui/suggestions/suggest-semicolon-for-fn-in-extern-block.fixed b/src/test/ui/suggestions/suggest-semicolon-for-fn-in-extern-block.fixed
new file mode 100644 (file)
index 0000000..5c55566
--- /dev/null
@@ -0,0 +1,9 @@
+// run-rustfix
+
+#[allow(dead_code)]
+
+extern "C" {
+  fn foo(); //~ERROR expected `;`
+}
+
+fn main() {}
diff --git a/src/test/ui/suggestions/suggest-semicolon-for-fn-in-extern-block.rs b/src/test/ui/suggestions/suggest-semicolon-for-fn-in-extern-block.rs
new file mode 100644 (file)
index 0000000..91971cb
--- /dev/null
@@ -0,0 +1,9 @@
+// run-rustfix
+
+#[allow(dead_code)]
+
+extern "C" {
+  fn foo() //~ERROR expected `;`
+}
+
+fn main() {}
diff --git a/src/test/ui/suggestions/suggest-semicolon-for-fn-in-extern-block.stderr b/src/test/ui/suggestions/suggest-semicolon-for-fn-in-extern-block.stderr
new file mode 100644 (file)
index 0000000..c5df72c
--- /dev/null
@@ -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
+