]> git.lizzy.rs Git - rust.git/commitdiff
Make inline associated constants a future compatibility warning
authorvarkor <github@varkor.com>
Fri, 25 Oct 2019 23:46:07 +0000 (00:46 +0100)
committervarkor <github@varkor.com>
Fri, 25 Oct 2019 23:46:07 +0000 (00:46 +0100)
src/librustc/hir/check_attr.rs
src/test/ui/lint/inline-trait-and-foreign-items.rs
src/test/ui/lint/inline-trait-and-foreign-items.stderr

index 7ee1d3448c4e9d023e1582ab22fc4246b1d156ec..96562002aa070240c1cffeee6b91d7bf7e91fe5a 100644 (file)
@@ -128,7 +128,7 @@ fn from_foreign_item(foreign_item: &hir::ForeignItem) -> 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::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);
@@ -142,8 +142,7 @@ fn from_impl_item<'tcx>(tcx: TyCtxt<'tcx>, impl_item: &hir::ImplItem) -> Target
                     Target::Method(MethodKind::Inherent)
                 }
             }
-            hir::ImplItemKind::TyAlias(..) => Target::TyAlias,
-            hir::ImplItemKind::OpaqueTy(..) => Target::OpaqueTy,
+            hir::ImplItemKind::TyAlias(..) | hir::ImplItemKind::OpaqueTy(..) => Target::AssocTy,
         }
     }
 }
@@ -205,12 +204,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
             }
index 2beb5aaba6590a803a0d72b00dc8dacaa2b630e9..8bdefbb36ae5f21c955bd9edba77e6a02b63f119 100644 (file)
@@ -1,8 +1,11 @@
 #![feature(extern_types)]
 #![feature(type_alias_impl_trait)]
 
+#![warn(unused_attributes)]
+
 trait Trait {
-    #[inline] //~ ERROR attribute should be applied to function or closure
+    #[inline] //~ WARN `#[inline]` is ignored on constants
+    //~^ WARN this was previously accepted
     const X: u32;
 
     #[inline] //~ ERROR attribute should be applied to function or closure
@@ -12,7 +15,8 @@ trait Trait {
 }
 
 impl Trait for () {
-    #[inline] //~ ERROR attribute should be applied to function or closure
+    #[inline] //~ WARN `#[inline]` is ignored on constants
+    //~^ WARN this was previously accepted
     const X: u32 = 0;
 
     #[inline] //~ ERROR attribute should be applied to function or closure
index f67c7a6018ce4b0f49b22d71904c744c274a08ad..6c94f88f139485efc3b77271e95b22ac8de4fd4d 100644 (file)
@@ -1,5 +1,5 @@
 error[E0518]: attribute should be applied to function or closure
-  --> $DIR/inline-trait-and-foreign-items.rs:26:5
+  --> $DIR/inline-trait-and-foreign-items.rs:30:5
    |
 LL |     #[inline]
    |     ^^^^^^^^^
@@ -7,39 +7,46 @@ LL |     static X: u32;
    |     -------------- not a function or closure
 
 error[E0518]: attribute should be applied to function or closure
-  --> $DIR/inline-trait-and-foreign-items.rs:29:5
+  --> $DIR/inline-trait-and-foreign-items.rs:33:5
    |
 LL |     #[inline]
    |     ^^^^^^^^^
 LL |     type T;
    |     ------- not a function or closure
 
-error[E0518]: attribute should be applied to function or closure
-  --> $DIR/inline-trait-and-foreign-items.rs:5:5
+warning: `#[inline]` is ignored on constants
+  --> $DIR/inline-trait-and-foreign-items.rs:7:5
    |
 LL |     #[inline]
    |     ^^^^^^^^^
-LL |     const X: u32;
-   |     ------------- not a function or closure
+   |
+note: lint level defined here
+  --> $DIR/inline-trait-and-foreign-items.rs:4:9
+   |
+LL | #![warn(unused_attributes)]
+   |         ^^^^^^^^^^^^^^^^^
+   = warning: 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>
 
 error[E0518]: attribute should be applied to function or closure
-  --> $DIR/inline-trait-and-foreign-items.rs:8:5
+  --> $DIR/inline-trait-and-foreign-items.rs:11:5
    |
 LL |     #[inline]
    |     ^^^^^^^^^
 LL |     type T;
    |     ------- not a function or closure
 
-error[E0518]: attribute should be applied to function or closure
-  --> $DIR/inline-trait-and-foreign-items.rs:15:5
+warning: `#[inline]` is ignored on constants
+  --> $DIR/inline-trait-and-foreign-items.rs:18:5
    |
 LL |     #[inline]
    |     ^^^^^^^^^
-LL |     const X: u32 = 0;
-   |     ----------------- not a function or closure
+   |
+   = warning: 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>
 
 error[E0518]: attribute should be applied to function or closure
-  --> $DIR/inline-trait-and-foreign-items.rs:18:5
+  --> $DIR/inline-trait-and-foreign-items.rs:22:5
    |
 LL |     #[inline]
    |     ^^^^^^^^^
@@ -47,7 +54,7 @@ LL |     type T = Self;
    |     -------------- not a function or closure
 
 error[E0518]: attribute should be applied to function or closure
-  --> $DIR/inline-trait-and-foreign-items.rs:21:5
+  --> $DIR/inline-trait-and-foreign-items.rs:25:5
    |
 LL |     #[inline]
    |     ^^^^^^^^^
@@ -55,11 +62,11 @@ LL |     type U = impl Trait;
    |     -------------------- not a function or closure
 
 error: could not find defining uses
-  --> $DIR/inline-trait-and-foreign-items.rs:22:5
+  --> $DIR/inline-trait-and-foreign-items.rs:26:5
    |
 LL |     type U = impl Trait;
    |     ^^^^^^^^^^^^^^^^^^^^
 
-error: aborting due to 8 previous errors
+error: aborting due to 6 previous errors
 
 For more information about this error, try `rustc --explain E0518`.