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),
59 impl Display for Target {
60 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 Target::ExternCrate => "extern crate",
67 Target::Static => "static item",
68 Target::Const => "constant item",
69 Target::Fn => "function",
70 Target::Closure => "closure",
71 Target::Mod => "module",
72 Target::ForeignMod => "foreign module",
73 Target::GlobalAsm => "global asm",
74 Target::TyAlias => "type alias",
75 Target::OpaqueTy => "opaque type",
76 Target::Enum => "enum",
77 Target::Variant => "enum variant",
78 Target::Struct => "struct",
79 Target::Field => "struct field",
80 Target::Union => "union",
81 Target::Trait => "trait",
82 Target::TraitAlias => "trait alias",
83 Target::Impl => "item",
84 Target::Expression => "expression",
85 Target::Statement => "statement",
86 Target::Arm => "match arm",
87 Target::AssocConst => "associated const",
88 Target::Method(_) => "method",
89 Target::AssocTy => "associated type",
90 Target::ForeignFn => "foreign function",
91 Target::ForeignStatic => "foreign static item",
92 Target::ForeignTy => "foreign type",
93 Target::GenericParam(kind) => match kind {
94 GenericParamKind::Type => "type parameter",
95 GenericParamKind::Lifetime => "lifetime parameter",
96 GenericParamKind::Const => "const parameter",
98 Target::MacroDef => "macro def",
105 pub fn from_item(item: &Item<'_>) -> Target {
107 ItemKind::ExternCrate(..) => Target::ExternCrate,
108 ItemKind::Use(..) => Target::Use,
109 ItemKind::Static(..) => Target::Static,
110 ItemKind::Const(..) => Target::Const,
111 ItemKind::Fn(..) => Target::Fn,
112 ItemKind::Mod(..) => Target::Mod,
113 ItemKind::ForeignMod { .. } => Target::ForeignMod,
114 ItemKind::GlobalAsm(..) => Target::GlobalAsm,
115 ItemKind::TyAlias(..) => Target::TyAlias,
116 ItemKind::OpaqueTy(..) => Target::OpaqueTy,
117 ItemKind::Enum(..) => Target::Enum,
118 ItemKind::Struct(..) => Target::Struct,
119 ItemKind::Union(..) => Target::Union,
120 ItemKind::Trait(..) => Target::Trait,
121 ItemKind::TraitAlias(..) => Target::TraitAlias,
122 ItemKind::Impl { .. } => Target::Impl,
126 pub fn from_trait_item(trait_item: &TraitItem<'_>) -> Target {
127 match trait_item.kind {
128 TraitItemKind::Const(..) => Target::AssocConst,
129 TraitItemKind::Fn(_, hir::TraitFn::Required(_)) => {
130 Target::Method(MethodKind::Trait { body: false })
132 TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => {
133 Target::Method(MethodKind::Trait { body: true })
135 TraitItemKind::Type(..) => Target::AssocTy,
139 pub fn from_foreign_item(foreign_item: &hir::ForeignItem<'_>) -> Target {
140 match foreign_item.kind {
141 hir::ForeignItemKind::Fn(..) => Target::ForeignFn,
142 hir::ForeignItemKind::Static(..) => Target::ForeignStatic,
143 hir::ForeignItemKind::Type => Target::ForeignTy,
147 pub fn from_generic_param(generic_param: &hir::GenericParam<'_>) -> Target {
148 match generic_param.kind {
149 hir::GenericParamKind::Type { .. } => Target::GenericParam(GenericParamKind::Type),
150 hir::GenericParamKind::Lifetime { .. } => {
151 Target::GenericParam(GenericParamKind::Lifetime)
153 hir::GenericParamKind::Const { .. } => Target::GenericParam(GenericParamKind::Const),