Skip to content

Commit c39cfaf

Browse files
authored
Rollup merge of rust-lang#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 52fa23a + 30e62ba commit c39cfaf

File tree

40 files changed

+266
-274
lines changed

40 files changed

+266
-274
lines changed

src/librustc_hir/def.rs

Lines changed: 38 additions & 1 deletion
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

Lines changed: 0 additions & 31 deletions
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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
192192
.get_opt_name()
193193
.map(|parent_symbol| parent_symbol.to_string());
194194

195-
let type_parent_desc = self
196-
.tcx
197-
.def_kind(parent_def_id)
198-
.map(|parent_def_kind| parent_def_kind.descr(parent_def_id));
199-
200-
(parent_name, type_parent_desc)
195+
(parent_name, Some(self.tcx.def_kind(parent_def_id).descr(parent_def_id)))
201196
} else {
202197
(None, None)
203198
};

src/librustc_metadata/rmeta/decoder.rs

Lines changed: 26 additions & 30 deletions
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

Lines changed: 31 additions & 29 deletions
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;
@@ -229,10 +229,14 @@ impl<'hir> Map<'hir> {
229229
self.tcx.definitions.opt_local_def_id_to_hir_id(def_id)
230230
}
231231

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

235-
Some(match node {
238+
let hir_id = self.local_def_id_to_hir_id(local_def_id);
239+
match self.get(hir_id) {
236240
Node::Item(item) => match item.kind {
237241
ItemKind::Static(..) => DefKind::Static,
238242
ItemKind::Const(..) => DefKind::Const,
@@ -245,11 +249,11 @@ impl<'hir> Map<'hir> {
245249
ItemKind::Union(..) => DefKind::Union,
246250
ItemKind::Trait(..) => DefKind::Trait,
247251
ItemKind::TraitAlias(..) => DefKind::TraitAlias,
248-
ItemKind::ExternCrate(_)
249-
| ItemKind::Use(..)
250-
| ItemKind::ForeignMod(..)
251-
| ItemKind::GlobalAsm(..)
252-
| ItemKind::Impl { .. } => return None,
252+
ItemKind::ExternCrate(_) => DefKind::ExternCrate,
253+
ItemKind::Use(..) => DefKind::Use,
254+
ItemKind::ForeignMod(..) => DefKind::ForeignMod,
255+
ItemKind::GlobalAsm(..) => DefKind::GlobalAsm,
256+
ItemKind::Impl { .. } => DefKind::Impl,
253257
},
254258
Node::ForeignItem(item) => match item.kind {
255259
ForeignItemKind::Fn(..) => DefKind::Fn,
@@ -270,7 +274,7 @@ impl<'hir> Map<'hir> {
270274
Node::Variant(_) => DefKind::Variant,
271275
Node::Ctor(variant_data) => {
272276
// FIXME(eddyb) is this even possible, if we have a `Node::Ctor`?
273-
variant_data.ctor_hir_id()?;
277+
assert_ne!(variant_data.ctor_hir_id(), None);
274278

275279
let ctor_of = match self.find(self.get_parent_node(hir_id)) {
276280
Some(Node::Item(..)) => def::CtorOf::Struct,
@@ -279,10 +283,20 @@ impl<'hir> Map<'hir> {
279283
};
280284
DefKind::Ctor(ctor_of, def::CtorKind::from_hir(variant_data))
281285
}
282-
Node::AnonConst(_)
283-
| Node::Field(_)
284-
| Node::Expr(_)
285-
| Node::Stmt(_)
286+
Node::AnonConst(_) => DefKind::AnonConst,
287+
Node::Field(_) => DefKind::Field,
288+
Node::Expr(expr) => match expr.kind {
289+
ExprKind::Closure(.., None) => DefKind::Closure,
290+
ExprKind::Closure(.., Some(_)) => DefKind::Generator,
291+
_ => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
292+
},
293+
Node::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
294+
Node::GenericParam(param) => match param.kind {
295+
GenericParamKind::Lifetime { .. } => DefKind::LifetimeParam,
296+
GenericParamKind::Type { .. } => DefKind::TyParam,
297+
GenericParamKind::Const { .. } => DefKind::ConstParam,
298+
},
299+
Node::Stmt(_)
286300
| Node::PathSegment(_)
287301
| Node::Ty(_)
288302
| Node::TraitRef(_)
@@ -294,14 +308,8 @@ impl<'hir> Map<'hir> {
294308
| Node::Lifetime(_)
295309
| Node::Visibility(_)
296310
| Node::Block(_)
297-
| Node::Crate(_) => return None,
298-
Node::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
299-
Node::GenericParam(param) => match param.kind {
300-
GenericParamKind::Lifetime { .. } => return None,
301-
GenericParamKind::Type { .. } => DefKind::TyParam,
302-
GenericParamKind::Const { .. } => DefKind::ConstParam,
303-
},
304-
})
311+
| Node::Crate(_) => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
312+
}
305313
}
306314

307315
fn find_entry(&self, id: HirId) -> Option<Entry<'hir>> {
@@ -1086,11 +1094,5 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
10861094
}
10871095

10881096
pub fn provide(providers: &mut Providers<'_>) {
1089-
providers.def_kind = |tcx, def_id| {
1090-
if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) {
1091-
tcx.hir().def_kind(hir_id)
1092-
} else {
1093-
bug!("calling local def_kind query provider for upstream DefId: {:?}", def_id);
1094-
}
1095-
};
1097+
providers.def_kind = |tcx, def_id| tcx.hir().def_kind(def_id.expect_local());
10961098
}

src/librustc_middle/middle/stability.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ rustc_queries! {
630630
cache_on_disk_if { true }
631631
}
632632

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

src/librustc_middle/ty/context.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use rustc_errors::ErrorReported;
5050
use rustc_hir as hir;
5151
use rustc_hir::def::{DefKind, Res};
5252
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId, LOCAL_CRATE};
53-
use rustc_hir::definitions::{DefPathData, DefPathHash, Definitions};
53+
use rustc_hir::definitions::{DefPathHash, Definitions};
5454
use rustc_hir::lang_items;
5555
use rustc_hir::lang_items::PanicLocationLangItem;
5656
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

0 commit comments

Comments
 (0)