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 std::fmt::{self, Display};
12 #[derive(Copy, Clone, PartialEq, Debug)]
13 pub enum GenericParamKind {
19 #[derive(Copy, Clone, PartialEq, Debug)]
25 #[derive(Copy, Clone, PartialEq, Debug)]
55 GenericParam(GenericParamKind),
60 impl Display for Target {
61 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 Target::ExternCrate => "extern crate",
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::Variant => "enum variant",
79 Target::Struct => "struct",
80 Target::Field => "struct field",
81 Target::Union => "union",
82 Target::Trait => "trait",
83 Target::TraitAlias => "trait alias",
84 Target::Impl => "item",
85 Target::Expression => "expression",
86 Target::Statement => "statement",
87 Target::Arm => "match arm",
88 Target::AssocConst => "associated const",
89 Target::Method(_) => "method",
90 Target::AssocTy => "associated type",
91 Target::ForeignFn => "foreign function",
92 Target::ForeignStatic => "foreign static item",
93 Target::ForeignTy => "foreign type",
94 Target::GenericParam(kind) => match kind {
95 GenericParamKind::Type => "type parameter",
96 GenericParamKind::Lifetime => "lifetime parameter",
97 GenericParamKind::Const => "const parameter",
99 Target::MacroDef => "macro def",
100 Target::Param => "function param",
107 pub fn from_item(item: &Item<'_>) -> Target {
109 ItemKind::ExternCrate(..) => Target::ExternCrate,
110 ItemKind::Use(..) => Target::Use,
111 ItemKind::Static(..) => Target::Static,
112 ItemKind::Const(..) => Target::Const,
113 ItemKind::Fn(..) => Target::Fn,
114 ItemKind::Macro(..) => Target::MacroDef,
115 ItemKind::Mod(..) => Target::Mod,
116 ItemKind::ForeignMod { .. } => Target::ForeignMod,
117 ItemKind::GlobalAsm(..) => Target::GlobalAsm,
118 ItemKind::TyAlias(..) => Target::TyAlias,
119 ItemKind::OpaqueTy(..) => Target::OpaqueTy,
120 ItemKind::Enum(..) => Target::Enum,
121 ItemKind::Struct(..) => Target::Struct,
122 ItemKind::Union(..) => Target::Union,
123 ItemKind::Trait(..) => Target::Trait,
124 ItemKind::TraitAlias(..) => Target::TraitAlias,
125 ItemKind::Impl { .. } => Target::Impl,
129 pub fn from_trait_item(trait_item: &TraitItem<'_>) -> Target {
130 match trait_item.kind {
131 TraitItemKind::Const(..) => Target::AssocConst,
132 TraitItemKind::Fn(_, hir::TraitFn::Required(_)) => {
133 Target::Method(MethodKind::Trait { body: false })
135 TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => {
136 Target::Method(MethodKind::Trait { body: true })
138 TraitItemKind::Type(..) => Target::AssocTy,
142 pub fn from_foreign_item(foreign_item: &hir::ForeignItem<'_>) -> Target {
143 match foreign_item.kind {
144 hir::ForeignItemKind::Fn(..) => Target::ForeignFn,
145 hir::ForeignItemKind::Static(..) => Target::ForeignStatic,
146 hir::ForeignItemKind::Type => Target::ForeignTy,
150 pub fn from_generic_param(generic_param: &hir::GenericParam<'_>) -> Target {
151 match generic_param.kind {
152 hir::GenericParamKind::Type { .. } => Target::GenericParam(GenericParamKind::Type),
153 hir::GenericParamKind::Lifetime { .. } => {
154 Target::GenericParam(GenericParamKind::Lifetime)
156 hir::GenericParamKind::Const { .. } => Target::GenericParam(GenericParamKind::Const),