Skip to content

Commit c1f74b2

Browse files
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 both `singular_name` and `plural_name` for more precision on the form of the name
1 parent 915a766 commit c1f74b2

File tree

2 files changed

+141
-11
lines changed

2 files changed

+141
-11
lines changed

compiler/rustc_hir/src/target.rs

Lines changed: 140 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,33 @@
66
77
use std::fmt::{self, Display};
88

9+
use rustc_ast::visit::AssocCtxt;
10+
use rustc_ast::{AssocItemKind, ForeignItemKind, ast};
11+
912
use crate::def::DefKind;
1013
use crate::{Item, ItemKind, TraitItem, TraitItemKind, hir};
1114

12-
#[derive(Copy, Clone, PartialEq, Debug)]
15+
#[derive(Copy, Clone, PartialEq, Debug, Eq)]
1316
pub enum GenericParamKind {
1417
Type,
1518
Lifetime,
1619
Const,
1720
}
1821

19-
#[derive(Copy, Clone, PartialEq, Debug)]
22+
#[derive(Copy, Clone, PartialEq, Debug, Eq)]
2023
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
2232
Inherent,
2333
}
2434

25-
#[derive(Copy, Clone, PartialEq, Debug)]
35+
#[derive(Copy, Clone, PartialEq, Debug, Eq)]
2636
pub enum Target {
2737
ExternCrate,
2838
Use,
@@ -57,6 +67,9 @@ pub enum Target {
5767
PatField,
5868
ExprField,
5969
WherePredicate,
70+
MacroCall,
71+
Crate,
72+
Delegation,
6073
}
6174

6275
impl Display for Target {
@@ -98,7 +111,10 @@ impl Target {
98111
| Target::Param
99112
| Target::PatField
100113
| Target::ExprField
101-
| Target::WherePredicate => false,
114+
| Target::MacroCall
115+
| Target::Crate
116+
| Target::WherePredicate
117+
| Target::Delegation => false,
102118
}
103119
}
104120

@@ -146,6 +162,39 @@ impl Target {
146162
}
147163
}
148164

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+
149198
pub fn from_trait_item(trait_item: &TraitItem<'_>) -> Target {
150199
match trait_item.kind {
151200
TraitItemKind::Const(..) => Target::AssocConst,
@@ -183,12 +232,39 @@ impl Target {
183232
}
184233
}
185234

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+
186262
pub fn name(self) -> &'static str {
187263
match self {
188264
Target::ExternCrate => "extern crate",
189265
Target::Use => "use",
190-
Target::Static => "static item",
191-
Target::Const => "constant item",
266+
Target::Static => "static",
267+
Target::Const => "constant",
192268
Target::Fn => "function",
193269
Target::Closure => "closure",
194270
Target::Mod => "module",
@@ -202,8 +278,7 @@ impl Target {
202278
Target::Union => "union",
203279
Target::Trait => "trait",
204280
Target::TraitAlias => "trait alias",
205-
Target::Impl { of_trait: false } => "inherent implementation block",
206-
Target::Impl { of_trait: true } => "trait implementation block",
281+
Target::Impl { .. } => "implementation block",
207282
Target::Expression => "expression",
208283
Target::Statement => "statement",
209284
Target::Arm => "match arm",
@@ -212,12 +287,13 @@ impl Target {
212287
MethodKind::Inherent => "inherent method",
213288
MethodKind::Trait { body: false } => "required trait method",
214289
MethodKind::Trait { body: true } => "provided trait method",
290+
MethodKind::TraitImpl => "trait method in an impl block",
215291
},
216292
Target::AssocTy => "associated type",
217293
Target::ForeignFn => "foreign function",
218294
Target::ForeignStatic => "foreign static item",
219295
Target::ForeignTy => "foreign type",
220-
Target::GenericParam { kind, has_default: _ } => match kind {
296+
Target::GenericParam { kind, .. } => match kind {
221297
GenericParamKind::Type => "type parameter",
222298
GenericParamKind::Lifetime => "lifetime parameter",
223299
GenericParamKind::Const => "const parameter",
@@ -227,6 +303,60 @@ impl Target {
227303
Target::PatField => "pattern field",
228304
Target::ExprField => "struct field",
229305
Target::WherePredicate => "where predicate",
306+
Target::MacroCall => "macro call",
307+
Target::Crate => "crate",
308+
Target::Delegation => "delegation",
309+
}
310+
}
311+
312+
pub fn plural_name(self) -> &'static str {
313+
match self {
314+
Target::ExternCrate => "extern crates",
315+
Target::Use => "use statements",
316+
Target::Static => "statics",
317+
Target::Const => "constants",
318+
Target::Fn => "functions",
319+
Target::Closure => "closures",
320+
Target::Mod => "modules",
321+
Target::ForeignMod => "foreign modules",
322+
Target::GlobalAsm => "global asms",
323+
Target::TyAlias => "type aliases",
324+
Target::Enum => "enums",
325+
Target::Variant => "enum variants",
326+
Target::Struct => "structs",
327+
Target::Field => "struct fields",
328+
Target::Union => "unions",
329+
Target::Trait => "traits",
330+
Target::TraitAlias => "trait aliases",
331+
Target::Impl { of_trait: false } => "inherent impl blocks",
332+
Target::Impl { of_trait: true } => "trait impl blocks",
333+
Target::Expression => "expressions",
334+
Target::Statement => "statements",
335+
Target::Arm => "match arms",
336+
Target::AssocConst => "associated consts",
337+
Target::Method(kind) => match kind {
338+
MethodKind::Inherent => "inherent methods",
339+
MethodKind::Trait { body: false } => "required trait methods",
340+
MethodKind::Trait { body: true } => "provided trait methods",
341+
MethodKind::TraitImpl => "trait methods in impl blocks",
342+
},
343+
Target::AssocTy => "associated types",
344+
Target::ForeignFn => "foreign functions",
345+
Target::ForeignStatic => "foreign statics",
346+
Target::ForeignTy => "foreign types",
347+
Target::GenericParam { kind, has_default: _ } => match kind {
348+
GenericParamKind::Type => "type parameters",
349+
GenericParamKind::Lifetime => "lifetime parameters",
350+
GenericParamKind::Const => "const parameters",
351+
},
352+
Target::MacroDef => "macro defs",
353+
Target::Param => "function params",
354+
Target::PatField => "pattern fields",
355+
Target::ExprField => "struct fields",
356+
Target::WherePredicate => "where predicates",
357+
Target::MacroCall => "macro calls",
358+
Target::Crate => "crates",
359+
Target::Delegation => "delegations",
230360
}
231361
}
232362
}

compiler/rustc_passes/src/lang_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ impl<'ast, 'tcx> visit::Visitor<'ast> for LanguageItemCollector<'ast, 'tcx> {
329329
match &self.parent_item.unwrap().kind {
330330
ast::ItemKind::Impl(i) => {
331331
if i.of_trait.is_some() {
332-
Target::Method(MethodKind::Trait { body })
332+
Target::Method(MethodKind::TraitImpl)
333333
} else {
334334
Target::Method(MethodKind::Inherent)
335335
}

0 commit comments

Comments
 (0)