Skip to content

Commit

Permalink
Make inline associated constants a future compatibility warning
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Oct 25, 2019
1 parent f8db8ff commit f47f530
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 25 deletions.
34 changes: 26 additions & 8 deletions src/librustc/hir/check_attr.rs
Expand Up @@ -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);
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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 \
<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
}
Expand Down
8 changes: 6 additions & 2 deletions 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
Expand All @@ -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
Expand Down
37 changes: 22 additions & 15 deletions src/test/ui/lint/inline-trait-and-foreign-items.stderr
@@ -1,65 +1,72 @@
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]
| ^^^^^^^^^
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]
| ^^^^^^^^^
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]
| ^^^^^^^^^
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`.

0 comments on commit f47f530

Please sign in to comment.