]> git.lizzy.rs Git - rust.git/commitdiff
Better diagnostics and recovery for `const` in extern blocks
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Wed, 9 Aug 2017 22:43:06 +0000 (01:43 +0300)
committerKornel <kornel@geekhood.net>
Wed, 9 Aug 2017 23:52:50 +0000 (00:52 +0100)
src/libsyntax/parse/parser.rs
src/test/ui/extern-const.rs [new file with mode: 0644]
src/test/ui/extern-const.stderr [new file with mode: 0644]

index c34317e649d8c41a48bb9b8022b4d3ce29b780e2..cb28f356fe6fb385436566c8e7609c08cf923b39 100644 (file)
@@ -5518,12 +5518,11 @@ fn parse_item_foreign_fn(&mut self, vis: ast::Visibility, lo: Span, attrs: Vec<A
         })
     }
 
-    /// Parse a static item from a foreign module
+    /// Parse a static item from a foreign module.
+    /// Assumes that the `static` keyword is already parsed.
     fn parse_item_foreign_static(&mut self, vis: ast::Visibility, lo: Span, attrs: Vec<Attribute>)
                                  -> PResult<'a, ForeignItem> {
-        self.expect_keyword(keywords::Static)?;
         let mutbl = self.eat_keyword(keywords::Mut);
-
         let ident = self.parse_ident()?;
         self.expect(&token::Colon)?;
         let ty = self.parse_ty()?;
@@ -5997,21 +5996,22 @@ fn parse_foreign_item(&mut self) -> PResult<'a, Option<ForeignItem>> {
         let lo = self.span;
         let visibility = self.parse_visibility(false)?;
 
-        if self.check_keyword(keywords::Static) {
-            // FOREIGN STATIC ITEM
+        // FOREIGN STATIC ITEM
+        // Treat `const` as `static` for error recovery, but don't add it to expected tokens.
+        if self.check_keyword(keywords::Static) || self.token.is_keyword(keywords::Const) {
+            if self.token.is_keyword(keywords::Const) {
+                self.diagnostic()
+                    .struct_span_err(self.span, "extern items cannot be `const`")
+                    .span_label(self.span, "use `static` instead").emit();
+            }
+            self.bump(); // `static` or `const`
             return Ok(Some(self.parse_item_foreign_static(visibility, lo, attrs)?));
         }
+        // FOREIGN FUNCTION ITEM
         if self.check_keyword(keywords::Fn) {
-            // FOREIGN FUNCTION ITEM
             return Ok(Some(self.parse_item_foreign_fn(visibility, lo, attrs)?));
         }
 
-        if self.check_keyword(keywords::Const) {
-            let mut err = self.span_fatal(self.span, "extern items cannot be `const`");
-            err.help("use `static` instead");
-            return Err(err);
-        }
-
         // FIXME #5668: this will occur for a macro invocation:
         match self.parse_macro_use_or_failure(attrs, true, false, lo, visibility)? {
             Some(item) => {
diff --git a/src/test/ui/extern-const.rs b/src/test/ui/extern-const.rs
new file mode 100644 (file)
index 0000000..a77d7b1
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// compile-flags: -Z continue-parse-after-error
+
+extern "C" {
+    const C: u8; //~ ERROR extern items cannot be `const`
+}
+
+fn main() {
+    let x = C;
+}
diff --git a/src/test/ui/extern-const.stderr b/src/test/ui/extern-const.stderr
new file mode 100644 (file)
index 0000000..e6c41a0
--- /dev/null
@@ -0,0 +1,8 @@
+error: extern items cannot be `const`
+  --> $DIR/extern-const.rs:14:5
+   |
+14 |     const C: u8; //~ ERROR extern items cannot be `const`
+   |     ^^^^^ use `static` instead
+
+error: aborting due to previous error
+