]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc/hir/check_attr.rs
Update to use new librustc_error_codes library
[rust.git] / src / librustc / hir / check_attr.rs
index c0fe712adc7581ea7010fc460251e908c31a93c3..ea1c585176d57c837e9cb6c310fc1cb3338cf3de 100644 (file)
 use syntax::{attr, symbol::sym};
 use syntax_pos::Span;
 
+use rustc_error_codes::*;
+
+#[derive(Copy, Clone, PartialEq)]
+pub(crate) enum MethodKind {
+    Trait { body: bool },
+    Inherent,
+}
+
 #[derive(Copy, Clone, PartialEq)]
 pub(crate) enum Target {
     ExternCrate,
@@ -38,8 +46,11 @@ pub(crate) enum Target {
     Expression,
     Statement,
     AssocConst,
-    Method { body: bool },
+    Method(MethodKind),
     AssocTy,
+    ForeignFn,
+    ForeignStatic,
+    ForeignTy,
 }
 
 impl Display for Target {
@@ -65,8 +76,11 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
             Target::Expression => "expression",
             Target::Statement => "statement",
             Target::AssocConst => "associated const",
-            Target::Method { .. } => "method",
+            Target::Method(_) => "method",
             Target::AssocTy => "associated type",
+            Target::ForeignFn => "foreign function",
+            Target::ForeignStatic => "foreign static item",
+            Target::ForeignTy => "foreign type",
         })
     }
 }
@@ -97,14 +111,40 @@ fn from_trait_item(trait_item: &TraitItem) -> Target {
         match trait_item.kind {
             TraitItemKind::Const(..) => Target::AssocConst,
             TraitItemKind::Method(_, hir::TraitMethod::Required(_)) => {
-                Target::Method { body: false }
+                Target::Method(MethodKind::Trait { body: false })
             }
             TraitItemKind::Method(_, hir::TraitMethod::Provided(_)) => {
-                Target::Method { body: true }
+                Target::Method(MethodKind::Trait { body: true })
             }
             TraitItemKind::Type(..) => Target::AssocTy,
         }
     }
+
+    fn from_foreign_item(foreign_item: &hir::ForeignItem) -> Target {
+        match foreign_item.kind {
+            hir::ForeignItemKind::Fn(..) => Target::ForeignFn,
+            hir::ForeignItemKind::Static(..) => Target::ForeignStatic,
+            hir::ForeignItemKind::Type => Target::ForeignTy,
+        }
+    }
+
+    fn from_impl_item<'tcx>(tcx: TyCtxt<'tcx>, impl_item: &hir::ImplItem) -> Target {
+        match impl_item.kind {
+            hir::ImplItemKind::Const(..) => Target::AssocConst,
+            hir::ImplItemKind::Method(..) => {
+                let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id);
+                let containing_item = tcx.hir().expect_item(parent_hir_id);
+                let containing_impl_is_for_trait = match &containing_item.kind {
+                    hir::ItemKind::Impl(_, _, _, _, tr, _, _) => tr.is_some(),
+                    _ => bug!("parent of an ImplItem must be an Impl"),
+                };
+                if containing_impl_is_for_trait {
+                    Target::Method(MethodKind::Trait { body: true })
+                } else {
+                    Target::Method(MethodKind::Inherent)
+                }
+            }
+            hir::ImplItemKind::TyAlias(..) | hir::ImplItemKind::OpaqueTy(..) => Target::AssocTy,
         }
     }
 }
@@ -134,7 +174,7 @@ fn check_attributes(
             } else if attr.check_name(sym::target_feature) {
                 self.check_target_feature(attr, span, target)
             } else if attr.check_name(sym::track_caller) {
-                self.check_track_caller(attr, &item, target)
+                self.check_track_caller(&attr.span, attrs, span, target)
             } else {
                 true
             };
