Skip to content

Commit e51cbc8

Browse files
authored
Rollup merge of #70043 - mark-i-m:def-kind-more, r=eddyb
Add all remaining `DefKind`s. r? @eddyb or @Centril ~~I'm not sure if this is what you were thinking of. There are also a few places where I'm not sure what the correct choice is because I don't fully understand the meaning of some variants.~~ ~~In general, it feels a bit odd to add some of these as `DefKind`s (e.g. `Arm`) because they don't feel like definitions. Are there things that it makes sense not to add?~~
2 parents 0862458 + 258ebfe commit e51cbc8

File tree

40 files changed

+234
-239
lines changed

40 files changed

+234
-239
lines changed

src/librustc_hir/def.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ pub enum DefKind {
7777

7878
// Macro namespace
7979
Macro(MacroKind),
80+
81+
// Not namespaced (or they are, but we don't treat them so)
82+
ExternCrate,
83+
Use,
84+
ForeignMod,
85+
AnonConst,
86+
Field,
87+
LifetimeParam,
88+
GlobalAsm,
89+
Impl,
90+
Closure,
91+
Generator,
8092
}
8193

8294
impl DefKind {
@@ -113,6 +125,16 @@ impl DefKind {
113125
DefKind::TyParam => "type parameter",
114126
DefKind::ConstParam => "const parameter",
115127
DefKind::Macro(macro_kind) => macro_kind.descr(),
128+
DefKind::LifetimeParam => "lifetime parameter",
129+
DefKind::Use => "import",
130+
DefKind::ForeignMod => "foreign module",
131+
DefKind::AnonConst => "constant expression",
132+
DefKind::Field => "field",
133+
DefKind::Impl => "implementation",
134+
DefKind::Closure => "closure",
135+
DefKind::Generator => "generator",
136+
DefKind::ExternCrate => "extern crate",
137+
DefKind::GlobalAsm => "global assembly block",
116138
}
117139
}
118140

@@ -124,7 +146,10 @@ impl DefKind {
124146
| DefKind::AssocOpaqueTy
125147
| DefKind::AssocFn
126148
| DefKind::Enum
127-
| DefKind::OpaqueTy => "an",
149+
| DefKind::OpaqueTy
150+
| DefKind::Impl
151+
| DefKind::Use
152+
| DefKind::ExternCrate => "an",
128153
DefKind::Macro(macro_kind) => macro_kind.article(),
129154
_ => "a",
130155
}
@@ -155,6 +180,18 @@ impl DefKind {
155180
| DefKind::AssocConst => ns == Namespace::ValueNS,
156181

157182
DefKind::Macro(..) => ns == Namespace::MacroNS,
183+
184+
// Not namespaced.
185+
DefKind::AnonConst
186+
| DefKind::Field
187+
| DefKind::LifetimeParam
188+
| DefKind::ExternCrate
189+
| DefKind::Closure
190+
| DefKind::Generator
191+
| DefKind::Use
192+
| DefKind::ForeignMod
193+
| DefKind::GlobalAsm
194+
| DefKind::Impl => false,
158195
}
159196
}
160197
}

src/librustc_hir/hir.rs

-31
Original file line numberDiff line numberDiff line change
@@ -2452,27 +2452,6 @@ pub enum ItemKind<'hir> {
24522452
}
24532453

24542454
impl ItemKind<'_> {
2455-
pub fn descr(&self) -> &str {
2456-
match *self {
2457-
ItemKind::ExternCrate(..) => "extern crate",
2458-
ItemKind::Use(..) => "`use` import",
2459-
ItemKind::Static(..) => "static item",
2460-
ItemKind::Const(..) => "constant item",
2461-
ItemKind::Fn(..) => "function",
2462-
ItemKind::Mod(..) => "module",
2463-
ItemKind::ForeignMod(..) => "extern block",
2464-
ItemKind::GlobalAsm(..) => "global asm item",
2465-
ItemKind::TyAlias(..) => "type alias",
2466-
ItemKind::OpaqueTy(..) => "opaque type",
2467-
ItemKind::Enum(..) => "enum",
2468-
ItemKind::Struct(..) => "struct",
2469-
ItemKind::Union(..) => "union",
2470-
ItemKind::Trait(..) => "trait",
2471-
ItemKind::TraitAlias(..) => "trait alias",
2472-
ItemKind::Impl { .. } => "implementation",
2473-
}
2474-
}
2475-
24762455
pub fn generics(&self) -> Option<&Generics<'_>> {
24772456
Some(match *self {
24782457
ItemKind::Fn(_, ref generics, _)
@@ -2551,16 +2530,6 @@ pub enum ForeignItemKind<'hir> {
25512530
Type,
25522531
}
25532532

2554-
impl ForeignItemKind<'hir> {
2555-
pub fn descriptive_variant(&self) -> &str {
2556-
match *self {
2557-
ForeignItemKind::Fn(..) => "foreign function",
2558-
ForeignItemKind::Static(..) => "foreign static item",
2559-
ForeignItemKind::Type => "foreign type",
2560-
}
2561-
}
2562-
}
2563-
25642533
/// A variable captured by a closure.
25652534
#[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable_Generic)]
25662535
pub struct Upvar {

src/librustc_infer/infer/error_reporting/need_type_info.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
207207
.get_opt_name()
208208
.map(|parent_symbol| parent_symbol.to_string());
209209

210-
let type_parent_desc = self
211-
.tcx
212-
.def_kind(parent_def_id)
213-
.map(|parent_def_kind| parent_def_kind.descr(parent_def_id));
214-
215-
(parent_name, type_parent_desc)
210+
(parent_name, Some(self.tcx.def_kind(parent_def_id).descr(parent_def_id)))
216211
} else {
217212
(None, None)
218213
};

