|
6 | 6 |
|
7 | 7 | use std::fmt::{self, Display};
|
8 | 8 |
|
| 9 | +use rustc_ast::visit::AssocCtxt; |
| 10 | +use rustc_ast::{AssocItemKind, ForeignItemKind, ast}; |
| 11 | + |
9 | 12 | use crate::def::DefKind;
|
10 | 13 | use crate::{Item, ItemKind, TraitItem, TraitItemKind, hir};
|
11 | 14 |
|
12 |
| -#[derive(Copy, Clone, PartialEq, Debug)] |
| 15 | +#[derive(Copy, Clone, PartialEq, Debug, Eq)] |
13 | 16 | pub enum GenericParamKind {
|
14 | 17 | Type,
|
15 | 18 | Lifetime,
|
16 | 19 | Const,
|
17 | 20 | }
|
18 | 21 |
|
19 |
| -#[derive(Copy, Clone, PartialEq, Debug)] |
| 22 | +#[derive(Copy, Clone, PartialEq, Debug, Eq)] |
20 | 23 | pub enum MethodKind {
|
21 |
| - Trait { body: bool }, |
| 24 | + /// Method in a `trait Trait` block |
| 25 | + Trait { |
| 26 | + /// Whether a default is provided for this method |
| 27 | + body: bool, |
| 28 | + }, |
| 29 | + /// Method in a `impl Trait for Type` block |
| 30 | + TraitImpl, |
| 31 | + /// Method in a `impl Type` block |
22 | 32 | Inherent,
|
23 | 33 | }
|
24 | 34 |
|
25 |
| -#[derive(Copy, Clone, PartialEq, Debug)] |
| 35 | +#[derive(Copy, Clone, PartialEq, Debug, Eq)] |
26 | 36 | pub enum Target {
|
27 | 37 | ExternCrate,
|
28 | 38 | Use,
|
@@ -57,11 +67,14 @@ pub enum Target {
|
57 | 67 | PatField,
|
58 | 68 | ExprField,
|
59 | 69 | WherePredicate,
|
| 70 | + MacroCall, |
| 71 | + Crate, |
| 72 | + Delegation, |
60 | 73 | }
|
61 | 74 |
|
62 | 75 | impl Display for Target {
|
63 | 76 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
64 |
| - write!(f, "{}", Self::name(*self)) |
| 77 | + write!(f, "{}", Self::plural_name(*self)) |
65 | 78 | }
|
66 | 79 | }
|
67 | 80 |
|
@@ -98,7 +111,10 @@ impl Target {
|
98 | 111 | | Target::Param
|
99 | 112 | | Target::PatField
|
100 | 113 | | Target::ExprField
|
101 |
| - | Target::WherePredicate => false, |
| 114 | + | Target::MacroCall |
| 115 | + | Target::Crate |
| 116 | + | Target::WherePredicate |
| 117 | + | Target::Delegation => false, |
102 | 118 | }
|
103 | 119 | }
|
104 | 120 |
|
@@ -146,6 +162,39 @@ impl Target {
|
146 | 162 | }
|
147 | 163 | }
|
148 | 164 |
|
| 165 | + pub fn from_ast_item(item: &ast::Item) -> Target { |
| 166 | + match item.kind { |
| 167 | + ast::ItemKind::ExternCrate(..) => Target::ExternCrate, |
| 168 | + ast::ItemKind::Use(..) => Target::Use, |
| 169 | + ast::ItemKind::Static { .. } => Target::Static, |
| 170 | + ast::ItemKind::Const(..) => Target::Const, |
| 171 | + ast::ItemKind::Fn { .. } => Target::Fn, |
| 172 | + ast::ItemKind::Mod(..) => Target::Mod, |
| 173 | + ast::ItemKind::ForeignMod { .. } => Target::ForeignMod, |
| 174 | + ast::ItemKind::GlobalAsm { .. } => Target::GlobalAsm, |
| 175 | + ast::ItemKind::TyAlias(..) => Target::TyAlias, |
| 176 | + ast::ItemKind::Enum(..) => Target::Enum, |
| 177 | + ast::ItemKind::Struct(..) => Target::Struct, |
| 178 | + ast::ItemKind::Union(..) => Target::Union, |
| 179 | + ast::ItemKind::Trait(..) => Target::Trait, |
| 180 | + ast::ItemKind::TraitAlias(..) => Target::TraitAlias, |
| 181 | + ast::ItemKind::Impl(ref i) => Target::Impl { of_trait: i.of_trait.is_some() }, |
| 182 | + ast::ItemKind::MacCall(..) => Target::MacroCall, |
| 183 | + ast::ItemKind::MacroDef(..) => Target::MacroDef, |
| 184 | + ast::ItemKind::Delegation(..) => Target::Fn, |
| 185 | + ast::ItemKind::DelegationMac(..) => panic!("macros should be expanded"), |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + pub fn from_foreign_item_kind(kind: &ast::ForeignItemKind) -> Target { |
| 190 | + match kind { |
| 191 | + ForeignItemKind::Static(_) => Target::ForeignStatic, |
| 192 | + ForeignItemKind::Fn(_) => Target::ForeignFn, |
| 193 | + ForeignItemKind::TyAlias(_) => Target::ForeignTy, |
| 194 | + ForeignItemKind::MacCall(_) => panic!("macros should be expanded"), |
| 195 | + } |
| 196 | + } |
| 197 | + |
149 | 198 | pub fn from_trait_item(trait_item: &TraitItem<'_>) -> Target {
|
150 | 199 | match trait_item.kind {
|
151 | 200 | TraitItemKind::Const(..) => Target::AssocConst,
|
@@ -183,50 +232,132 @@ impl Target {
|
183 | 232 | }
|
184 | 233 | }
|
185 | 234 |
|
186 |
| - pub fn name(self) -> &'static str { |
| 235 | + pub fn from_assoc_item_kind(kind: &ast::AssocItemKind, assoc_ctxt: AssocCtxt) -> Target { |
| 236 | + match kind { |
| 237 | + AssocItemKind::Const(_) => Target::AssocConst, |
| 238 | + AssocItemKind::Fn(f) => Target::Method(match assoc_ctxt { |
| 239 | + AssocCtxt::Trait => MethodKind::Trait { body: f.body.is_some() }, |
| 240 | + AssocCtxt::Impl { of_trait } => { |
| 241 | + if of_trait { |
| 242 | + MethodKind::TraitImpl |
| 243 | + } else { |
| 244 | + MethodKind::Inherent |
| 245 | + } |
| 246 | + } |
| 247 | + }), |
| 248 | + AssocItemKind::Type(_) => Target::AssocTy, |
| 249 | + AssocItemKind::Delegation(_) => Target::Delegation, |
| 250 | + _ => unreachable!(), |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + pub fn from_expr(expr: &ast::Expr) -> Self { |
| 255 | + match &expr.kind { |
| 256 | + ast::ExprKind::Closure(..) | ast::ExprKind::Gen(..) => Self::Closure, |
| 257 | + ast::ExprKind::Paren(e) => Self::from_expr(&e), |
| 258 | + _ => Self::Expression, |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + pub fn singular_name(self) -> &'static str { |
| 263 | + match self { |
| 264 | + Target::ExternCrate => "an extern crate", |
| 265 | + Target::Use => "a use statement", |
| 266 | + Target::Static => "a static", |
| 267 | + Target::Const => "a constant", |
| 268 | + Target::Fn => "a function", |
| 269 | + Target::Closure => "a closure", |
| 270 | + Target::Mod => "a module", |
| 271 | + Target::ForeignMod => "a foreign module", |
| 272 | + Target::GlobalAsm => "a global asm", |
| 273 | + Target::TyAlias => "a type alias", |
| 274 | + Target::Enum => "an enum", |
| 275 | + Target::Variant => "an enum variant", |
| 276 | + Target::Struct => "a struct", |
| 277 | + Target::Field => "a struct field", |
| 278 | + Target::Union => "a union", |
| 279 | + Target::Trait => "a trait", |
| 280 | + Target::TraitAlias => "a trait alias", |
| 281 | + Target::Impl { of_trait: false } => "an inherent impl block", |
| 282 | + Target::Impl { of_trait: true } => "a trait impl block", |
| 283 | + Target::Expression => "an expression", |
| 284 | + Target::Statement => "a statement", |
| 285 | + Target::Arm => "a match arm", |
| 286 | + Target::AssocConst => "an associated const", |
| 287 | + Target::Method(kind) => match kind { |
| 288 | + MethodKind::Inherent => "an inherent method", |
| 289 | + MethodKind::Trait { body: false } => "a required trait method", |
| 290 | + MethodKind::Trait { body: true } => "a provided trait method", |
| 291 | + MethodKind::TraitImpl => "a trait method in an impl block", |
| 292 | + }, |
| 293 | + Target::AssocTy => "an associated type", |
| 294 | + Target::ForeignFn => "a foreign function", |
| 295 | + Target::ForeignStatic => "a foreign static", |
| 296 | + Target::ForeignTy => "a foreign type", |
| 297 | + Target::GenericParam { kind, has_default: _ } => match kind { |
| 298 | + GenericParamKind::Type => "a type parameter", |
| 299 | + GenericParamKind::Lifetime => "a lifetime parameter", |
| 300 | + GenericParamKind::Const => "a const parameter", |
| 301 | + }, |
| 302 | + Target::MacroDef => "a macro def", |
| 303 | + Target::Param => "a function param", |
| 304 | + Target::PatField => "a pattern field", |
| 305 | + Target::ExprField => "a struct field", |
| 306 | + Target::WherePredicate => "a where predicate", |
| 307 | + Target::MacroCall => "a macro call", |
| 308 | + Target::Crate => "a crate", |
| 309 | + Target::Delegation => "a delegation", |
| 310 | + } |
| 311 | + } |
| 312 | + |
| 313 | + pub fn plural_name(self) -> &'static str { |
187 | 314 | match self {
|
188 |
| - Target::ExternCrate => "extern crate", |
189 |
| - Target::Use => "use", |
190 |
| - Target::Static => "static item", |
191 |
| - Target::Const => "constant item", |
192 |
| - Target::Fn => "function", |
193 |
| - Target::Closure => "closure", |
194 |
| - Target::Mod => "module", |
195 |
| - Target::ForeignMod => "foreign module", |
196 |
| - Target::GlobalAsm => "global asm", |
197 |
| - Target::TyAlias => "type alias", |
198 |
| - Target::Enum => "enum", |
199 |
| - Target::Variant => "enum variant", |
200 |
| - Target::Struct => "struct", |
201 |
| - Target::Field => "struct field", |
202 |
| - Target::Union => "union", |
203 |
| - Target::Trait => "trait", |
204 |
| - Target::TraitAlias => "trait alias", |
205 |
| - Target::Impl { of_trait: false } => "inherent implementation block", |
206 |
| - Target::Impl { of_trait: true } => "trait implementation block", |
207 |
| - Target::Expression => "expression", |
208 |
| - Target::Statement => "statement", |
209 |
| - Target::Arm => "match arm", |
210 |
| - Target::AssocConst => "associated const", |
| 315 | + Target::ExternCrate => "extern crates", |
| 316 | + Target::Use => "use statements", |
| 317 | + Target::Static => "statics", |
| 318 | + Target::Const => "constants", |
| 319 | + Target::Fn => "functions", |
| 320 | + Target::Closure => "closures", |
| 321 | + Target::Mod => "modules", |
| 322 | + Target::ForeignMod => "foreign modules", |
| 323 | + Target::GlobalAsm => "global asms", |
| 324 | + Target::TyAlias => "type aliases", |
| 325 | + Target::Enum => "enums", |
| 326 | + Target::Variant => "enum variants", |
| 327 | + Target::Struct => "structs", |
| 328 | + Target::Field => "struct fields", |
| 329 | + Target::Union => "unions", |
| 330 | + Target::Trait => "traits", |
| 331 | + Target::TraitAlias => "trait aliases", |
| 332 | + Target::Impl { of_trait: false } => "inherent impl blocks", |
| 333 | + Target::Impl { of_trait: true } => "trait impl blocks", |
| 334 | + Target::Expression => "expressions", |
| 335 | + Target::Statement => "statements", |
| 336 | + Target::Arm => "match arms", |
| 337 | + Target::AssocConst => "associated consts", |
211 | 338 | Target::Method(kind) => match kind {
|
212 |
| - MethodKind::Inherent => "inherent method", |
213 |
| - MethodKind::Trait { body: false } => "required trait method", |
214 |
| - MethodKind::Trait { body: true } => "provided trait method", |
| 339 | + MethodKind::Inherent => "inherent methods", |
| 340 | + MethodKind::Trait { body: false } => "required trait methods", |
| 341 | + MethodKind::Trait { body: true } => "provided trait methods", |
| 342 | + MethodKind::TraitImpl => "trait methods in impl blocks", |
215 | 343 | },
|
216 |
| - Target::AssocTy => "associated type", |
217 |
| - Target::ForeignFn => "foreign function", |
218 |
| - Target::ForeignStatic => "foreign static item", |
219 |
| - Target::ForeignTy => "foreign type", |
| 344 | + Target::AssocTy => "associated types", |
| 345 | + Target::ForeignFn => "foreign functions", |
| 346 | + Target::ForeignStatic => "foreign statics", |
| 347 | + Target::ForeignTy => "foreign types", |
220 | 348 | Target::GenericParam { kind, has_default: _ } => match kind {
|
221 |
| - GenericParamKind::Type => "type parameter", |
222 |
| - GenericParamKind::Lifetime => "lifetime parameter", |
223 |
| - GenericParamKind::Const => "const parameter", |
| 349 | + GenericParamKind::Type => "type parameters", |
| 350 | + GenericParamKind::Lifetime => "lifetime parameters", |
| 351 | + GenericParamKind::Const => "const parameters", |
224 | 352 | },
|
225 |
| - Target::MacroDef => "macro def", |
226 |
| - Target::Param => "function param", |
227 |
| - Target::PatField => "pattern field", |
228 |
| - Target::ExprField => "struct field", |
229 |
| - Target::WherePredicate => "where predicate", |
| 353 | + Target::MacroDef => "macro defs", |
| 354 | + Target::Param => "function params", |
| 355 | + Target::PatField => "pattern fields", |
| 356 | + Target::ExprField => "struct fields", |
| 357 | + Target::WherePredicate => "where predicates", |
| 358 | + Target::MacroCall => "macro calls", |
| 359 | + Target::Crate => "crates", |
| 360 | + Target::Delegation => "delegations", |
230 | 361 | }
|
231 | 362 | }
|
232 | 363 | }
|
0 commit comments