@@ -155,8 +195,9 @@ fn check_attributes(
     /// Checks if an `#[inline]` is applied to a function or a closure. Returns `true` if valid.
     fn check_inline(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) -> bool {
         match target {
-            Target::Fn | Target::Closure | Target::Method { body: true } => true,
-            Target::Method { body: false } | Target::ForeignFn => {
+            Target::Fn | Target::Closure | Target::Method(MethodKind::Trait { body: true })
+            | Target::Method(MethodKind::Inherent) => true,
+            Target::Method(MethodKind::Trait { body: false }) | Target::ForeignFn => {
                 self.tcx.struct_span_lint_hir(
                     UNUSED_ATTRIBUTES,
                     hir_id,
@@ -165,12 +206,31 @@ fn check_inline(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Tar
                 ).emit();
                 true
             }
+            // FIXME(#65833): We permit associated consts to have an `#[inline]` attribute with
+            // just a lint, because we previously erroneously allowed it and some crates used it
+            // accidentally, to to be compatible with crates depending on them, we can't throw an
+            // error here.
+            Target::AssocConst => {
+                self.tcx.struct_span_lint_hir(
+                    UNUSED_ATTRIBUTES,
+                    hir_id,
+                    attr.span,
+                    "`#[inline]` is ignored on constants",
+                ).warn("this was previously accepted by the compiler but is \
+                       being phased out; it will become a hard error in \
+                       a future release!")
+                .note("for more information, see issue #65833 \
+                       <https://github.com/rust-lang/rust/issues/65833>")
+                .emit();
+                true
+            }
             _ => {
-                struct_span_err!(self.tcx.sess,
-                                 attr.span,
-                                 E0518,
-                                 "attribute should be applied to function or closure")
-                    .span_label(*span, "not a function or closure")
+                struct_span_err!(
+                    self.tcx.sess,
+                    attr.span,
+                    E0518,
+                    "attribute should be applied to function or closure",
+                ).span_label(*span, "not a function or closure")
                     .emit();
                 false
             }
@@ -178,28 +238,44 @@ fn check_inline(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Tar
     }
 
     /// Checks if a `#[track_caller]` is applied to a non-naked function. Returns `true` if valid.
-    fn check_track_caller(&self, attr: &hir::Attribute, item: &hir::Item, target: Target) -> bool {
-        if target != Target::Fn {
-            struct_span_err!(
-                self.tcx.sess,
-                attr.span,
-                E0739,
-                "attribute should be applied to function"
-            )
-            .span_label(item.span, "not a function")
-            .emit();
-            false
-        } else if attr::contains_name(&item.attrs, sym::naked) {
-            struct_span_err!(
-                self.tcx.sess,
-                attr.span,
-                E0736,
-                "cannot use `#[track_caller]` with `#[naked]`",
-            )
-            .emit();
-            false
-        } else {
-            true
+    fn check_track_caller(
+        &self,
+        attr_span: &Span,
+        attrs: &HirVec<Attribute>,
+        span: &Span,
+        target: Target,
+    ) -> bool {
+        match target {
+            Target::Fn if attr::contains_name(attrs, sym::naked) => {
+                struct_span_err!(
+                    self.tcx.sess,
+                    *attr_span,
+                    E0736,
+                    "cannot use `#[track_caller]` with `#[naked]`",
+                ).emit();
+                false
+            }
+            Target::Fn | Target::Method(MethodKind::Inherent) => true,
+            Target::Method(_) => {
+                struct_span_err!(
+                    self.tcx.sess,
+                    *attr_span,
+                    E0738,
+                    "`#[track_caller]` may not be used on trait methods",
+                ).emit();
+                false
+            }
+            _ => {
+                struct_span_err!(
+                    self.tcx.sess,
+                    *attr_span,
+                    E0739,
+                    "attribute should be applied to function"
+                )
+                .span_label(*span, "not a function")
+                .emit();
+                false
+            }
         }
     }
 
@@ -241,7 +317,8 @@ fn check_marker(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
     /// Checks if the `#[target_feature]` attribute on `item` is valid. Returns `true` if valid.
     fn check_target_feature(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
         match target {
-            Target::Fn => true,
+            Target::Fn | Target::Method(MethodKind::Trait { body: true })
+            | Target::Method(MethodKind::Inherent) => true,
             _ => {
                 self.tcx.sess
                     .struct_span_err(attr.span, "attribute should be applied to a function")
@@ -342,7 +419,7 @@ fn check_repr(
         // Warn on repr(u8, u16), repr(C, simd), and c-like-enum-repr(C, u8)
         if (int_reprs > 1)
            || (is_simd && is_c)
-           || (int_reprs == 1 && is_c && item.map(|item| is_c_like_enum(item)).unwrap_or(false)) {
+           || (int_reprs == 1 && is_c && item.map_or(false, |item| is_c_like_enum(item))) {
             let hint_spans: Vec<_> = hint_spans.collect();
             span_warn!(self.tcx.sess, hint_spans, E0566,
                        "conflicting representation hints");
@@ -427,6 +504,18 @@ fn visit_trait_item(&mut self, trait_item: &'tcx TraitItem) {
         intravisit::walk_trait_item(self, trait_item)
     }
 
+    fn visit_foreign_item(&mut self, f_item: &'tcx hir::ForeignItem) {
+        let target = Target::from_foreign_item(f_item);
+        self.check_attributes(f_item.hir_id, &f_item.attrs, &f_item.span, target, None);
+        intravisit::walk_foreign_item(self, f_item)
+    }
+
+    fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
+        let target = Target::from_impl_item(self.tcx, impl_item);
+        self.check_attributes(impl_item.hir_id, &impl_item.attrs, &impl_item.span, target, None);
+        intravisit::walk_impl_item(self, impl_item)
+    }
+
     fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt) {
         self.check_stmt_attributes(stmt);
         intravisit::walk_stmt(self, stmt)