Improved Target type

- Added a few more variants which are needed for various attributes
- Previously a trait method with default block had the same target representation as a method in a `impl trait for` block, this has been changed (See `MethodKind`)
- Added `plural_name` for more precision on the form of the name
This commit is contained in:
Jonathan Brouwer
2025-08-09 20:31:06 +02:00
parent 30017c36d6
commit 106731f714
2 changed files with 143 additions and 11 deletions

View File

@@ -6,23 +6,34 @@
use std::fmt::{self, Display};
use rustc_ast::visit::AssocCtxt;
use rustc_ast::{AssocItemKind, ForeignItemKind, ast};
use rustc_macros::HashStable_Generic;
use crate::def::DefKind;
use crate::{Item, ItemKind, TraitItem, TraitItemKind, hir};
#[derive(Copy, Clone, PartialEq, Debug)]
#[derive(Copy, Clone, PartialEq, Debug, Eq, HashStable_Generic)]
pub enum GenericParamKind {
Type,
Lifetime,
Const,
}
#[derive(Copy, Clone, PartialEq, Debug)]
#[derive(Copy, Clone, PartialEq, Debug, Eq, HashStable_Generic)]
pub enum MethodKind {
Trait { body: bool },
/// Method in a `trait Trait` block
Trait {
/// Whether a default is provided for this method
body: bool,
},
/// Method in a `impl Trait for Type` block
TraitImpl,
/// Method in a `impl Type` block
Inherent,
}
#[derive(Copy, Clone, PartialEq, Debug)]
#[derive(Copy, Clone, PartialEq, Debug, Eq, HashStable_Generic)]
pub enum Target {
ExternCrate,
Use,
@@ -57,6 +68,9 @@ pub enum Target {
PatField,
ExprField,
WherePredicate,
MacroCall,
Crate,
Delegation { mac: bool },
}
impl Display for Target {
@@ -98,7 +112,10 @@ impl Target {
| Target::Param
| Target::PatField
| Target::ExprField
| Target::WherePredicate => false,
| Target::MacroCall
| Target::Crate
| Target::WherePredicate
| Target::Delegation { .. } => false,
}
}
@@ -146,6 +163,39 @@ impl Target {
}
}
pub fn from_ast_item(item: &ast::Item) -> Target {
match item.kind {
ast::ItemKind::ExternCrate(..) => Target::ExternCrate,
ast::ItemKind::Use(..) => Target::Use,
ast::ItemKind::Static { .. } => Target::Static,
ast::ItemKind::Const(..) => Target::Const,
ast::ItemKind::Fn { .. } => Target::Fn,
ast::ItemKind::Mod(..) => Target::Mod,
ast::ItemKind::ForeignMod { .. } => Target::ForeignMod,
ast::ItemKind::GlobalAsm { .. } => Target::GlobalAsm,
ast::ItemKind::TyAlias(..) => Target::TyAlias,
ast::ItemKind::Enum(..) => Target::Enum,
ast::ItemKind::Struct(..) => Target::Struct,
ast::ItemKind::Union(..) => Target::Union,
ast::ItemKind::Trait(..) => Target::Trait,
ast::ItemKind::TraitAlias(..) => Target::TraitAlias,
ast::ItemKind::Impl(ref i) => Target::Impl { of_trait: i.of_trait.is_some() },
ast::ItemKind::MacCall(..) => Target::MacroCall,
ast::ItemKind::MacroDef(..) => Target::MacroDef,
ast::ItemKind::Delegation(..) => Target::Delegation { mac: false },
ast::ItemKind::DelegationMac(..) => Target::Delegation { mac: true },
}
}
pub fn from_foreign_item_kind(kind: &ast::ForeignItemKind) -> Target {
match kind {
ForeignItemKind::Static(_) => Target::ForeignStatic,
ForeignItemKind::Fn(_) => Target::ForeignFn,
ForeignItemKind::TyAlias(_) => Target::ForeignTy,
ForeignItemKind::MacCall(_) => Target::MacroCall,
}
}
pub fn from_trait_item(trait_item: &TraitItem<'_>) -> Target {
match trait_item.kind {
TraitItemKind::Const(..) => Target::AssocConst,
@@ -183,12 +233,40 @@ impl Target {
}
}
pub fn from_assoc_item_kind(kind: &ast::AssocItemKind, assoc_ctxt: AssocCtxt) -> Target {
match kind {
AssocItemKind::Const(_) => Target::AssocConst,
AssocItemKind::Fn(f) => Target::Method(match assoc_ctxt {
AssocCtxt::Trait => MethodKind::Trait { body: f.body.is_some() },
AssocCtxt::Impl { of_trait } => {
if of_trait {
MethodKind::TraitImpl
} else {
MethodKind::Inherent
}
}
}),
AssocItemKind::Type(_) => Target::AssocTy,
AssocItemKind::Delegation(_) => Target::Delegation { mac: false },
AssocItemKind::DelegationMac(_) => Target::Delegation { mac: true },
AssocItemKind::MacCall(_) => Target::MacroCall,
}
}
pub fn from_expr(expr: &ast::Expr) -> Self {
match &expr.kind {
ast::ExprKind::Closure(..) | ast::ExprKind::Gen(..) => Self::Closure,
ast::ExprKind::Paren(e) => Self::from_expr(&e),
_ => Self::Expression,
}
}
pub fn name(self) -> &'static str {
match self {
Target::ExternCrate => "extern crate",
Target::Use => "use",
Target::Static => "static item",
Target::Const => "constant item",
Target::Static => "static",
Target::Const => "constant",
Target::Fn => "function",
Target::Closure => "closure",
Target::Mod => "module",
@@ -202,8 +280,7 @@ impl Target {
Target::Union => "union",
Target::Trait => "trait",
Target::TraitAlias => "trait alias",
Target::Impl { of_trait: false } => "inherent implementation block",
Target::Impl { of_trait: true } => "trait implementation block",
Target::Impl { .. } => "implementation block",
Target::Expression => "expression",
Target::Statement => "statement",
Target::Arm => "match arm",
@@ -212,12 +289,13 @@ impl Target {
MethodKind::Inherent => "inherent method",
MethodKind::Trait { body: false } => "required trait method",
MethodKind::Trait { body: true } => "provided trait method",
MethodKind::TraitImpl => "trait method in an impl block",
},
Target::AssocTy => "associated type",
Target::ForeignFn => "foreign function",
Target::ForeignStatic => "foreign static item",
Target::ForeignTy => "foreign type",
Target::GenericParam { kind, has_default: _ } => match kind {
Target::GenericParam { kind, .. } => match kind {
GenericParamKind::Type => "type parameter",
GenericParamKind::Lifetime => "lifetime parameter",
GenericParamKind::Const => "const parameter",
@@ -227,6 +305,60 @@ impl Target {
Target::PatField => "pattern field",
Target::ExprField => "struct field",
Target::WherePredicate => "where predicate",
Target::MacroCall => "macro call",
Target::Crate => "crate",
Target::Delegation { .. } => "delegation",
}
}
pub fn plural_name(self) -> &'static str {
match self {
Target::ExternCrate => "extern crates",
Target::Use => "use statements",
Target::Static => "statics",
Target::Const => "constants",
Target::Fn => "functions",
Target::Closure => "closures",
Target::Mod => "modules",
Target::ForeignMod => "foreign modules",
Target::GlobalAsm => "global asms",
Target::TyAlias => "type aliases",
Target::Enum => "enums",
Target::Variant => "enum variants",
Target::Struct => "structs",
Target::Field => "struct fields",
Target::Union => "unions",
Target::Trait => "traits",
Target::TraitAlias => "trait aliases",
Target::Impl { of_trait: false } => "inherent impl blocks",
Target::Impl { of_trait: true } => "trait impl blocks",
Target::Expression => "expressions",
Target::Statement => "statements",
Target::Arm => "match arms",
Target::AssocConst => "associated consts",
Target::Method(kind) => match kind {
MethodKind::Inherent => "inherent methods",
MethodKind::Trait { body: false } => "required trait methods",
MethodKind::Trait { body: true } => "provided trait methods",
MethodKind::TraitImpl => "trait methods in impl blocks",
},
Target::AssocTy => "associated types",
Target::ForeignFn => "foreign functions",
Target::ForeignStatic => "foreign statics",
Target::ForeignTy => "foreign types",
Target::GenericParam { kind, has_default: _ } => match kind {
GenericParamKind::Type => "type parameters",
GenericParamKind::Lifetime => "lifetime parameters",
GenericParamKind::Const => "const parameters",
},
Target::MacroDef => "macro defs",
Target::Param => "function params",
Target::PatField => "pattern fields",
Target::ExprField => "struct fields",
Target::WherePredicate => "where predicates",
Target::MacroCall => "macro calls",
Target::Crate => "crates",
Target::Delegation { .. } => "delegations",
}
}
}