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)]
18 #[derive(Copy, Clone, PartialEq)]
47 impl Display for Target {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 Target::ExternCrate => "extern crate",
55 Target::Static => "static item",
56 Target::Const => "constant item",
57 Target::Fn => "function",
58 Target::Closure => "closure",
59 Target::Mod => "module",
60 Target::ForeignMod => "foreign module",
61 Target::GlobalAsm => "global asm",
62 Target::TyAlias => "type alias",
63 Target::OpaqueTy => "opaque type",
64 Target::Enum => "enum",
65 Target::Struct => "struct",
66 Target::Union => "union",
67 Target::Trait => "trait",
68 Target::TraitAlias => "trait alias",
69 Target::Impl => "item",
70 Target::Expression => "expression",
71 Target::Statement => "statement",
72 Target::AssocConst => "associated const",
73 Target::Method(_) => "method",
74 Target::AssocTy => "associated type",
75 Target::ForeignFn => "foreign function",
76 Target::ForeignStatic => "foreign static item",
77 Target::ForeignTy => "foreign type",
84 pub fn from_item(item: &Item<'_>) -> Target {
86 ItemKind::ExternCrate(..) => Target::ExternCrate,
87 ItemKind::Use(..) => Target::Use,
88 ItemKind::Static(..) => Target::Static,
89 ItemKind::Const(..) => Target::Const,
90 ItemKind::Fn(..) => Target::Fn,
91 ItemKind::Mod(..) => Target::Mod,
92 ItemKind::ForeignMod(..) => Target::ForeignMod,
93 ItemKind::GlobalAsm(..) => Target::GlobalAsm,
94 ItemKind::TyAlias(..) => Target::TyAlias,
95 ItemKind::OpaqueTy(..) => Target::OpaqueTy,
96 ItemKind::Enum(..) => Target::Enum,
97 ItemKind::Struct(..) => Target::Struct,
98 ItemKind::Union(..) => Target::Union,
99 ItemKind::Trait(..) => Target::Trait,
100 ItemKind::TraitAlias(..) => Target::TraitAlias,
101 ItemKind::Impl { .. } => Target::Impl,
105 pub fn from_trait_item(trait_item: &TraitItem<'_>) -> Target {
106 match trait_item.kind {
107 TraitItemKind::Const(..) => Target::AssocConst,
108 TraitItemKind::Fn(_, hir::TraitFn::Required(_)) => {
109 Target::Method(MethodKind::Trait { body: false })
111 TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => {
112 Target::Method(MethodKind::Trait { body: true })
114 TraitItemKind::Type(..) => Target::AssocTy,
118 pub fn from_foreign_item(foreign_item: &hir::ForeignItem<'_>) -> Target {
119 match foreign_item.kind {
120 hir::ForeignItemKind::Fn(..) => Target::ForeignFn,
121 hir::ForeignItemKind::Static(..) => Target::ForeignStatic,
122 hir::ForeignItemKind::Type => Target::ForeignTy,