src/librustc_metadata/rmeta/decoder.rs

+26-30
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,8 @@ impl MetadataBlob {
562562
}
563563

564564
impl EntryKind {
565-
fn def_kind(&self) -> Option<DefKind> {
566-
Some(match *self {
565+
fn def_kind(&self) -> DefKind {
566+
match *self {
567567
EntryKind::Const(..) => DefKind::Const,
568568
EntryKind::AssocConst(..) => DefKind::AssocConst,
569569
EntryKind::ImmStatic
@@ -587,14 +587,13 @@ impl EntryKind {
587587
EntryKind::Enum(..) => DefKind::Enum,
588588
EntryKind::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
589589
EntryKind::ForeignType => DefKind::ForeignTy,
590-
591-
EntryKind::ForeignMod
592-
| EntryKind::GlobalAsm
593-
| EntryKind::Impl(_)
594-
| EntryKind::Field
595-
| EntryKind::Generator(_)
596-
| EntryKind::Closure => return None,
597-
})
590+
EntryKind::Impl(_) => DefKind::Impl,
591+
EntryKind::Closure => DefKind::Closure,
592+
EntryKind::ForeignMod => DefKind::ForeignMod,
593+
EntryKind::GlobalAsm => DefKind::GlobalAsm,
594+
EntryKind::Field => DefKind::Field,
595+
EntryKind::Generator(_) => DefKind::Generator,
596+
}
598597
}
599598
}
600599

@@ -679,11 +678,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
679678
}
680679
}
681680

682-
fn def_kind(&self, index: DefIndex) -> Option<DefKind> {
681+
fn def_kind(&self, index: DefIndex) -> DefKind {
683682
if !self.is_proc_macro(index) {
684683
self.kind(index).def_kind()
685684
} else {
686-
Some(DefKind::Macro(macro_kind(self.raw_proc_macro(index))))
685+
DefKind::Macro(macro_kind(self.raw_proc_macro(index)))
687686
}
688687
}
689688

@@ -1009,20 +1008,19 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10091008
.get(self, child_index)
10101009
.unwrap_or(Lazy::empty());
10111010
for child_index in child_children.decode((self, sess)) {
1012-
if let Some(kind) = self.def_kind(child_index) {
1013-
callback(Export {
1014-
res: Res::Def(kind, self.local_def_id(child_index)),
1015-
ident: self.item_ident(child_index, sess),
1016-
vis: self.get_visibility(child_index),
1017-
span: self
1018-
.root
1019-
.tables
1020-
.span
1021-
.get(self, child_index)
1022-
.unwrap()
1023-
.decode((self, sess)),
1024-
});
1025-
}
1011+
let kind = self.def_kind(child_index);
1012+
callback(Export {
1013+
res: Res::Def(kind, self.local_def_id(child_index)),
1014+
ident: self.item_ident(child_index, sess),
1015+
vis: self.get_visibility(child_index),
1016+
span: self
1017+
.root
1018+
.tables
1019+
.span
1020+
.get(self, child_index)
1021+
.unwrap()
1022+
.decode((self, sess)),
1023+
});
10261024
}
10271025
continue;
10281026
}
@@ -1033,10 +1031,8 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10331031

