Skip to content

Commit 1b7e79a

Browse files
committed
Move check_mod_attr query in librustc_passes.
1 parent f0a8991 commit 1b7e79a

File tree

4 files changed

+135
-119
lines changed

4 files changed

+135
-119
lines changed

src/librustc/hir/check_attr.rs

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//! This module implements some validity checks for attributes.
2+
//! In particular it verifies that `#[inline]` and `#[repr]` attributes are
3+
//! attached to items that actually support them and if there are
4+
//! conflicts between multiple such attributes attached to the same
5+
//! item.
6+
7+
use rustc_hir as hir;
8+
use rustc_hir::{Item, ItemKind, TraitItem, TraitItemKind};
9+
10+
use std::fmt::{self, Display};
11+
12+
#[derive(Copy, Clone, PartialEq)]
13+
pub enum MethodKind {
14+
Trait { body: bool },
15+
Inherent,
16+
}
17+
18+
#[derive(Copy, Clone, PartialEq)]
19+
pub enum Target {
20+
ExternCrate,
21+
Use,
22+
Static,
23+
Const,
24+
Fn,
25+
Closure,
26+
Mod,
27+
ForeignMod,
28+
GlobalAsm,
29+
TyAlias,
30+
OpaqueTy,
31+
Enum,
32+
Struct,
33+
Union,
34+
Trait,
35+
TraitAlias,
36+
Impl,
37+
Expression,
38+
Statement,
39+
AssocConst,
40+
Method(MethodKind),
41+
AssocTy,
42+
ForeignFn,
43+
ForeignStatic,
44+
ForeignTy,
45+
}
46+
47+
impl Display for Target {
48+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49+
write!(
50+
f,
51+
"{}",
52+
match *self {
53+
Target::ExternCrate => "extern crate",
54+
Target::Use => "use",
55+
Target::Static => "static item",
56+
Target::Const => "constant item",
57+
Target::Fn => "function",
58+
Target::Closure => "closure",
59+
Target::Mod => "module",
60+
Target::ForeignMod => "foreign module",
61+
Target::GlobalAsm => "global asm",
62+
Target::TyAlias => "type alias",
63+
Target::OpaqueTy => "opaque type",
64+
Target::Enum => "enum",
65+
Target::Struct => "struct",
66+
Target::Union => "union",
67+
Target::Trait => "trait",
68+
Target::TraitAlias => "trait alias",
69+
Target::Impl => "item",
70+
Target::Expression => "expression",
71+
Target::Statement => "statement",
72+
Target::AssocConst => "associated const",
73+
Target::Method(_) => "method",
74+
Target::AssocTy => "associated type",
75+
Target::ForeignFn => "foreign function",
76+
Target::ForeignStatic => "foreign static item",
77+
Target::ForeignTy => "foreign type",
78+
}
79+
)
80+
}
81+
}
82+
83+
impl Target {
84+
pub fn from_item(item: &Item<'_>) -> Target {
85+
match item.kind {
86+
ItemKind::ExternCrate(..) => Target::ExternCrate,
87+
ItemKind::Use(..) => Target::Use,
88+
ItemKind::Static(..) => Target::Static,
89+
ItemKind::Const(..) => Target::Const,
90+
ItemKind::Fn(..) => Target::Fn,
91+
ItemKind::Mod(..) => Target::Mod,
92+
ItemKind::ForeignMod(..) => Target::ForeignMod,
93+
ItemKind::GlobalAsm(..) => Target::GlobalAsm,
94+
ItemKind::TyAlias(..) => Target::TyAlias,
95+
ItemKind::OpaqueTy(..) => Target::OpaqueTy,
96+
ItemKind::Enum(..) => Target::Enum,
97+
ItemKind::Struct(..) => Target::Struct,
98+
ItemKind::Union(..) => Target::Union,
99+
ItemKind::Trait(..) => Target::Trait,
100+
ItemKind::TraitAlias(..) => Target::TraitAlias,
101+
ItemKind::Impl { .. } => Target::Impl,
102+
}
103+
}
104+
105+
pub fn from_trait_item(trait_item: &TraitItem<'_>) -> Target {
106+
match trait_item.kind {
107+
TraitItemKind::Const(..) => Target::AssocConst,
108+
TraitItemKind::Method(_, hir::TraitMethod::Required(_)) => {
109+
Target::Method(MethodKind::Trait { body: false })
110+
}
111+
TraitItemKind::Method(_, hir::TraitMethod::Provided(_)) => {
112+
Target::Method(MethodKind::Trait { body: true })
113+
}
114+
TraitItemKind::Type(..) => Target::AssocTy,
115+
}
116+
}
117+
118+
pub fn from_foreign_item(foreign_item: &hir::ForeignItem<'_>) -> Target {
119+
match foreign_item.kind {
120+
hir::ForeignItemKind::Fn(..) => Target::ForeignFn,
121+
hir::ForeignItemKind::Static(..) => Target::ForeignStatic,
122+
hir::ForeignItemKind::Type => Target::ForeignTy,
123+
}
124+
}
125+
}

