]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir/src/target.rs
Rollup merge of #94806 - jyn514:cargo-run-tidy, r=Mark-Simulacrum
[rust.git] / compiler / rustc_hir / src / target.rs
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
5 //! item.
6
7 use crate::hir;
8 use crate::{Item, ItemKind, TraitItem, TraitItemKind};
9
10 use std::fmt::{self, Display};
11
12 #[derive(Copy, Clone, PartialEq, Debug)]
13 pub enum GenericParamKind {
14     Type,
15     Lifetime,
16     Const,
17 }
18
19 #[derive(Copy, Clone, PartialEq, Debug)]
20 pub enum MethodKind {
21     Trait { body: bool },
22     Inherent,
23 }
24
25 #[derive(Copy, Clone, PartialEq, Debug)]
26 pub enum Target {
27     ExternCrate,
28     Use,
29     Static,
30     Const,
31     Fn,
32     Closure,
33     Mod,
34     ForeignMod,
35     GlobalAsm,
36     TyAlias,
37     OpaqueTy,
38     Enum,
39     Variant,
40     Struct,
41     Field,
42     Union,
43     Trait,
44     TraitAlias,
45     Impl,
46     Expression,
47     Statement,
48     Arm,
49     AssocConst,
50     Method(MethodKind),
51     AssocTy,
52     ForeignFn,
53     ForeignStatic,
54     ForeignTy,
55     GenericParam(GenericParamKind),
56     MacroDef,
57     Param,
58 }
59
60 impl Display for Target {
61     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62         write!(
63             f,
64             "{}",
65             match *self {
66                 Target::ExternCrate => "extern crate",
67                 Target::Use => "use",
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(kind) => match kind {
90                     MethodKind::Inherent => "inherent method",
91                     MethodKind::Trait { body: false } => "required trait method",
92                     MethodKind::Trait { body: true } => "provided trait method",
93                 },
94                 Target::AssocTy => "associated type",
95                 Target::ForeignFn => "foreign function",
96                 Target::ForeignStatic => "foreign static item",
97                 Target::ForeignTy => "foreign type",
98                 Target::GenericParam(kind) => match kind {
99                     GenericParamKind::Type => "type parameter",
100                     GenericParamKind::Lifetime => "lifetime parameter",
101                     GenericParamKind::Const => "const parameter",
102                 },
103                 Target::MacroDef => "macro def",
104                 Target::Param => "function param",
105             }
106         )
107     }
108 }
109
110 impl Target {
111     pub fn from_item(item: &Item<'_>) -> Target {
112         match item.kind {
113             ItemKind::ExternCrate(..) => Target::ExternCrate,
114             ItemKind::Use(..) => Target::Use,
115             ItemKind::Static(..) => Target::Static,
116             ItemKind::Const(..) => Target::Const,
117             ItemKind::Fn(..) => Target::Fn,
118             ItemKind::Macro(..) => Target::MacroDef,
119             ItemKind::Mod(..) => Target::Mod,
120             ItemKind::ForeignMod { .. } => Target::ForeignMod,
121             ItemKind::GlobalAsm(..) => Target::GlobalAsm,
122             ItemKind::TyAlias(..) => Target::TyAlias,
123             ItemKind::OpaqueTy(..) => Target::OpaqueTy,
124             ItemKind::Enum(..) => Target::Enum,
125             ItemKind::Struct(..) => Target::Struct,
126             ItemKind::Union(..) => Target::Union,
127             ItemKind::Trait(..) => Target::Trait,
128             ItemKind::TraitAlias(..) => Target::TraitAlias,
129             ItemKind::Impl { .. } => Target::Impl,
130         }
131     }
132
133     pub fn from_trait_item(trait_item: &TraitItem<'_>) -> Target {
134         match trait_item.kind {
135             TraitItemKind::Const(..) => Target::AssocConst,
136             TraitItemKind::Fn(_, hir::TraitFn::Required(_)) => {
137                 Target::Method(MethodKind::Trait { body: false })
138             }
139             TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => {
140                 Target::Method(MethodKind::Trait { body: true })
141             }
142             TraitItemKind::Type(..) => Target::AssocTy,
143         }
144     }
145
146     pub fn from_foreign_item(foreign_item: &hir::ForeignItem<'_>) -> Target {
147         match foreign_item.kind {
148             hir::ForeignItemKind::Fn(..) => Target::ForeignFn,
149             hir::ForeignItemKind::Static(..) => Target::ForeignStatic,
150             hir::ForeignItemKind::Type => Target::ForeignTy,
151         }
152     }
153
154     pub fn from_generic_param(generic_param: &hir::GenericParam<'_>) -> Target {
155         match generic_param.kind {
156             hir::GenericParamKind::Type { .. } => Target::GenericParam(GenericParamKind::Type),
157             hir::GenericParamKind::Lifetime { .. } => {
158                 Target::GenericParam(GenericParamKind::Lifetime)
159             }
160             hir::GenericParamKind::Const { .. } => Target::GenericParam(GenericParamKind::Const),
161         }
162     }
163 }