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)]
18 #[derive(Copy, Clone, PartialEq, Debug)]
48 impl Display for Target {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 Target::ExternCrate => "extern crate",
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",
66 Target::Variant => "enum variant",
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",
86 pub fn from_item(item: &Item<'_>) -> Target {
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,
107 pub fn from_trait_item(trait_item: &TraitItem<'_>) -> Target {
108 match trait_item.kind {
109 TraitItemKind::Const(..) => Target::AssocConst,
110 TraitItemKind::Fn(_, hir::TraitFn::Required(_)) => {
111 Target::Method(MethodKind::Trait { body: false })
113 TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => {
114 Target::Method(MethodKind::Trait { body: true })
116 TraitItemKind::Type(..) => Target::AssocTy,
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,