]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_ast_passes/ast_validation.rs
Rollup merge of #68469 - ollie27:skip_count, r=sfackler
[rust.git] / src / librustc_ast_passes / ast_validation.rs
index 23701459025ae3dd8b12fcb967135ed613f3f6db..152086bfce0eafcf9d964d5eff0d5095b3b5b10f 100644 (file)
@@ -23,8 +23,6 @@
 use syntax::visit::{self, Visitor};
 use syntax::walk_list;
 
-use rustc_error_codes::*;
-
 /// A syntactic context that disallows certain kinds of bounds (e.g., `?Trait` or `?const Trait`).
 #[derive(Clone, Copy)]
 enum BoundContext {
@@ -616,6 +614,7 @@ fn visit_item(&mut self, item: &'a Item) {
                 unsafety,
                 polarity,
                 defaultness: _,
+                constness: _,
                 generics: _,
                 of_trait: Some(_),
                 ref self_ty,
@@ -649,6 +648,7 @@ fn visit_item(&mut self, item: &'a Item) {
                 unsafety,
                 polarity,
                 defaultness,
+                constness,
                 generics: _,
                 of_trait: None,
                 self_ty: _,
@@ -676,6 +676,12 @@ fn visit_item(&mut self, item: &'a Item) {
                         .note("only trait implementations may be annotated with default")
                         .emit();
                 }
+                if constness == Constness::Const {
+                    self.err_handler()
+                        .struct_span_err(item.span, "inherent impls cannot be `const`")
+                        .note("only trait implementations may be annotated with `const`")
+                        .emit();
+                }
             }
             ItemKind::Fn(ref sig, ref generics, _) => {
                 self.visit_fn_header(&sig.header);
@@ -909,23 +915,20 @@ fn visit_generic_param(&mut self, param: &'a GenericParam) {
     }
 
     fn visit_param_bound(&mut self, bound: &'a GenericBound) {
-        if let GenericBound::Trait(poly, maybe_bound) = bound {
-            match poly.trait_ref.constness {
-                Some(Constness::NotConst) => {
-                    if *maybe_bound == TraitBoundModifier::Maybe {
-                        self.err_handler()
-                            .span_err(bound.span(), "`?const` and `?` are mutually exclusive");
-                    }
-
-                    if let Some(ctx) = self.bound_context {
-                        let msg = format!("`?const` is not permitted in {}", ctx.description());
-                        self.err_handler().span_err(bound.span(), &msg);
-                    }
+        match bound {
+            GenericBound::Trait(_, TraitBoundModifier::MaybeConst) => {
+                if let Some(ctx) = self.bound_context {
+                    let msg = format!("`?const` is not permitted in {}", ctx.description());
+                    self.err_handler().span_err(bound.span(), &msg);
                 }
+            }
 
-                Some(Constness::Const) => panic!("Parser should reject bare `const` on bounds"),
-                None => {}
+            GenericBound::Trait(_, TraitBoundModifier::MaybeConstMaybe) => {
+                self.err_handler()
+                    .span_err(bound.span(), "`?const` and `?` are mutually exclusive");
             }
+
+            _ => {}
         }
 
         visit::walk_param_bound(self, bound)