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
8 use crate::{Item, ItemKind, TraitItem, TraitItemKind};
10 use crate::def::DefKind;
11 use std::fmt::{self, Display};
13 #[derive(Copy, Clone, PartialEq, Debug)]
14 pub enum GenericParamKind {
20 #[derive(Copy, Clone, PartialEq, Debug)]
26 #[derive(Copy, Clone, PartialEq, Debug)]
56 GenericParam(GenericParamKind),
61 impl Display for Target {
62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67 Target::ExternCrate => "extern crate",
69 Target::Static => "static item",
70 Target::Const => "constant item",
71 Target::Fn => "function",
72 Target::Closure => "closure",
73 Target::Mod => "module",
74 Target::ForeignMod => "foreign module",
75 Target::GlobalAsm => "global asm",
76 Target::TyAlias => "type alias",
77 Target::OpaqueTy => "opaque type",
78 Target::Enum => "enum",
79 Target::Variant => "enum variant",
80 Target::Struct => "struct",
81 Target::Field => "struct field",
82 Target::Union => "union",
83 Target::Trait => "trait",
84 Target::TraitAlias => "trait alias",
85 Target::Impl => "item",
86 Target::Expression => "expression",
87 Target::Statement => "statement",
88 Target::Arm => "match arm",
89 Target::AssocConst => "associated const",
90 Target::Method(kind) => match kind {
91 MethodKind::Inherent => "inherent method",
92 MethodKind::Trait { body: false } => "required trait method",
93 MethodKind::Trait { body: true } => "provided trait method",
95 Target::AssocTy => "associated type",
96 Target::ForeignFn => "foreign function",
97 Target::ForeignStatic => "foreign static item",
98 Target::ForeignTy => "foreign type",
99 Target::GenericParam(kind) => match kind {
100 GenericParamKind::Type => "type parameter",
101 GenericParamKind::Lifetime => "lifetime parameter",
102 GenericParamKind::Const => "const parameter",
104 Target::MacroDef => "macro def",
105 Target::Param => "function param",
112 pub fn from_item(item: &Item<'_>) -> Target {
114 ItemKind::ExternCrate(..) => Target::ExternCrate,
115 ItemKind::Use(..) => Target::Use,
116 ItemKind::Static(..) => Target::Static,
117 ItemKind::Const(..) => Target::Const,
118 ItemKind::Fn(..) => Target::Fn,
119 ItemKind::Macro(..) => Target::MacroDef,
120 ItemKind::Mod(..) => Target::Mod,
121 ItemKind::ForeignMod { .. } => Target::ForeignMod,
122 ItemKind::GlobalAsm(..) => Target::GlobalAsm,
123 ItemKind::TyAlias(..) => Target::TyAlias,
124 ItemKind::OpaqueTy(..) => Target::OpaqueTy,
125 ItemKind::Enum(..) => Target::Enum,
126 ItemKind::Struct(..) => Target::Struct,
127 ItemKind::Union(..) => Target::Union,
128 ItemKind::Trait(..) => Target::Trait,
129 ItemKind::TraitAlias(..) => Target::TraitAlias,
130 ItemKind::Impl { .. } => Target::Impl,
134 pub fn from_def_kind(def_kind: DefKind) -> Target {
136 DefKind::ExternCrate => Target::ExternCrate,
137 DefKind::Use => Target::Use,
138 DefKind::Static(..) => Target::Static,
139 DefKind::Const => Target::Const,
140 DefKind::Fn => Target::Fn,
141 DefKind::Macro(..) => Target::MacroDef,
142 DefKind::Mod => Target::Mod,
143 DefKind::ForeignMod => Target::ForeignMod,
144 DefKind::GlobalAsm => Target::GlobalAsm,
145 DefKind::TyAlias => Target::TyAlias,
146 DefKind::OpaqueTy => Target::OpaqueTy,
147 DefKind::Enum => Target::Enum,
148 DefKind::Struct => Target::Struct,
149 DefKind::Union => Target::Union,
150 DefKind::Trait => Target::Trait,
151 DefKind::TraitAlias => Target::TraitAlias,
152 DefKind::Impl => Target::Impl,
153 _ => panic!("impossible case reached"),
157 pub fn from_trait_item(trait_item: &TraitItem<'_>) -> Target {
158 match trait_item.kind {
159 TraitItemKind::Const(..) => Target::AssocConst,
160 TraitItemKind::Fn(_, hir::TraitFn::Required(_)) => {
161 Target::Method(MethodKind::Trait { body: false })
163 TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => {
164 Target::Method(MethodKind::Trait { body: true })
166 TraitItemKind::Type(..) => Target::AssocTy,
170 pub fn from_foreign_item(foreign_item: &hir::ForeignItem<'_>) -> Target {
171 match foreign_item.kind {
172 hir::ForeignItemKind::Fn(..) => Target::ForeignFn,
173 hir::ForeignItemKind::Static(..) => Target::ForeignStatic,
174 hir::ForeignItemKind::Type => Target::ForeignTy,
178 pub fn from_generic_param(generic_param: &hir::GenericParam<'_>) -> Target {
179 match generic_param.kind {
180 hir::GenericParamKind::Type { .. } => Target::GenericParam(GenericParamKind::Type),
181 hir::GenericParamKind::Lifetime { .. } => {
182 Target::GenericParam(GenericParamKind::Lifetime)
184 hir::GenericParamKind::Const { .. } => Target::GenericParam(GenericParamKind::Const),