]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_ast_passes/ast_validation.rs
parse extern consts
[rust.git] / src / librustc_ast_passes / ast_validation.rs
index fd65750367e595d20726dbe573cd2723fda9593b..8efd50ad0987efbd559cf6c2641cc49c3e43632f 100644 (file)
@@ -533,6 +533,20 @@ fn check_foreign_fn_headerless(&self, ident: Ident, span: Span, header: FnHeader
         }
     }
 
+    fn error_foreign_const(&self, ident: Ident, span: Span) {
+        self.err_handler()
+            .struct_span_err(ident.span, "extern items cannot be `const`")
+            .span_suggestion(
+                span.with_hi(ident.span.lo()),
+                "try using a static value",
+                "static ".to_string(),
+                Applicability::MachineApplicable,
+            )
+            .span_label(self.current_extern_span(), "in this `extern` block")
+            .note(MORE_EXTERN)
+            .emit();
+    }
+
     /// Reject C-varadic type unless the function is foreign,
     /// or free and `unsafe extern "C"` semantically.
     fn check_c_varadic_type(&self, fk: FnKind<'a>) {
@@ -989,6 +1003,9 @@ fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
             ForeignItemKind::Static(_, _, body) => {
                 self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span));
             }
+            ForeignItemKind::Const(..) => {
+                self.error_foreign_const(fi.ident, fi.span);
+            }
             ForeignItemKind::Macro(..) => {}
         }
 
@@ -1250,8 +1267,13 @@ fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
             }
         }
 
-        if let AssocItemKind::Const(..) = item.kind {
-            self.check_item_named(item.ident, "const");
+        match item.kind {
+            AssocItemKind::Const(..) => self.check_item_named(item.ident, "const"),
+            AssocItemKind::Static(..) => self
+                .err_handler()
+                .struct_span_err(item.span, "associated `static` items are not allowed")
+                .emit(),
+            _ => {}
         }
 
         self.with_in_trait_impl(false, |this| visit::walk_assoc_item(this, item, ctxt));