src/librustc/hir/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ pub mod upvars;
1010
use crate::ty::query::Providers;
1111

1212
pub fn provide(providers: &mut Providers<'_>) {
13-
check_attr::provide(providers);
1413
map::provide(providers);
1514
upvars::provide(providers);
1615
}

src/librustc_passes/check_attr.rs

+8-118
Original file line numberDiff line numberDiff line change
@@ -4,138 +4,28 @@
44
//! conflicts between multiple such attributes attached to the same
55
//! item.
66
7-
use crate::hir::map::Map;
8-
use crate::ty::query::Providers;
9-
use crate::ty::TyCtxt;
7+
use rustc::hir::check_attr::{MethodKind, Target};
8+
use rustc::hir::map::Map;
9+
use rustc::ty::query::Providers;
10+
use rustc::ty::TyCtxt;
1011

1112
use rustc_errors::struct_span_err;
1213
use rustc_hir as hir;
1314
use rustc_hir::def_id::DefId;
1415
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
1516
use rustc_hir::DUMMY_HIR_ID;
16-
use rustc_hir::{self, HirId, Item, ItemKind, TraitItem, TraitItemKind};
17+
use rustc_hir::{self, HirId, Item, ItemKind, TraitItem};
1718
use rustc_session::lint::builtin::{CONFLICTING_REPR_HINTS, UNUSED_ATTRIBUTES};
1819
use rustc_span::symbol::sym;
1920
use rustc_span::Span;
2021
use syntax::ast::Attribute;
2122
use syntax::attr;
2223

