]> 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 55ac95f4ac4b7876d5487003481daf480211b6d3..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,7 +46,7 @@ pub(crate) enum Target {
     Expression,
     Statement,
     AssocConst,
-    Method { body: bool },
+    Method(MethodKind),
     AssocTy,
     ForeignFn,
     ForeignStatic,
@@ -68,7 +76,7 @@ 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",
@@ -103,10 +111,10 @@ 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,
         }
@@ -120,12 +128,23 @@ fn from_foreign_item(foreign_item: &hir::ForeignItem) -> Target {
         }
     }
 
-    fn from_impl_item(impl_item: &hir::ImplItem) -> Target {
+    fn from_impl_item<'tcx>(tcx: TyCtxt<'tcx>, impl_item: &hir::ImplItem) -> Target {
         match impl_item.kind {
-            hir::ImplItemKind::Const(..) => Target::Const,
-            hir::ImplItemKind::Method(..) => Target::Method { body: true },
-            hir::ImplItemKind::TyAlias(..) => Target::TyAlias,
-            hir::ImplItemKind::OpaqueTy(..) => Target::OpaqueTy,
+            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,
         }
     }
 }
@@ -176,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,
@@ -186,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
             }
@@ -216,8 +255,8 @@ fn check_track_caller(
                 ).emit();
                 false
             }
-            Target::Fn => true,
-            Target::Method { .. } => {
+            Target::Fn | Target::Method(MethodKind::Inherent) => true,
+            Target::Method(_) => {
                 struct_span_err!(
                     self.tcx.sess,
                     *attr_span,
@@ -278,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")
@@ -471,7 +511,7 @@ fn visit_foreign_item(&mut self, f_item: &'tcx hir::ForeignItem) {
     }
 
     fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
-        let target = Target::from_impl_item(impl_item);
+        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)
     }