Skip to content

Commit f47f530

Browse files
committed
Make inline associated constants a future compatibility warning
1 parent f8db8ff commit f47f530

File tree

3 files changed

+54
-25
lines changed

3 files changed

+54
-25
lines changed

src/librustc/hir/check_attr.rs

+26-8
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl Target {
128128

129129
fn from_impl_item<'tcx>(tcx: TyCtxt<'tcx>, impl_item: &hir::ImplItem) -> Target {
130130
match impl_item.kind {
131-
hir::ImplItemKind::Const(..) => Target::Const,
131+
hir::ImplItemKind::Const(..) => Target::AssocConst,
132132
hir::ImplItemKind::Method(..) => {
133133
let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id);
134134
let containing_item = tcx.hir().expect_item(parent_hir_id);
@@ -142,8 +142,7 @@ impl Target {
142142
Target::Method(MethodKind::Inherent)
143143
}
144144
}
145-
hir::ImplItemKind::TyAlias(..) => Target::TyAlias,
146-
hir::ImplItemKind::OpaqueTy(..) => Target::OpaqueTy,
145+
hir::ImplItemKind::TyAlias(..) | hir::ImplItemKind::OpaqueTy(..) => Target::AssocTy,
147146
}
148147
}
149148
}
@@ -205,12 +204,31 @@ impl CheckAttrVisitor<'tcx> {
205204
).emit();
206205
true
207206
}
207+
// FIXME(#65833): We permit associated consts to have an `#[inline]` attribute with
208+
// just a lint, because we previously erroneously allowed it and some crates used it
209+
// accidentally, to to be compatible with crates depending on them, we can't throw an
210+
// error here.
211+
Target::AssocConst => {
212+
self.tcx.struct_span_lint_hir(
213+
UNUSED_ATTRIBUTES,
214+
hir_id,
215+
attr.span,
216+
"`#[inline]` is ignored on constants",
217+
).warn("this was previously accepted by the compiler but is \
218+
being phased out; it will become a hard error in \
219+
a future release!")
220+
.note("for more information, see issue #65833 \
221+
<https://github.com/rust-lang/rust/issues/65833>")
222+
.emit();
223+
true
224+
}
208225
_ => {
209-
struct_span_err!(self.tcx.sess,
210-
attr.span,
211-
E0518,
212-
"attribute should be applied to function or closure")
213-
.span_label(*span, "not a function or closure")
226+
struct_span_err!(
227+
self.tcx.sess,
228+
attr.span,
229+
E0518,
230+
"attribute should be applied to function or closure",
231+
).span_label(*span, "not a function or closure")
214232
.emit();
215233
false
216234
}

src/test/ui/lint/inline-trait-and-foreign-items.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#![feature(extern_types)]
22
#![feature(type_alias_impl_trait)]
33

4+
#![warn(unused_attributes)]
5+
46
trait Trait {
5-
#[inline] //~ ERROR attribute should be applied to function or closure
7+
#[inline] //~ WARN `#[inline]` is ignored on constants
8+
//~^ WARN this was previously accepted
69
const X: u32;
710

811
#[inline] //~ ERROR attribute should be applied to function or closure
@@ -12,7 +15,8 @@ trait Trait {
1215
}
1316

1417
impl Trait for () {
15-
#[inline] //~ ERROR attribute should be applied to function or closure
18+
#[inline] //~ WARN `#[inline]` is ignored on constants
19+
//~^ WARN this was previously accepted
1620
const X: u32 = 0;
1721

1822
#[inline] //~ ERROR attribute should be applied to function or closure
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,72 @@
11
error[E0518]: attribute should be applied to function or closure
2-
--> $DIR/inline-trait-and-foreign-items.rs:26:5
2+
--> $DIR/inline-trait-and-foreign-items.rs:30:5
33
|
44
LL | #[inline]
55
| ^^^^^^^^^
66
LL | static X: u32;
77
| -------------- not a function or closure
88

99
error[E0518]: attribute should be applied to function or closure
10-
--> $DIR/inline-trait-and-foreign-items.rs:29:5
10+
--> $DIR/inline-trait-and-foreign-items.rs:33:5
1111
|
1212
LL | #[inline]
1313
| ^^^^^^^^^
1414
LL | type T;
1515
| ------- not a function or closure
1616

17-
error[E0518]: attribute should be applied to function or closure
18-
--> $DIR/inline-trait-and-foreign-items.rs:5:5
17+
warning: `#[inline]` is ignored on constants
18+
--> $DIR/inline-trait-and-foreign-items.rs:7:5
1919
|
2020
LL | #[inline]
2121
| ^^^^^^^^^
22-
LL | const X: u32;
23-
| ------------- not a function or closure
22+
|
23+
note: lint level defined here
24+
--> $DIR/inline-trait-and-foreign-items.rs:4:9
25+
|
26+
LL | #![warn(unused_attributes)]
27+
| ^^^^^^^^^^^^^^^^^
28+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
29+
= note: for more information, see issue #65833 <https://github.com/rust-lang/rust/issues/65833>
2430

2531
error[E0518]: attribute should be applied to function or closure
26-
--> $DIR/inline-trait-and-foreign-items.rs:8:5
32+
--> $DIR/inline-trait-and-foreign-items.rs:11:5
2733
|
2834
LL | #[inline]
2935
| ^^^^^^^^^
3036
LL | type T;
3137
| ------- not a function or closure
3238

33-
error[E0518]: attribute should be applied to function or closure
34-
--> $DIR/inline-trait-and-foreign-items.rs:15:5
39+
warning: `#[inline]` is ignored on constants
40+
--> $DIR/inline-trait-and-foreign-items.rs:18:5
3541
|
3642
LL | #[inline]
3743
| ^^^^^^^^^
38-
LL | const X: u32 = 0;
39-
| ----------------- not a function or closure
44+
|
45+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
46+
= note: for more information, see issue #65833 <https://github.com/rust-lang/rust/issues/65833>
4047

4148
error[E0518]: attribute should be applied to function or closure
42-
--> $DIR/inline-trait-and-foreign-items.rs:18:5
49+
--> $DIR/inline-trait-and-foreign-items.rs:22:5
4350
|
4451
LL | #[inline]
4552
| ^^^^^^^^^
4653
LL | type T = Self;
4754
| -------------- not a function or closure
4855

4956
error[E0518]: attribute should be applied to function or closure
50-
--> $DIR/inline-trait-and-foreign-items.rs:21:5
57+
--> $DIR/inline-trait-and-foreign-items.rs:25:5
5158
|
5259
LL | #[inline]
5360
| ^^^^^^^^^
5461
LL | type U = impl Trait;
5562
| -------------------- not a function or closure
5663

5764
error: could not find defining uses
58-
--> $DIR/inline-trait-and-foreign-items.rs:22:5
65+
--> $DIR/inline-trait-and-foreign-items.rs:26:5
5966
|
6067
LL | type U = impl Trait;
6168
| ^^^^^^^^^^^^^^^^^^^^
6269

63-
error: aborting due to 8 previous errors
70+
error: aborting due to 6 previous errors
6471

6572
For more information about this error, try `rustc --explain E0518`.

0 commit comments

Comments
 (0)