10341032
let def_key = self.def_key(child_index);
10351033
let span = self.get_span(child_index, sess);
1036-
if let (Some(kind), true) = (
1037-
self.def_kind(child_index),
1038-
def_key.disambiguated_data.data.get_opt_name().is_some(),
1039-
) {
1034+
if def_key.disambiguated_data.data.get_opt_name().is_some() {
1035+
let kind = self.def_kind(child_index);
10401036
let ident = self.item_ident(child_index, sess);
10411037
let vis = self.get_visibility(child_index);
10421038
let def_id = self.local_def_id(child_index);

src/librustc_middle/hir/map/mod.rs

+31-24
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::ty::TyCtxt;
66
use rustc_ast::ast::{self, Name, NodeId};
77
use rustc_data_structures::svh::Svh;
88
use rustc_hir::def::{DefKind, Res};
9-
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
9+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
1010
use rustc_hir::definitions::{DefKey, DefPath, Definitions};
1111
use rustc_hir::intravisit;
1212
use rustc_hir::itemlikevisit::ItemLikeVisitor;
@@ -227,10 +227,14 @@ impl<'hir> Map<'hir> {
227227
self.tcx.definitions.opt_local_def_id_to_hir_id(def_id)
228228
}
229229

230-
pub fn def_kind(&self, hir_id: HirId) -> Option<DefKind> {
231-
let node = self.find(hir_id)?;
230+
pub fn def_kind(&self, local_def_id: LocalDefId) -> DefKind {
231+
// FIXME(eddyb) support `find` on the crate root.
232+
if local_def_id.to_def_id().index == CRATE_DEF_INDEX {
233+
return DefKind::Mod;
234+
}
232235

233-
Some(match node {
236+
let hir_id = self.local_def_id_to_hir_id(local_def_id);
237+
match self.get(hir_id) {
234238
Node::Item(item) => match item.kind {
235239
ItemKind::Static(..) => DefKind::Static,
236240
ItemKind::Const(..) => DefKind::Const,
@@ -243,11 +247,11 @@ impl<'hir> Map<'hir> {
243247
ItemKind::Union(..) => DefKind::Union,
244248
ItemKind::Trait(..) => DefKind::Trait,
245249
ItemKind::TraitAlias(..) => DefKind::TraitAlias,
246-
ItemKind::ExternCrate(_)
247-
| ItemKind::Use(..)
248-
| ItemKind::ForeignMod(..)
249-
| ItemKind::GlobalAsm(..)
250-
| ItemKind::Impl { .. } => return None,
250+
ItemKind::ExternCrate(_) => DefKind::ExternCrate,
251+
ItemKind::Use(..) => DefKind::Use,
252+
ItemKind::ForeignMod(..) => DefKind::ForeignMod,
253+
ItemKind::GlobalAsm(..) => DefKind::GlobalAsm,
254+
ItemKind::Impl { .. } => DefKind::Impl,
251255
},
252256
Node::ForeignItem(item) => match item.kind {
253257
ForeignItemKind::Fn(..) => DefKind::Fn,
@@ -268,7 +272,7 @@ impl<'hir> Map<'hir> {
268272
Node::Variant(_) => DefKind::Variant,
269273
Node::Ctor(variant_data) => {
270274
// FIXME(eddyb) is this even possible, if we have a `Node::Ctor`?
271-
variant_data.ctor_hir_id()?;
275+
assert_ne!(variant_data.ctor_hir_id(), None);
272276

273277
let ctor_of = match self.find(self.get_parent_node(hir_id)) {
274278
Some(Node::Item(..)) => def::CtorOf::Struct,
@@ -277,10 +281,20 @@ impl<'hir> Map<'hir> {
277281
};
278282
DefKind::Ctor(ctor_of, def::CtorKind::from_hir(variant_data))
279283
}
280-
Node::AnonConst(_)
281-
| Node::Field(_)
282-
| Node::Expr(_)
283-
| Node::Stmt(_)
284+
Node::AnonConst(_) => DefKind::AnonConst,
285+
Node::Field(_) => DefKind::Field,
286+
Node::Expr(expr) => match expr.kind {
287+
ExprKind::Closure(.., None) => DefKind::Closure,
288+
ExprKind::Closure(.., Some(_)) => DefKind::Generator,
289+
_ => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
290+
},
291+
Node::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
292+
Node::GenericParam(param) => match param.kind {
293+
GenericParamKind::Lifetime { .. } => DefKind::LifetimeParam,
294+
GenericParamKind::Type { .. } => DefKind::TyParam,
295+
GenericParamKind::Const { .. } => DefKind::ConstParam,
296+
},
297+
Node::Stmt(_)
284298
| Node::PathSegment(_)
285299
| Node::Ty(_)
286300
| Node::TraitRef(_)
@@ -292,14 +306,8 @@ impl<'hir> Map<'hir> {
292306
| Node::Lifetime(_)
293307
| Node::Visibility(_)
294308
| Node::Block(_)
295-
| Node::Crate(_) => return None,
296-
Node::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
297-
Node::GenericParam(param) => match param.kind {
298-
GenericParamKind::Lifetime { .. } => return None,
299-
GenericParamKind::Type { .. } => DefKind::TyParam,
300-
GenericParamKind::Const { .. } => DefKind::ConstParam,
301-
},
302-
})
309+
| Node::Crate(_) => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
310+
}
303311
}
304312

305313
fn find_entry(&self, id: HirId) -> Option<Entry<'hir>> {
@@ -1082,6 +1090,5 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
10821090
}
10831091

10841092
pub fn provide(providers: &mut Providers<'_>) {
1085-
providers.def_kind =
1086-
|tcx, def_id| tcx.hir().def_kind(tcx.hir().as_local_hir_id(def_id.expect_local()));
1093+
providers.def_kind = |tcx, def_id| tcx.hir().def_kind(def_id.expect_local());
10871094
}

src/librustc_middle/middle/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ pub enum EvalResult {
246246
fn skip_stability_check_due_to_privacy(tcx: TyCtxt<'_>, mut def_id: DefId) -> bool {
247247
// Check if `def_id` is a trait method.
248248
match tcx.def_kind(def_id) {
249-
Some(DefKind::AssocFn) | Some(DefKind::AssocTy) | Some(DefKind::AssocConst) => {
249+
DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => {
250250
if let ty::TraitContainer(trait_def_id) = tcx.associated_item(def_id).container {
251251
// Trait methods do not declare visibility (even
252252
// for visibility info in cstore). Use containing

src/librustc_middle/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ rustc_queries! {
620620
cache_on_disk_if { true }
621621
}
622622

623-
query def_kind(_: DefId) -> Option<DefKind> {}
623+
query def_kind(_: DefId) -> DefKind {}
624624
query def_span(_: DefId) -> Span {
625625
// FIXME(mw): DefSpans are not really inputs since they are derived from
626626
// HIR. But at the moment HIR hashing still contains some hacks that allow

src/librustc_middle/ty/context.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use rustc_errors::ErrorReported;
4848
use rustc_hir as hir;
4949
use rustc_hir::def::{DefKind, Res};
5050
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId, LOCAL_CRATE};
51-
use rustc_hir::definitions::{DefPathData, DefPathHash, Definitions};
51+
use rustc_hir::definitions::{DefPathHash, Definitions};
5252
use rustc_hir::lang_items;
5353
use rustc_hir::lang_items::PanicLocationLangItem;
5454
use rustc_hir::{HirId, Node, TraitCandidate};
@@ -1492,21 +1492,13 @@ impl<'tcx> TyCtxt<'tcx> {
14921492

14931493
/// Returns a displayable description and article for the given `def_id` (e.g. `("a", "struct")`).
14941494
pub fn article_and_description(&self, def_id: DefId) -> (&'static str, &'static str) {
1495-
self.def_kind(def_id)
1496-
.map(|def_kind| (def_kind.article(), def_kind.descr(def_id)))
1497-
.unwrap_or_else(|| match self.def_key(def_id).disambiguated_data.data {
1498-
DefPathData::ClosureExpr => match self.generator_kind(def_id) {
1499-
None => ("a", "closure"),
1500-
Some(rustc_hir::GeneratorKind::Async(..)) => ("an", "async closure"),
1501-
Some(rustc_hir::GeneratorKind::Gen) => ("a", "generator"),
1502-
},
1503-
DefPathData::LifetimeNs(..) => ("a", "lifetime"),
1504-
DefPathData::Impl => ("an", "implementation"),
1505-
DefPathData::TypeNs(..) | DefPathData::ValueNs(..) | DefPathData::MacroNs(..) => {
1506-
unreachable!()
1507-
}
1508-
_ => bug!("article_and_description called on def_id {:?}", def_id),
1509-
})
1495+
match self.def_kind(def_id) {
1496+
DefKind::Generator => match self.generator_kind(def_id).unwrap() {
1497+
rustc_hir::GeneratorKind::Async(..) => ("an", "async closure"),
1498+
rustc_hir::GeneratorKind::Gen => ("a", "generator"),
1499+
},
1500+
def_kind => (def_kind.article(), def_kind.descr(def_id)),
1501+
}
15101502
}
15111503
}
15121504

src/librustc_middle/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2680,7 +2680,7 @@ impl<'tcx> TyCtxt<'tcx> {
26802680
}
26812681
} else {
26822682
match self.def_kind(def_id) {
2683-
Some(DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy) => true,
2683+
DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy => true,
26842684
_ => false,
26852685
}
26862686
};

src/librustc_middle/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ pub trait PrettyPrinter<'tcx>:
888888
p!(write("::{:?}", promoted));
889889
} else {
890890
match self.tcx().def_kind(did) {
891-
Some(DefKind::Static | DefKind::Const | DefKind::AssocConst) => {
891+
DefKind::Static | DefKind::Const | DefKind::AssocConst => {
892892
p!(print_value_path(did, substs))
893893
}
894894
_ => {

0 commit comments

Comments
 (0)