diff --git a/src/librustc/hir/check_attr.rs b/src/librustc/hir/check_attr.rs index 7ee1d3448c4e9..96562002aa070 100644 --- a/src/librustc/hir/check_attr.rs +++ b/src/librustc/hir/check_attr.rs @@ -128,7 +128,7 @@ impl 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 @@ impl 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 @@ impl CheckAttrVisitor<'tcx> { ).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 \ + ") + .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 } diff --git a/src/test/ui/lint/inline-trait-and-foreign-items.rs b/src/test/ui/lint/inline-trait-and-foreign-items.rs index 2beb5aaba6590..8bdefbb36ae5f 100644 --- a/src/test/ui/lint/inline-trait-and-foreign-items.rs +++ b/src/test/ui/lint/inline-trait-and-foreign-items.rs @@ -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 diff --git a/src/test/ui/lint/inline-trait-and-foreign-items.stderr b/src/test/ui/lint/inline-trait-and-foreign-items.stderr index f67c7a6018ce4..6c94f88f13948 100644 --- a/src/test/ui/lint/inline-trait-and-foreign-items.stderr +++ b/src/test/ui/lint/inline-trait-and-foreign-items.stderr @@ -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 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 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`.