]>
Commit | Line | Data |
---|---|---|
74b04a01 XL |
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 crate::hir; | |
8 | use crate::{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, | |
3dfed10e | 32 | Variant, |
74b04a01 XL |
33 | Struct, |
34 | Union, | |
35 | Trait, | |
36 | TraitAlias, | |
37 | Impl, | |
38 | Expression, | |
39 | Statement, | |
40 | AssocConst, | |
41 | Method(MethodKind), | |
42 | AssocTy, | |
43 | ForeignFn, | |
44 | ForeignStatic, | |
45 | ForeignTy, | |
46 | } | |
47 | ||
48 | impl Display for Target { | |
49 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
50 | write!( | |
51 | f, | |
52 | "{}", | |
53 | match *self { | |
54 | Target::ExternCrate => "extern crate", | |
55 | Target::Use => "use", | |
56 | Target::Static => "static item", | |
57 | Target::Const => "constant item", | |
58 | Target::Fn => "function", | |
59 | Target::Closure => "closure", | |
60 | Target::Mod => "module", | |
61 | Target::ForeignMod => "foreign module", | |
62 | Target::GlobalAsm => "global asm", | |
63 | Target::TyAlias => "type alias", | |
64 | Target::OpaqueTy => "opaque type", | |
65 | Target::Enum => "enum", | |
3dfed10e | 66 | Target::Variant => "enum variant", |
74b04a01 XL |
67 | Target::Struct => "struct", |
68 | Target::Union => "union", | |
69 | Target::Trait => "trait", | |
70 | Target::TraitAlias => "trait alias", | |
71 | Target::Impl => "item", | |
72 | Target::Expression => "expression", | |
73 | Target::Statement => "statement", | |
74 | Target::AssocConst => "associated const", | |
75 | Target::Method(_) => "method", | |
76 | Target::AssocTy => "associated type", | |
77 | Target::ForeignFn => "foreign function", | |
78 | Target::ForeignStatic => "foreign static item", | |
79 | Target::ForeignTy => "foreign type", | |
80 | } | |
81 | ) | |
82 | } | |
83 | } | |
84 | ||
85 | impl Target { | |
86 | pub fn from_item(item: &Item<'_>) -> Target { | |
87 | match item.kind { | |
88 | ItemKind::ExternCrate(..) => Target::ExternCrate, | |
89 | ItemKind::Use(..) => Target::Use, | |
90 | ItemKind::Static(..) => Target::Static, | |
91 | ItemKind::Const(..) => Target::Const, | |
92 | ItemKind::Fn(..) => Target::Fn, | |
93 | ItemKind::Mod(..) => Target::Mod, | |
94 | ItemKind::ForeignMod(..) => Target::ForeignMod, | |
95 | ItemKind::GlobalAsm(..) => Target::GlobalAsm, | |
96 | ItemKind::TyAlias(..) => Target::TyAlias, | |
97 | ItemKind::OpaqueTy(..) => Target::OpaqueTy, | |
98 | ItemKind::Enum(..) => Target::Enum, | |
99 | ItemKind::Struct(..) => Target::Struct, | |
100 | ItemKind::Union(..) => Target::Union, | |
101 | ItemKind::Trait(..) => Target::Trait, | |
102 | ItemKind::TraitAlias(..) => Target::TraitAlias, | |
103 | ItemKind::Impl { .. } => Target::Impl, | |
104 | } | |
105 | } | |
106 | ||
107 | pub fn from_trait_item(trait_item: &TraitItem<'_>) -> Target { | |
108 | match trait_item.kind { | |
109 | TraitItemKind::Const(..) => Target::AssocConst, | |
ba9703b0 | 110 | TraitItemKind::Fn(_, hir::TraitFn::Required(_)) => { |
74b04a01 XL |
111 | Target::Method(MethodKind::Trait { body: false }) |
112 | } | |
ba9703b0 | 113 | TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => { |
74b04a01 XL |
114 | Target::Method(MethodKind::Trait { body: true }) |
115 | } | |
116 | TraitItemKind::Type(..) => Target::AssocTy, | |
117 | } | |
118 | } | |
119 | ||
120 | pub fn from_foreign_item(foreign_item: &hir::ForeignItem<'_>) -> Target { | |
121 | match foreign_item.kind { | |
122 | hir::ForeignItemKind::Fn(..) => Target::ForeignFn, | |
123 | hir::ForeignItemKind::Static(..) => Target::ForeignStatic, | |
124 | hir::ForeignItemKind::Type => Target::ForeignTy, | |
125 | } | |
126 | } | |
127 | } |