]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir/src/target.rs
Rollup merge of #81687 - WaffleLapkin:split_at_spare, r=KodrAus
[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 }
58
59 impl Display for Target {
60     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61         write!(
62             f,
63             "{}",
64             match *self {
65                 Target::ExternCrate => "extern crate",
66                 Target::Use => "use",
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",
97                 },
98                 Target::MacroDef => "macro def",
99             }
100         )
101     }
102 }
103
104 impl Target {
105     pub fn from_item(item: &Item<'_>) -> Target {
106         match item.kind {
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,
123         }
124     }
125
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 })
131             }
132             TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => {
133                 Target::Method(MethodKind::Trait { body: true })
134             }
135             TraitItemKind::Type(..) => Target::AssocTy,
136         }
137     }
138
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,
144         }
145     }
146
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)
152             }
153             hir::GenericParamKind::Const { .. } => Target::GenericParam(GenericParamKind::Const),
154         }
155     }
156 }