Skip to content

Commit b92b1a7

Browse files
committed
rustc: use DefKind instead of Def, where possible.
1 parent a3fcab3 commit b92b1a7

File tree

31 files changed

+381
-357
lines changed

31 files changed

+381
-357
lines changed

src/librustc/hir/def.rs

+50-34
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,53 @@ pub enum DefKind {
8080
Macro(MacroKind),
8181
}
8282

83+
impl DefKind {
84+
pub fn descr(self) -> &'static str {
85+
match self {
86+
DefKind::Fn => "function",
87+
DefKind::Mod => "module",
88+
DefKind::Static => "static",
89+
DefKind::Enum => "enum",
90+
DefKind::Variant => "variant",
91+
DefKind::Ctor(CtorOf::Variant, CtorKind::Fn) => "tuple variant",
92+
DefKind::Ctor(CtorOf::Variant, CtorKind::Const) => "unit variant",
93+
DefKind::Ctor(CtorOf::Variant, CtorKind::Fictive) => "struct variant",
94+
DefKind::Struct => "struct",
95+
DefKind::Ctor(CtorOf::Struct, CtorKind::Fn) => "tuple struct",
96+
DefKind::Ctor(CtorOf::Struct, CtorKind::Const) => "unit struct",
97+
DefKind::Ctor(CtorOf::Struct, CtorKind::Fictive) =>
98+
bug!("impossible struct constructor"),
99+
DefKind::Existential => "existential type",
100+
DefKind::TyAlias => "type alias",
101+
DefKind::TraitAlias => "trait alias",
102+
DefKind::AssociatedTy => "associated type",
103+
DefKind::AssociatedExistential => "associated existential type",
104+
DefKind::Union => "union",
105+
DefKind::Trait => "trait",
106+
DefKind::ForeignTy => "foreign type",
107+
DefKind::Method => "method",
108+
DefKind::Const => "constant",
109+
DefKind::AssociatedConst => "associated constant",
110+
DefKind::TyParam => "type parameter",
111+
DefKind::ConstParam => "const parameter",
112+
DefKind::Macro(macro_kind) => macro_kind.descr(),
113+
}
114+
}
115+
116+
/// An English article for the def.
117+
pub fn article(&self) -> &'static str {
118+
match *self {
119+
DefKind::AssociatedTy
120+
| DefKind::AssociatedConst
121+
| DefKind::AssociatedExistential
122+
| DefKind::Enum
123+
| DefKind::Existential => "an",
124+
DefKind::Macro(macro_kind) => macro_kind.article(),
125+
_ => "a",
126+
}
127+
}
128+
}
129+
83130
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
84131
pub enum Def<Id = hir::HirId> {
85132
Def(DefKind, DefId),
@@ -328,39 +375,13 @@ impl<Id> Def<Id> {
328375
/// A human readable name for the def kind ("function", "module", etc.).
329376
pub fn kind_name(&self) -> &'static str {
330377
match *self {
331-
Def::Def(DefKind::Fn, _) => "function",
332-
Def::Def(DefKind::Mod, _) => "module",
333-
Def::Def(DefKind::Static, _) => "static",
334-
Def::Def(DefKind::Enum, _) => "enum",
335-
Def::Def(DefKind::Variant, _) => "variant",
336-
Def::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Fn), _) => "tuple variant",
337-
Def::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Const), _) => "unit variant",
338-
Def::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Fictive), _) => "struct variant",
339-
Def::Def(DefKind::Struct, _) => "struct",
340-
Def::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Fn), _) => "tuple struct",
341-
Def::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Const), _) => "unit struct",
342-
Def::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Fictive), _) =>
343-
bug!("impossible struct constructor"),
344-
Def::Def(DefKind::Existential, _) => "existential type",
345-
Def::Def(DefKind::TyAlias, _) => "type alias",
346-
Def::Def(DefKind::TraitAlias, _) => "trait alias",
347-
Def::Def(DefKind::AssociatedTy, _) => "associated type",
348-
Def::Def(DefKind::AssociatedExistential, _) => "associated existential type",
378+
Def::Def(kind, _) => kind.descr(),
349379
Def::SelfCtor(..) => "self constructor",
350-
Def::Def(DefKind::Union, _) => "union",
351-
Def::Def(DefKind::Trait, _) => "trait",
352-
Def::Def(DefKind::ForeignTy, _) => "foreign type",
353-
Def::Def(DefKind::Method, _) => "method",
354-
Def::Def(DefKind::Const, _) => "constant",
355-
Def::Def(DefKind::AssociatedConst, _) => "associated constant",
356-
Def::Def(DefKind::TyParam, _) => "type parameter",
357-
Def::Def(DefKind::ConstParam, _) => "const parameter",
358380
Def::PrimTy(..) => "builtin type",
359381
Def::Local(..) => "local variable",
360382
Def::Upvar(..) => "closure capture",
361383
Def::Label(..) => "label",
362384
Def::SelfTy(..) => "self type",
363-
Def::Def(DefKind::Macro(macro_kind), _) => macro_kind.descr(),
364385
Def::ToolMod => "tool module",
365386
Def::NonMacroAttr(attr_kind) => attr_kind.descr(),
366387
Def::Err => "unresolved item",
@@ -370,13 +391,8 @@ impl<Id> Def<Id> {
370391
/// An English article for the def.
371392
pub fn article(&self) -> &'static str {
372393
match *self {
373-
Def::Def(DefKind::AssociatedTy, _)
374-
| Def::Def(DefKind::AssociatedConst, _)
375-
| Def::Def(DefKind::AssociatedExistential, _)
376-
| Def::Def(DefKind::Enum, _)
377-
| Def::Def(DefKind::Existential, _)
378-
| Def::Err => "an",
379-
Def::Def(DefKind::Macro(macro_kind), _) => macro_kind.article(),
394+
Def::Def(kind, _) => kind.article(),
395+
Def::Err => "an",
380396
_ => "a",
381397
}
382398
}

