Skip to content

Commit 13fde05

Browse files
committed
Auto merge of #60462 - eddyb:def-1-a-mere-resolution, r=petrochenkov
rustc: factor out most of hir::def::Def's variants into DefKind, and rename to Res. The first two commits are about introducing `DefKind`, both to simplify/orthogonalize `hir::def::Def`, and to allow reasoning about the kind of a definition without dealing with the redundant `DefId`. (There are likely more changes to be made, such as adding enough `DefKind` variants for `tcx.def_kind(def_id)` to return just `DefKind`, not `Option<DefKind>`, but this is pretty big as-is) The third commit frees up the `Def` name (which we may want to use in the future for "definitions", in the sense of "entities with a `DefId`") by renaming `hir::def::Def` to `Res` ("resolution"). IMO this fits, as it represents all the possible name resolution results, not just "definitions (with a `DefId`)". Quick examples: ```rust // Before: if tcx.describe_def(def_id) == Some(Def::Struct(def_id)) {...} if let Def::Struct(def_id) = path.def {...} ``` ```rust // After: if tcx.def_kind(def_id) == Some(DefKind::Struct) {...} if let Res::Def(DefKind::Struct, def_id) = path.res {...} ``` r? @petrochenkov cc @rust-lang/compiler
2 parents a340455 + ff174fe commit 13fde05

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1840
-1692
lines changed

src/librustc/hir/def.rs

+144-142
Large diffs are not rendered by default.

src/librustc/hir/lowering.rs

+69-66
Large diffs are not rendered by default.

src/librustc/hir/map/mod.rs

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

1919
use crate::hir::*;
20+
use crate::hir::DefKind;
2021
use crate::hir::itemlikevisit::ItemLikeVisitor;
2122
use crate::hir::print::Nested;
2223
use crate::util::nodemap::FxHashMap;
@@ -309,74 +310,68 @@ impl<'hir> Map<'hir> {
309310
self.definitions.as_local_node_id(def_id.to_def_id()).unwrap()
310311
}
311312

