Skip to content

Commit 0c90156

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 0c90156

File tree

2 files changed

+193
-12
lines changed

2 files changed

+193
-12
lines changed

compiler/rustc_hir/src/target.rs

Lines changed: 192 additions & 11 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,11 +67,14 @@ pub enum Target {
5767
PatField,
5868
ExprField,
5969
WherePredicate,
70+
MacroCall,
71+
Crate,
72+
Delegation,
6073
}
6174

6275
impl Display for Target {
6376
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64-
write!(f, "{}", Self::name(*self))
77+
write!(f, "{}", Self::plural_name(*self))
6578
}
6679
}
6780

@@ -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,111 @@ 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 singular_name(self) -> &'static str {
313+
match self {
314+
Target::ExternCrate => "an extern crate",
315+
Target::Use => "a use statement",
316+
Target::Static => "a static",
317+
Target::Const => "a constant",
318+
Target::Fn => "a function",
319+
Target::Closure => "a closure",
320+
Target::Mod => "a module",
321+
Target::ForeignMod => "a foreign module",
322+
Target::GlobalAsm => "a global asm",
323+
Target::TyAlias => "a type alias",
324+
Target::Enum => "an enum",
325+
Target::Variant => "an enum variant",
326+
Target::Struct => "a struct",
327+
Target::Field => "a struct field",
328+
Target::Union => "a union",
329+
Target::Trait => "a trait",
330+
Target::TraitAlias => "a trait alias",
331+
Target::Impl { of_trait: false } => "an inherent impl block",
332+
Target::Impl { of_trait: true } => "a trait impl block",
333+
Target::Expression => "an expression",
334+
Target::Statement => "a statement",
335+
Target::Arm => "a match arm",
336+
Target::AssocConst => "an associated const",
337+
Target::Method(kind) => match kind {
338+
MethodKind::Inherent => "an inherent method",
339+
MethodKind::Trait { body: false } => "a required trait method",
340+
MethodKind::Trait { body: true } => "a provided trait method",
341+
MethodKind::TraitImpl => "a trait method in an impl block",
342+
},
343+
Target::AssocTy => "an associated type",
344+
Target::ForeignFn => "a foreign function",
345+
Target::ForeignStatic => "a foreign static",
346+
Target::ForeignTy => "a foreign type",
347+
Target::GenericParam { kind, has_default: _ } => match kind {
348+
GenericParamKind::Type => "a type parameter",
349+
GenericParamKind::Lifetime => "a lifetime parameter",
350+
GenericParamKind::Const => "a const parameter",
351+
},
352+
Target::MacroDef => "a macro def",
353+
Target::Param => "a function param",
354+
Target::PatField => "a pattern field",
355+
Target::ExprField => "a struct field",
356+
Target::WherePredicate => "a where predicate",
357+
Target::MacroCall => "a macro call",
358+
Target::Crate => "a crate",
359+
Target::Delegation => "a delegation",
360+
}
361+
}
362+
363+
pub fn plural_name(self) -> &'static str {
364+
match self {
365+
Target::ExternCrate => "extern crates",
366+
Target::Use => "use statements",
367+
Target::Static => "statics",
368+
Target::Const => "constants",
369+
Target::Fn => "functions",
370+
Target::Closure => "closures",
371+
Target::Mod => "modules",
372+
Target::ForeignMod => "foreign modules",
373+
Target::GlobalAsm => "global asms",
374+
Target::TyAlias => "type aliases",
375+
Target::Enum => "enums",
376+
Target::Variant => "enum variants",
377+
Target::Struct => "structs",
378+
Target::Field => "struct fields",
379+
Target::Union => "unions",
380+
Target::Trait => "traits",
381+
Target::TraitAlias => "trait aliases",
382+
Target::Impl { of_trait: false } => "inherent impl blocks",
383+
Target::Impl { of_trait: true } => "trait impl blocks",
384+
Target::Expression => "expressions",
385+
Target::Statement => "statements",
386+
Target::Arm => "match arms",
387+
Target::AssocConst => "associated consts",
388+
Target::Method(kind) => match kind {
389+
MethodKind::Inherent => "inherent methods",
390+
MethodKind::Trait { body: false } => "required trait methods",
391+
MethodKind::Trait { body: true } => "provided trait methods",
392+
MethodKind::TraitImpl => "trait methods in impl blocks",
393+
},
394+
Target::AssocTy => "associated types",
395+
Target::ForeignFn => "foreign functions",
396+
Target::ForeignStatic => "foreign statics",
397+
Target::ForeignTy => "foreign types",
398+
Target::GenericParam { kind, has_default: _ } => match kind {
399+
GenericParamKind::Type => "type parameters",
400+
GenericParamKind::Lifetime => "lifetime parameters",
401+
GenericParamKind::Const => "const parameters",
402+
},
403+
Target::MacroDef => "macro defs",
404+
Target::Param => "function params",
405+
Target::PatField => "pattern fields",
406+
Target::ExprField => "struct fields",
407+
Target::WherePredicate => "where predicates",
408+
Target::MacroCall => "macro calls",
409+
Target::Crate => "crates",
410+
Target::Delegation => "delegations",
230411
}
231412
}
232413
}

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)