Skip to content

Commit b176e52

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 ca77504 commit b176e52

File tree

2 files changed

+178
-47
lines changed

2 files changed

+178
-47
lines changed

compiler/rustc_hir/src/target.rs

Lines changed: 176 additions & 45 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,50 +232,132 @@ impl Target {
183232
}
184233
}
185234

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 {
187314
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",
211338
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",
215343
},
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",
220348
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",
224352
},
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",
230361
}
231362
}
232363
}

compiler/rustc_passes/src/lang_items.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl<'ast, 'tcx> LanguageItemCollector<'ast, 'tcx> {
226226
span: attr_span,
227227
generics_span: generics.span,
228228
name: name.as_str(),
229-
kind: target.name(),
229+
kind: target.singular_name(),
230230
num,
231231
actual_num,
232232
at_least,
@@ -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)