23-
use std::fmt::{self, Display};
24-
25-
#[derive(Copy, Clone, PartialEq)]
26-
pub(crate) enum MethodKind {
27-
Trait { body: bool },
28-
Inherent,
29-
}
30-
31-
#[derive(Copy, Clone, PartialEq)]
32-
pub(crate) enum Target {
33-
ExternCrate,
34-
Use,
35-
Static,
36-
Const,
37-
Fn,
38-
Closure,
39-
Mod,
40-
ForeignMod,
41-
GlobalAsm,
42-
TyAlias,
43-
OpaqueTy,
44-
Enum,
45-
Struct,
46-
Union,
47-
Trait,
48-
TraitAlias,
49-
Impl,
50-
Expression,
51-
Statement,
52-
AssocConst,
53-
Method(MethodKind),
54-
AssocTy,
55-
ForeignFn,
56-
ForeignStatic,
57-
ForeignTy,
24+
pub(crate) trait TargetExt {
25+
fn from_impl_item<'tcx>(tcx: TyCtxt<'tcx>, impl_item: &hir::ImplItem<'_>) -> Target;
5826
}
5927

60-
impl Display for Target {
61-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62-
write!(
63-
f,
64-
"{}",
65-
match *self {
66-
Target::ExternCrate => "extern crate",
67-
Target::Use => "use",
68-
Target::Static => "static item",
69-
Target::Const => "constant item",
70-
Target::Fn => "function",
71-
Target::Closure => "closure",
72-
Target::Mod => "module",
73-
Target::ForeignMod => "foreign module",
74-
Target::GlobalAsm => "global asm",
75-
Target::TyAlias => "type alias",
76-
Target::OpaqueTy => "opaque type",
77-
Target::Enum => "enum",
78-
Target::Struct => "struct",
79-
Target::Union => "union",
80-
Target::Trait => "trait",
81-
Target::TraitAlias => "trait alias",
82-
Target::Impl => "item",
83-
Target::Expression => "expression",
84-
Target::Statement => "statement",
85-
Target::AssocConst => "associated const",
86-
Target::Method(_) => "method",
87-
Target::AssocTy => "associated type",
88-
Target::ForeignFn => "foreign function",
89-
Target::ForeignStatic => "foreign static item",
90-
Target::ForeignTy => "foreign type",
91-
}
92-
)
93-
}
94-
}
95-
96-
impl Target {
97-
pub(crate) fn from_item(item: &Item<'_>) -> Target {
98-
match item.kind {
99-
ItemKind::ExternCrate(..) => Target::ExternCrate,
100-
ItemKind::Use(..) => Target::Use,
101-
ItemKind::Static(..) => Target::Static,
102-
ItemKind::Const(..) => Target::Const,
103-
ItemKind::Fn(..) => Target::Fn,
104-
ItemKind::Mod(..) => Target::Mod,
105-
ItemKind::ForeignMod(..) => Target::ForeignMod,
106-
ItemKind::GlobalAsm(..) => Target::GlobalAsm,
107-
ItemKind::TyAlias(..) => Target::TyAlias,
108-
ItemKind::OpaqueTy(..) => Target::OpaqueTy,
109-
ItemKind::Enum(..) => Target::Enum,
110-
ItemKind::Struct(..) => Target::Struct,
111-
ItemKind::Union(..) => Target::Union,
112-
ItemKind::Trait(..) => Target::Trait,
113-
ItemKind::TraitAlias(..) => Target::TraitAlias,
114-
ItemKind::Impl { .. } => Target::Impl,
115-
}
116-
}
117-
118-
fn from_trait_item(trait_item: &TraitItem<'_>) -> Target {
119-
match trait_item.kind {
120-
TraitItemKind::Const(..) => Target::AssocConst,
121-
TraitItemKind::Method(_, hir::TraitMethod::Required(_)) => {
122-
Target::Method(MethodKind::Trait { body: false })
123-
}
124-
TraitItemKind::Method(_, hir::TraitMethod::Provided(_)) => {
125-
Target::Method(MethodKind::Trait { body: true })
126-
}
127-
TraitItemKind::Type(..) => Target::AssocTy,
128-
}
129-
}
130-
131-
fn from_foreign_item(foreign_item: &hir::ForeignItem<'_>) -> Target {
132-
match foreign_item.kind {
133-
hir::ForeignItemKind::Fn(..) => Target::ForeignFn,
134-
hir::ForeignItemKind::Static(..) => Target::ForeignStatic,
135-
hir::ForeignItemKind::Type => Target::ForeignTy,
136-
}
137-
}
138-
28+
impl TargetExt for Target {
13929
fn from_impl_item<'tcx>(tcx: TyCtxt<'tcx>, impl_item: &hir::ImplItem<'_>) -> Target {
14030
match impl_item.kind {
14131
hir::ImplItemKind::Const(..) => Target::AssocConst,

src/librustc_passes/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extern crate log;
1717

1818
use rustc::ty::query::Providers;
1919

20+
mod check_attr;
2021
mod check_const;
2122
pub mod dead;
2223
mod diagnostic_items;
@@ -32,6 +33,7 @@ mod region;
3233
pub mod stability;
3334

3435
pub fn provide(providers: &mut Providers<'_>) {
36+
check_attr::provide(providers);
3537
check_const::provide(providers);
3638
diagnostic_items::provide(providers);
3739
entry::provide(providers);

0 commit comments

Comments
 (0)