src/librustc/hir/map/mod.rs

+8-19
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use syntax::ext::base::MacroKind;
1717
use syntax_pos::{Span, DUMMY_SP};
1818

1919
use crate::hir::*;
20-
use crate::hir::{Def, DefKind};
20+
use crate::hir::DefKind;
2121
use crate::hir::itemlikevisit::ItemLikeVisitor;
2222
use crate::hir::print::Nested;
2323
use crate::util::nodemap::FxHashMap;
@@ -310,14 +310,14 @@ impl<'hir> Map<'hir> {
310310
self.definitions.as_local_node_id(def_id.to_def_id()).unwrap()
311311
}
312312

313-
pub fn describe_def(&self, node_id: NodeId) -> Option<Def> {
313+
fn def_kind(&self, node_id: NodeId) -> Option<DefKind> {
314314
let node = if let Some(node) = self.find(node_id) {
315315
node
316316
} else {
317317
return None
318318
};
319319

320-
let kind = match node {
320+
Some(match node {
321321
Node::Item(item) => {
322322
match item.node {
323323
ItemKind::Static(..) => DefKind::Static,
@@ -382,15 +382,11 @@ impl<'hir> Map<'hir> {
382382
Node::TraitRef(_) |
383383
Node::Pat(_) |
384384
Node::Binding(_) |
385+
Node::Local(_) |
385386
Node::Lifetime(_) |
386387
Node::Visibility(_) |
387388
Node::Block(_) |
388389
Node::Crate => return None,
389-
// FIXME(eddyb) this is the only non-`DefKind` case here,
390-
// investigate whether it's actually used, and ideally remove it.
391-
Node::Local(local) => {
392-
return Some(Def::Local(local.hir_id));
393-
}
394390
Node::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
395391
Node::GenericParam(param) => {
396392
match param.kind {
@@ -399,14 +395,7 @@ impl<'hir> Map<'hir> {
399395
GenericParamKind::Const { .. } => DefKind::ConstParam,
400396
}
401397
}
402-
};
403-
Some(Def::Def(kind, self.local_def_id(node_id)))
404-
}
405-
406-
// FIXME(@ljedrz): replace the NodeId variant
407-
pub fn describe_def_by_hir_id(&self, hir_id: HirId) -> Option<Def> {
408-
let node_id = self.hir_to_node_id(hir_id);
409-
self.describe_def(node_id)
398+
})
410399
}
411400

412401
fn entry_count(&self) -> usize {
@@ -1464,11 +1453,11 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
14641453
node_id_to_string(map, node_id, include_id)
14651454
}
14661455

1467-
pub fn describe_def(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Option<Def> {
1456+
pub fn def_kind(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Option<DefKind> {
14681457
if let Some(node_id) = tcx.hir().as_local_node_id(def_id) {
1469-
tcx.hir().describe_def(node_id)
1458+
tcx.hir().def_kind(node_id)
14701459
} else {
1471-
bug!("Calling local describe_def query provider for upstream DefId: {:?}",
1460+
bug!("Calling local def_kind query provider for upstream DefId: {:?}",
14721461
def_id)
14731462
}
14741463
}

src/librustc/hir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2521,7 +2521,7 @@ pub type GlobMap = NodeMap<FxHashSet<Name>>;
25212521

25222522
pub fn provide(providers: &mut Providers<'_>) {
25232523
check_attr::provide(providers);
2524-
providers.describe_def = map::describe_def;
2524+
providers.def_kind = map::def_kind;
25252525
}
25262526

25272527
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]

src/librustc/middle/reachable.rs

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ReachableContext<'a, 'tcx> {
9898
}
9999
hir::ExprKind::MethodCall(..) => {
100100
self.tables.type_dependent_def(expr.hir_id)
101+
.map(|(kind, def_id)| Def::Def(kind, def_id))
101102
}
102103
_ => None
103104
};

src/librustc/middle/stability.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -525,10 +525,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
525525
// See issue #38412.
526526
fn skip_stability_check_due_to_privacy(self, mut def_id: DefId) -> bool {
527527
// Check if `def_id` is a trait method.
528-
match self.describe_def(def_id) {
529-
Some(Def::Def(DefKind::Method, _)) |
530-
Some(Def::Def(DefKind::AssociatedTy, _)) |
531-
Some(Def::Def(DefKind::AssociatedConst, _)) => {
528+
match self.def_kind(def_id) {
529+
Some(DefKind::Method) |
530+
Some(DefKind::AssociatedTy) |
531+
Some(DefKind::AssociatedConst) => {
532532
if let ty::TraitContainer(trait_def_id) = self.associated_item(def_id).container {
533533
// Trait methods do not declare visibility (even
534534
// for visibility info in cstore). Use containing

src/librustc/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ rustc_queries! {
469469
cache { true }
470470
}
471471

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

src/librustc/ty/context.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use crate::ty::steal::Steal;
4444
use crate::ty::subst::{UserSubsts, UnpackedKind};
4545
use crate::ty::{BoundVar, BindingMode};
4646
use crate::ty::CanonicalPolyFnSig;
47+
use crate::util::common::ErrorReported;
4748
use crate::util::nodemap::{DefIdMap, DefIdSet, ItemLocalMap, ItemLocalSet};
4849
use crate::util::nodemap::{FxHashMap, FxHashSet};
4950
use errors::DiagnosticBuilder;
@@ -347,7 +348,7 @@ pub struct TypeckTables<'tcx> {
347348

348349
/// Resolved definitions for `<T>::X` associated paths and
349350
/// method calls, including those of overloaded operators.
350-
type_dependent_defs: ItemLocalMap<Def>,
351+
type_dependent_defs: ItemLocalMap<Result<(DefKind, DefId), ErrorReported>>,
351352

352353
/// Resolved field indices for field accesses in expressions (`S { field }`, `obj.field`)
353354
/// or patterns (`S { field }`). The index is often useful by itself, but to learn more
@@ -481,30 +482,32 @@ impl<'tcx> TypeckTables<'tcx> {
481482
pub fn qpath_def(&self, qpath: &hir::QPath, id: hir::HirId) -> Def {
482483
match *qpath {
483484
hir::QPath::Resolved(_, ref path) => path.def,
484-
hir::QPath::TypeRelative(..) => {
485-
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
486-
self.type_dependent_defs.get(&id.local_id).cloned().unwrap_or(Def::Err)
487-
}
485+
hir::QPath::TypeRelative(..) => self.type_dependent_def(id)
486+
.map_or(Def::Err, |(kind, def_id)| Def::Def(kind, def_id)),
488487
}
489488
}
490489

491-
pub fn type_dependent_defs(&self) -> LocalTableInContext<'_, Def> {
490+
pub fn type_dependent_defs(
491+
&self,
492+
) -> LocalTableInContext<'_, Result<(DefKind, DefId), ErrorReported>> {
492493
LocalTableInContext {
493494
local_id_root: self.local_id_root,
494495
data: &self.type_dependent_defs
495496
}
496497
}
497498

498-
pub fn type_dependent_def(&self, id: HirId) -> Option<Def> {
499+
pub fn type_dependent_def(&self, id: HirId) -> Option<(DefKind, DefId)> {
499500
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
500-
self.type_dependent_defs.get(&id.local_id).cloned()
501+
self.type_dependent_defs.get(&id.local_id).cloned().and_then(|r| r.ok())
501502
}
502503

503504
pub fn type_dependent_def_id(&self, id: HirId) -> Option<DefId> {
504-
self.type_dependent_def(id).map(|def| def.def_id())
505+
self.type_dependent_def(id).map(|(_, def_id)| def_id)
505506
}
506507

507-
pub fn type_dependent_defs_mut(&mut self) -> LocalTableInContextMut<'_, Def> {
508+
pub fn type_dependent_defs_mut(
509+
&mut self,
510+
) -> LocalTableInContextMut<'_, Result<(DefKind, DefId), ErrorReported>> {
508511
LocalTableInContextMut {
509512
local_id_root: self.local_id_root,
510513
data: &mut self.type_dependent_defs
@@ -658,7 +661,7 @@ impl<'tcx> TypeckTables<'tcx> {
658661
}
659662

660663
match self.type_dependent_defs().get(expr.hir_id) {
661-
Some(&Def::Def(DefKind::Method, _)) => true,
664+
Some(Ok((DefKind::Method, _))) => true,
662665
_ => false
663666
}
664667
}

src/librustc/ty/mod.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,12 @@ pub enum AssociatedKind {
191191
}
192192

193193
impl AssociatedItem {
194-
pub fn def(&self) -> Def {
194+
pub fn def_kind(&self) -> DefKind {
195195
match self.kind {
196-
AssociatedKind::Const => Def::Def(DefKind::AssociatedConst, self.def_id),
197-
AssociatedKind::Method => Def::Def(DefKind::Method, self.def_id),
198-
AssociatedKind::Type => Def::Def(DefKind::AssociatedTy, self.def_id),
199-
AssociatedKind::Existential => Def::Def(DefKind::AssociatedExistential, self.def_id),
196+
AssociatedKind::Const => DefKind::AssociatedConst,
197+
AssociatedKind::Method => DefKind::Method,
198+
AssociatedKind::Type => DefKind::AssociatedTy,
199+
AssociatedKind::Existential => DefKind::AssociatedExistential,
200200
}
201201
}
202202

@@ -2805,10 +2805,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
28052805
_ => false,
28062806
}
28072807
} else {
2808-
match self.describe_def(def_id).expect("no def for def-id") {
2809-
Def::Def(DefKind::AssociatedConst, _)
2810-
| Def::Def(DefKind::Method, _)
2811-
| Def::Def(DefKind::AssociatedTy, _) => true,
2808+
match self.def_kind(def_id).expect("no def for def-id") {
2809+
DefKind::AssociatedConst
2810+
| DefKind::Method
2811+
| DefKind::AssociatedTy => true,
28122812
_ => false,
28132813
}
28142814
};
@@ -3046,7 +3046,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
30463046
/// `DefId` of the impl that the method belongs to; otherwise, returns `None`.
30473047
pub fn impl_of_method(self, def_id: DefId) -> Option<DefId> {
30483048
let item = if def_id.krate != LOCAL_CRATE {
3049-
if let Some(Def::Def(DefKind::Method, _)) = self.describe_def(def_id) {
3049+
if let Some(DefKind::Method) = self.def_kind(def_id) {
30503050
Some(self.associated_item(def_id))
30513051
} else {
30523052
None

src/librustc/ty/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::dep_graph::{self, DepNode};
22
use crate::hir::def_id::{CrateNum, DefId, DefIndex};
3-
use crate::hir::def::{Def, Export};
3+
use crate::hir::def::{DefKind, Export};
44
use crate::hir::{self, TraitCandidate, ItemLocalId, CodegenFnAttrs};
55
use crate::infer::canonical::{self, Canonical};
66
use crate::lint;

src/librustc_lint/unused.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
8787

8888
let mut fn_warned = false;
8989
let mut op_warned = false;
90-
let maybe_def = match expr.node {
90+
let maybe_def_id = match expr.node {
9191
hir::ExprKind::Call(ref callee, _) => {
9292
match callee.node {
9393
hir::ExprKind::Path(ref qpath) => {
9494
let def = cx.tables.qpath_def(qpath, callee.hir_id);
9595
match def {
96-
Def::Def(DefKind::Fn, _) | Def::Def(DefKind::Method, _) => Some(def),
96+
Def::Def(DefKind::Fn, def_id)
97+
| Def::Def(DefKind::Method, def_id) => Some(def_id),
9798
// `Def::Local` if it was a closure, for which we
9899
// do not currently support must-use linting
99100
_ => None
@@ -103,12 +104,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
103104
}
104105
},
105106
hir::ExprKind::MethodCall(..) => {
106-
cx.tables.type_dependent_def(expr.hir_id)
107+
cx.tables.type_dependent_def_id(expr.hir_id)
107108
},
108109
_ => None
109110
};
110-
if let Some(def) = maybe_def {
111-
let def_id = def.def_id();
111+
if let Some(def_id) = maybe_def_id {
112112
fn_warned = check_must_use(cx, def_id, s.span, "return value of ", "");
113113
} else if type_permits_lack_of_use {
114114
// We don't warn about unused unit or uninhabited types.

src/librustc_metadata/cstore_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
138138
is_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) }
139139
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
140140
static_mutability => { cdata.static_mutability(def_id.index) }
141-
describe_def => { cdata.get_def(def_id.index) }
141+
def_kind => { cdata.def_kind(def_id.index) }
142142
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
143143
lookup_stability => {
144144
cdata.get_stability(def_id.index).map(|s| tcx.intern_stability(s))

0 commit comments

Comments
 (0)