312-
pub fn describe_def(&self, node_id: NodeId) -> Option<Def> {
313+
fn def_kind(&self, node_id: NodeId) -> Option<DefKind> {
313314
let node = if let Some(node) = self.find(node_id) {
314315
node
315316
} else {
316317
return None
317318
};
318319

319-
match node {
320+
Some(match node {
320321
Node::Item(item) => {
321-
let def_id = || self.local_def_id_from_hir_id(item.hir_id);
322-
323322
match item.node {
324-
ItemKind::Static(..) => Some(Def::Static(def_id())),
325-
ItemKind::Const(..) => Some(Def::Const(def_id())),
326-
ItemKind::Fn(..) => Some(Def::Fn(def_id())),
327-
ItemKind::Mod(..) => Some(Def::Mod(def_id())),
328-
ItemKind::Existential(..) => Some(Def::Existential(def_id())),
329-
ItemKind::Ty(..) => Some(Def::TyAlias(def_id())),
330-
ItemKind::Enum(..) => Some(Def::Enum(def_id())),
331-
ItemKind::Struct(..) => Some(Def::Struct(def_id())),
332-
ItemKind::Union(..) => Some(Def::Union(def_id())),
333-
ItemKind::Trait(..) => Some(Def::Trait(def_id())),
334-
ItemKind::TraitAlias(..) => Some(Def::TraitAlias(def_id())),
323+
ItemKind::Static(..) => DefKind::Static,
324+
ItemKind::Const(..) => DefKind::Const,
325+
ItemKind::Fn(..) => DefKind::Fn,
326+
ItemKind::Mod(..) => DefKind::Mod,
327+
ItemKind::Existential(..) => DefKind::Existential,
328+
ItemKind::Ty(..) => DefKind::TyAlias,
329+
ItemKind::Enum(..) => DefKind::Enum,
330+
ItemKind::Struct(..) => DefKind::Struct,
331+
ItemKind::Union(..) => DefKind::Union,
332+
ItemKind::Trait(..) => DefKind::Trait,
333+
ItemKind::TraitAlias(..) => DefKind::TraitAlias,
335334
ItemKind::ExternCrate(_) |
336335
ItemKind::Use(..) |
337336
ItemKind::ForeignMod(..) |
338337
ItemKind::GlobalAsm(..) |
339-
ItemKind::Impl(..) => None,
338+
ItemKind::Impl(..) => return None,
340339
}
341340
}
342341
Node::ForeignItem(item) => {
343-
let def_id = self.local_def_id_from_hir_id(item.hir_id);
344342
match item.node {
345-
ForeignItemKind::Fn(..) => Some(Def::Fn(def_id)),
346-
ForeignItemKind::Static(..) => Some(Def::Static(def_id)),
347-
ForeignItemKind::Type => Some(Def::ForeignTy(def_id)),
343+
ForeignItemKind::Fn(..) => DefKind::Fn,
344+
ForeignItemKind::Static(..) => DefKind::Static,
345+
ForeignItemKind::Type => DefKind::ForeignTy,
348346
}
349347
}
350348
Node::TraitItem(item) => {
351-
let def_id = self.local_def_id_from_hir_id(item.hir_id);
352349
match item.node {
353-
TraitItemKind::Const(..) => Some(Def::AssociatedConst(def_id)),
354-
TraitItemKind::Method(..) => Some(Def::Method(def_id)),
355-
TraitItemKind::Type(..) => Some(Def::AssociatedTy(def_id)),
350+
TraitItemKind::Const(..) => DefKind::AssociatedConst,
351+
TraitItemKind::Method(..) => DefKind::Method,
352+
TraitItemKind::Type(..) => DefKind::AssociatedTy,
356353
}
357354
}
358355
Node::ImplItem(item) => {
359-
let def_id = self.local_def_id_from_hir_id(item.hir_id);
360356
match item.node {
361-
ImplItemKind::Const(..) => Some(Def::AssociatedConst(def_id)),
362-
ImplItemKind::Method(..) => Some(Def::Method(def_id)),
363-
ImplItemKind::Type(..) => Some(Def::AssociatedTy(def_id)),
364-
ImplItemKind::Existential(..) => Some(Def::AssociatedExistential(def_id)),
357+
ImplItemKind::Const(..) => DefKind::AssociatedConst,
358+
ImplItemKind::Method(..) => DefKind::Method,
359+
ImplItemKind::Type(..) => DefKind::AssociatedTy,
360+
ImplItemKind::Existential(..) => DefKind::AssociatedExistential,
365361
}
366362
}
367-
Node::Variant(variant) => {
368-
let def_id = self.local_def_id_from_hir_id(variant.node.id);
369-
Some(Def::Variant(def_id))
370-
}
363+
Node::Variant(_) => DefKind::Variant,
371364
Node::Ctor(variant_data) => {
365+
// FIXME(eddyb) is this even possible, if we have a `Node::Ctor`?
366+
if variant_data.ctor_hir_id().is_none() {
367+
return None;
368+
}
372369
let ctor_of = match self.find(self.get_parent_node(node_id)) {
373370
Some(Node::Item(..)) => def::CtorOf::Struct,
374371
Some(Node::Variant(..)) => def::CtorOf::Variant,
375372
_ => unreachable!(),
376373
};
377-
variant_data.ctor_hir_id()
378-
.map(|hir_id| self.local_def_id_from_hir_id(hir_id))
379-
.map(|def_id| Def::Ctor(def_id, ctor_of, def::CtorKind::from_hir(variant_data)))
374+
DefKind::Ctor(ctor_of, def::CtorKind::from_hir(variant_data))
380375
}
381376
Node::AnonConst(_) |
382377
Node::Field(_) |
@@ -387,35 +382,20 @@ impl<'hir> Map<'hir> {
387382
Node::TraitRef(_) |
388383
Node::Pat(_) |
389384
Node::Binding(_) |
385+
Node::Local(_) |
390386
Node::Lifetime(_) |
391387
Node::Visibility(_) |
392388
Node::Block(_) |
393-
Node::Crate => None,
394-
Node::Local(local) => {
395-
Some(Def::Local(local.hir_id))
396-
}
397-
Node::MacroDef(macro_def) => {
398-
Some(Def::Macro(self.local_def_id_from_hir_id(macro_def.hir_id),
399-
MacroKind::Bang))
400-
}
389+
Node::Crate => return None,
390+
Node::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
401391
Node::GenericParam(param) => {
402-
Some(match param.kind {
403-
GenericParamKind::Lifetime { .. } => {
404-
Def::Local(param.hir_id)
405-
},
406-
GenericParamKind::Type { .. } => Def::TyParam(
407-
self.local_def_id_from_hir_id(param.hir_id)),
408-
GenericParamKind::Const { .. } => Def::ConstParam(
409-
self.local_def_id_from_hir_id(param.hir_id)),
410-
})
392+
match param.kind {
393+
GenericParamKind::Lifetime { .. } => return None,
394+
GenericParamKind::Type { .. } => DefKind::TyParam,
395+
GenericParamKind::Const { .. } => DefKind::ConstParam,
396+
}
411397
}
412-
}
413-
}
414-
415-
// FIXME(@ljedrz): replace the NodeId variant
416-
pub fn describe_def_by_hir_id(&self, hir_id: HirId) -> Option<Def> {
417-
let node_id = self.hir_to_node_id(hir_id);
418-
self.describe_def(node_id)
398+
})
419399
}
420400

421401
fn entry_count(&self) -> usize {
@@ -1473,11 +1453,11 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
14731453
node_id_to_string(map, node_id, include_id)
14741454
}
14751455

1476-
pub fn describe_def(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Option<Def> {
1456+
pub fn def_kind(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Option<DefKind> {
14771457
if let Some(node_id) = tcx.hir().as_local_node_id(def_id) {
1478-
tcx.hir().describe_def(node_id)
1458+
tcx.hir().def_kind(node_id)
14791459
} else {
1480-
bug!("Calling local describe_def query provider for upstream DefId: {:?}",
1460+
bug!("Calling local def_kind query provider for upstream DefId: {:?}",
14811461
def_id)
14821462
}
14831463
}

src/librustc/hir/mod.rs

+24-21
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub use self::PrimTy::*;
1010
pub use self::UnOp::*;
1111
pub use self::UnsafeSource::*;
1212

13-
use crate::hir::def::Def;
13+
use crate::hir::def::{Res, DefKind};
1414
use crate::hir::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX};
1515
use crate::util::nodemap::{NodeMap, FxHashSet};
1616
use crate::mir::mono::Linkage;
@@ -296,8 +296,8 @@ impl Lifetime {
296296
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
297297
pub struct Path {
298298
pub span: Span,
299-
/// The definition that the path resolved to.
300-
pub def: Def,
299+
/// The resolution for the path.
300+
pub res: Res,
301301
/// The segments in the path: the things separated by `::`.
302302
pub segments: HirVec<PathSegment>,
303303
}
@@ -327,13 +327,13 @@ pub struct PathSegment {
327327
/// The identifier portion of this path segment.
328328
#[stable_hasher(project(name))]
329329
pub ident: Ident,
330-
// `id` and `def` are optional. We currently only use these in save-analysis,
330+
// `id` and `res` are optional. We currently only use these in save-analysis,
331331
// any path segments without these will not have save-analysis info and
332332
// therefore will not have 'jump to def' in IDEs, but otherwise will not be
333333
// affected. (In general, we don't bother to get the defs for synthesized
334334
// segments, only for segments which have come from the AST).
335335
pub hir_id: Option<HirId>,
336-
pub def: Option<Def>,
336+
pub res: Option<Res>,
337337

338338
/// Type/lifetime parameters attached to this path. They come in
339339
/// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
@@ -355,7 +355,7 @@ impl PathSegment {
355355
PathSegment {
356356
ident,
357357
hir_id: None,
358-
def: None,
358+
res: None,
359359
infer_types: true,
360360
args: None,
361361
}
@@ -364,14 +364,14 @@ impl PathSegment {
364364
pub fn new(
365365
ident: Ident,
366366
hir_id: Option<HirId>,
367-
def: Option<Def>,
367+
res: Option<Res>,
368368
args: GenericArgs,
369369
infer_types: bool,
370370
) -> Self {
371371
PathSegment {
372372
ident,
373373
hir_id,
374-
def,
374+
res,
375375
infer_types,
376376
args: if args.is_empty() {
377377
None
@@ -1393,8 +1393,11 @@ impl Expr {
13931393
pub fn is_place_expr(&self) -> bool {
13941394
match self.node {
13951395
ExprKind::Path(QPath::Resolved(_, ref path)) => {
1396-
match path.def {
1397-
Def::Local(..) | Def::Upvar(..) | Def::Static(..) | Def::Err => true,
1396+
match path.res {
1397+
Res::Local(..)
1398+
| Res::Upvar(..)
1399+
| Res::Def(DefKind::Static, _)
1400+
| Res::Err => true,
13981401
_ => false,
13991402
}
14001403
}
@@ -2139,7 +2142,7 @@ pub enum UseKind {
21392142
/// resolve maps each TraitRef's ref_id to its defining trait; that's all
21402143
/// that the ref_id is for. Note that ref_id's value is not the NodeId of the
21412144
/// trait being referred to but just a unique NodeId that serves as a key
2142-
/// within the DefMap.
2145+
/// within the ResMap.
21432146
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable)]
21442147
pub struct TraitRef {
21452148
pub path: Path,
@@ -2151,10 +2154,10 @@ pub struct TraitRef {
21512154
impl TraitRef {
21522155
/// Gets the `DefId` of the referenced trait. It _must_ actually be a trait or trait alias.
21532156
pub fn trait_def_id(&self) -> DefId {
2154-
match self.path.def {
2155-
Def::Trait(did) => did,
2156-
Def::TraitAlias(did) => did,
2157-
Def::Err => {
2157+
match self.path.res {
2158+
Res::Def(DefKind::Trait, did) => did,
2159+
Res::Def(DefKind::TraitAlias, did) => did,
2160+
Res::Err => {
21582161
FatalError.raise();
21592162
}
21602163
_ => unreachable!(),
@@ -2476,7 +2479,7 @@ impl ForeignItemKind {
24762479
#[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
24772480
pub struct Freevar<Id = HirId> {
24782481
/// The variable being accessed free.
2479-
pub def: def::Def<Id>,
2482+
pub res: Res<Id>,
24802483

24812484
// First span where it is accessed (there can be multiple).
24822485
pub span: Span
@@ -2485,15 +2488,15 @@ pub struct Freevar<Id = HirId> {
24852488
impl<Id: fmt::Debug + Copy> Freevar<Id> {
24862489
pub fn map_id<R>(self, map: impl FnMut(Id) -> R) -> Freevar<R> {
24872490
Freevar {
2488-
def: self.def.map_id(map),
2491+
res: self.res.map_id(map),
24892492
span: self.span,
24902493
}
24912494
}
24922495

24932496
pub fn var_id(&self) -> Id {
2494-
match self.def {
2495-
Def::Local(id) | Def::Upvar(id, ..) => id,
2496-
_ => bug!("Freevar::var_id: bad def ({:?})", self.def)
2497+
match self.res {
2498+
Res::Local(id) | Res::Upvar(id, ..) => id,
2499+
_ => bug!("Freevar::var_id: bad res ({:?})", self.res)
24972500
}
24982501
}
24992502
}
@@ -2518,7 +2521,7 @@ pub type GlobMap = NodeMap<FxHashSet<Name>>;
25182521

25192522
pub fn provide(providers: &mut Providers<'_>) {
25202523
check_attr::provide(providers);
2521-
providers.describe_def = map::describe_def;
2524+
providers.def_kind = map::def_kind;
25222525
}
25232526

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

src/librustc/hir/pat_util.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::hir::def::{CtorOf, Def};
1+
use crate::hir::def::{CtorOf, Res, DefKind};
22
use crate::hir::def_id::DefId;
33
use crate::hir::{self, HirId, PatKind};
44
use syntax::ast;
@@ -54,8 +54,8 @@ impl hir::Pat {
5454
PatKind::Path(hir::QPath::Resolved(_, ref path)) |
5555
PatKind::TupleStruct(hir::QPath::Resolved(_, ref path), ..) |
5656
PatKind::Struct(hir::QPath::Resolved(_, ref path), ..) => {
57-
match path.def {
58-
Def::Variant(..) => true,
57+
match path.res {
58+
Res::Def(DefKind::Variant, _) => true,
5959
_ => false
6060
}
6161
}
@@ -124,9 +124,9 @@ impl hir::Pat {
124124
PatKind::Path(hir::QPath::Resolved(_, ref path)) |
125125
PatKind::TupleStruct(hir::QPath::Resolved(_, ref path), ..) |
126126
PatKind::Struct(hir::QPath::Resolved(_, ref path), ..) => {
127-
match path.def {
128-
Def::Variant(id) => variants.push(id),
129-
Def::Ctor(id, CtorOf::Variant, ..) => variants.push(id),
127+
match path.res {
128+
Res::Def(DefKind::Variant, id) => variants.push(id),
129+
Res::Def(DefKind::Ctor(CtorOf::Variant, ..), id) => variants.push(id),
130130
_ => ()
131131
}
132132
}

src/librustc/lint/internal.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyTyKind {
170170

171171
fn lint_ty_kind_usage(cx: &LateContext<'_, '_>, segment: &PathSegment) -> bool {
172172
if segment.ident.as_str() == "TyKind" {
173-
if let Some(def) = segment.def {
174-
if let Some(did) = def.opt_def_id() {
173+
if let Some(res) = segment.res {
174+
if let Some(did) = res.opt_def_id() {
175175
return cx.match_def_path(did, &["rustc", "ty", "sty", "TyKind"]);
176176
}
177177
}
@@ -184,7 +184,7 @@ fn is_ty_or_ty_ctxt(cx: &LateContext<'_, '_>, ty: &Ty) -> Option<String> {
184184
match &ty.node {
185185
TyKind::Path(qpath) => {
186186
if let QPath::Resolved(_, path) = qpath {
187-
let did = path.def.opt_def_id()?;
187+
let did = path.res.opt_def_id()?;
188188
if cx.match_def_path(did, &["rustc", "ty", "Ty"]) {
189189
return Some(format!("Ty{}", gen_args(path.segments.last().unwrap())));
190190
} else if cx.match_def_path(did, &["rustc", "ty", "context", "TyCtxt"]) {

0 commit comments

Comments
 (0)