Skip to content

Commit 8042206

Browse files
committed
Handle ImplItem in check_attr
1 parent 41ee9ea commit 8042206

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

src/librustc/hir/check_attr.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ impl Target {
119119
hir::ForeignItemKind::Type => Target::ForeignTy,
120120
}
121121
}
122+
123+
fn from_impl_item(impl_item: &hir::ImplItem) -> Target {
124+
match impl_item.kind {
125+
hir::ImplItemKind::Const(..) => Target::Const,
126+
hir::ImplItemKind::Method(..) => Target::Method { body: true },
127+
hir::ImplItemKind::TyAlias(..) => Target::TyAlias,
128+
hir::ImplItemKind::OpaqueTy(..) => Target::OpaqueTy,
129+
}
130+
}
122131
}
123132

124133
struct CheckAttrVisitor<'tcx> {
@@ -360,7 +369,7 @@ impl CheckAttrVisitor<'tcx> {
360369
// Warn on repr(u8, u16), repr(C, simd), and c-like-enum-repr(C, u8)
361370
if (int_reprs > 1)
362371
|| (is_simd && is_c)
363-
|| (int_reprs == 1 && is_c && item.map(|item| is_c_like_enum(item)).unwrap_or(false)) {
372+
|| (int_reprs == 1 && is_c && item.map_or(false, |item| is_c_like_enum(item))) {
364373
let hint_spans: Vec<_> = hint_spans.collect();
365374
span_warn!(self.tcx.sess, hint_spans, E0566,
366375
"conflicting representation hints");
@@ -451,6 +460,12 @@ impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
451460
intravisit::walk_foreign_item(self, f_item)
452461
}
453462

463+
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
464+
let target = Target::from_impl_item(impl_item);
465+
self.check_attributes(impl_item.hir_id, &impl_item.attrs, &impl_item.span, target, None);
466+
intravisit::walk_impl_item(self, impl_item)
467+
}
468+
454469
fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt) {
455470
self.check_stmt_attributes(stmt);
456471
intravisit::walk_stmt(self, stmt)

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

+14
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
#![feature(extern_types)]
2+
#![feature(type_alias_impl_trait)]
23

34
trait Trait {
45
#[inline] //~ ERROR attribute should be applied to function or closure
56
const X: u32;
67

78
#[inline] //~ ERROR attribute should be applied to function or closure
89
type T;
10+
11+
type U;
12+
}
13+
14+
impl Trait for () {
15+
#[inline] //~ ERROR attribute should be applied to function or closure
16+
const X: u32 = 0;
17+
18+
#[inline] //~ ERROR attribute should be applied to function or closure
19+
type T = Self;
20+
21+
#[inline] //~ ERROR attribute should be applied to function or closure
22+
type U = impl Trait; //~ ERROR could not find defining uses
923
}
1024

1125
extern {
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,65 @@
11
error[E0518]: attribute should be applied to function or closure
2-
--> $DIR/inline-trait-and-foreign-items.rs:12:5
2+
--> $DIR/inline-trait-and-foreign-items.rs:26: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:15:5
10+
--> $DIR/inline-trait-and-foreign-items.rs:29:5
1111
|
1212
LL | #[inline]
1313
| ^^^^^^^^^
1414
LL | type T;
1515
| ------- not a function or closure
1616

1717
error[E0518]: attribute should be applied to function or closure
18-
--> $DIR/inline-trait-and-foreign-items.rs:4:5
18+
--> $DIR/inline-trait-and-foreign-items.rs:5:5
1919
|
2020
LL | #[inline]
2121
| ^^^^^^^^^
2222
LL | const X: u32;
2323
| ------------- not a function or closure
2424

2525
error[E0518]: attribute should be applied to function or closure
26-
--> $DIR/inline-trait-and-foreign-items.rs:7:5
26+
--> $DIR/inline-trait-and-foreign-items.rs:8:5
2727
|
2828
LL | #[inline]
2929
| ^^^^^^^^^
3030
LL | type T;
3131
| ------- not a function or closure
3232

33-
error: aborting due to 4 previous errors
33+
error[E0518]: attribute should be applied to function or closure
34+
--> $DIR/inline-trait-and-foreign-items.rs:15:5
35+
|
36+
LL | #[inline]
37+
| ^^^^^^^^^
38+
LL | const X: u32 = 0;
39+
| ----------------- not a function or closure
40+
41+
error[E0518]: attribute should be applied to function or closure
42+
--> $DIR/inline-trait-and-foreign-items.rs:18:5
43+
|
44+
LL | #[inline]
45+
| ^^^^^^^^^
46+
LL | type T = Self;
47+
| -------------- not a function or closure
48+
49+
error[E0518]: attribute should be applied to function or closure
50+
--> $DIR/inline-trait-and-foreign-items.rs:21:5
51+
|
52+
LL | #[inline]
53+
| ^^^^^^^^^
54+
LL | type U = impl Trait;
55+
| -------------------- not a function or closure
56+
57+
error: could not find defining uses
58+
--> $DIR/inline-trait-and-foreign-items.rs:22:5
59+
|
60+
LL | type U = impl Trait;
61+
| ^^^^^^^^^^^^^^^^^^^^
62+
63+
error: aborting due to 8 previous errors
3464

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

0 commit comments

Comments
 (0)