Skip to content

Commit 21f33db

Browse files
committed
Rollup merge of rust-lang#38418 - michaelwoerister:def_path_cleanup, r=eddyb
Cleanup refactoring around DefPath handling This PR makes two big changes: * All DefPaths of a crate are now stored in metadata in their own table (as opposed to `DefKey`s as part of metadata `Entry`s. * The compiler will no longer allocate a pseudo-local DefId for inlined HIR nodes (because those are gross). Inlined HIR nodes will have a NodeId but they don't have there own DefId anymore. Turns out they were not needed anymore either. Hopefully HIR inlining will be gone completely one day but if until then we start needing to be able to map inlined NodeIds to original DefIds, we can add an additional table to metadata that allows for reconstructing this. Overall this makes for some nice simplifications and removal of special cases. r? @eddyb cc @rust-lang/compiler
2 parents 2104111 + 3a82b0d commit 21f33db

23 files changed

+238
-532
lines changed

src/librustc/dep_graph/README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ to see something like:
418418

419419
Hir(foo) -> Collect(bar)
420420
Collect(bar) -> TypeckItemBody(bar)
421-
421+
422422
That first edge looks suspicious to you. So you set
423423
`RUST_FORBID_DEP_GRAPH_EDGE` to `Hir&foo -> Collect&bar`, re-run, and
424424
then observe the backtrace. Voila, bug fixed!
@@ -440,6 +440,4 @@ To achieve this, the HIR map will detect if the def-id originates in
440440
an inlined node and add a dependency to a suitable `MetaData` node
441441
instead. If you are reading a HIR node and are not sure if it may be
442442
inlined or not, you can use `tcx.map.read(node_id)` and it will detect
443-
whether the node is inlined or not and do the right thing. You can
444-
also use `tcx.map.is_inlined_def_id()` and
445-
`tcx.map.is_inlined_node_id()` to test.
443+
whether the node is inlined or not and do the right thing.

src/librustc/dep_graph/visit.rs

-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ pub fn visit_all_item_likes_in_krate<'a, 'tcx, V, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>
4040
let task_id = (self.dep_node_fn)(item_def_id);
4141
let _task = self.tcx.dep_graph.in_task(task_id.clone());
4242
debug!("Started task {:?}", task_id);
43-
assert!(!self.tcx.map.is_inlined_def_id(item_def_id));
4443
self.tcx.dep_graph.read(DepNode::Hir(item_def_id));
4544
self.visitor.visit_item(i);
4645
debug!("Ended task {:?}", task_id);
@@ -51,7 +50,6 @@ pub fn visit_all_item_likes_in_krate<'a, 'tcx, V, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>
5150
let task_id = (self.dep_node_fn)(impl_item_def_id);
5251
let _task = self.tcx.dep_graph.in_task(task_id.clone());
5352
debug!("Started task {:?}", task_id);
54-
assert!(!self.tcx.map.is_inlined_def_id(impl_item_def_id));
5553
self.tcx.dep_graph.read(DepNode::Hir(impl_item_def_id));
5654
self.visitor.visit_impl_item(i);
5755
debug!("Ended task {:?}", task_id);

src/librustc/hir/def_id.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ impl fmt::Debug for DefId {
120120

121121
ty::tls::with_opt(|opt_tcx| {
122122
if let Some(tcx) = opt_tcx {
123-
if let Some(def_path) = tcx.opt_def_path(*self) {
124-
write!(f, " => {}", def_path.to_string(tcx))?;
125-
}
123+
write!(f, " => {}", tcx.def_path(*self).to_string(tcx))?;
126124
}
127125
Ok(())
128126
})?;

src/librustc/hir/map/collector.rs

-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use super::*;
1212

1313
use hir::intravisit::{Visitor, NestedVisitorMap};
14-
use hir::def_id::DefId;
1514
use middle::cstore::InlinedItem;
1615
use std::iter::repeat;
1716
use syntax::ast::{NodeId, CRATE_NODE_ID};
@@ -47,8 +46,6 @@ impl<'ast> NodeCollector<'ast> {
4746
pub fn extend(krate: &'ast Crate,
4847
parent: &'ast InlinedItem,
4948
parent_node: NodeId,
50-
parent_def_path: DefPath,
51-
parent_def_id: DefId,
5249
map: Vec<MapEntry<'ast>>)
5350
-> NodeCollector<'ast> {
5451
let mut collector = NodeCollector {
@@ -58,7 +55,6 @@ impl<'ast> NodeCollector<'ast> {
5855
ignore_nested_items: true
5956
};
6057

61-
assert_eq!(parent_def_path.krate, parent_def_id.krate);
6258
collector.insert_entry(parent_node, RootInlinedParent(parent));
6359

6460
collector

src/librustc/hir/map/def_collector.rs

+1-212
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@
99
// except according to those terms.
1010

1111
use hir::map::definitions::*;
12-
13-
use hir;
14-
use hir::intravisit::{self, Visitor, NestedVisitorMap};
15-
use hir::def_id::{CRATE_DEF_INDEX, DefId, DefIndex};
16-
17-
use middle::cstore::InlinedItem;
12+
use hir::def_id::{CRATE_DEF_INDEX, DefIndex};
1813

1914
use syntax::ast::*;
2015
use syntax::ext::hygiene::Mark;
@@ -23,9 +18,6 @@ use syntax::symbol::{Symbol, keywords};
2318

2419
/// Creates def ids for nodes in the HIR.
2520
pub struct DefCollector<'a> {
26-
// If we are walking HIR (c.f., AST), we need to keep a reference to the
27-
// crate.
28-
hir_crate: Option<&'a hir::Crate>,
2921
definitions: &'a mut Definitions,
3022
parent_def: Option<DefIndex>,
3123
pub visit_macro_invoc: Option<&'a mut FnMut(MacroInvocationData)>,
@@ -40,43 +32,16 @@ pub struct MacroInvocationData {
4032
impl<'a> DefCollector<'a> {
4133
pub fn new(definitions: &'a mut Definitions) -> Self {
4234
DefCollector {
43-
hir_crate: None,
4435
definitions: definitions,
4536
parent_def: None,
4637
visit_macro_invoc: None,
4738
}
4839
}
4940

50-
pub fn extend(parent_node: NodeId,
51-
parent_def_path: DefPath,
52-
parent_def_id: DefId,
53-
definitions: &'a mut Definitions)
54-
-> Self {
55-
let mut collector = DefCollector::new(definitions);
56-
57-
assert_eq!(parent_def_path.krate, parent_def_id.krate);
58-
let root_path = Box::new(InlinedRootPath {
59-
data: parent_def_path.data,
60-
def_id: parent_def_id,
61-
});
62-
63-
let def = collector.create_def(parent_node, DefPathData::InlinedRoot(root_path));
64-
collector.parent_def = Some(def);
65-
66-
collector
67-
}
68-
6941
pub fn collect_root(&mut self) {
7042
let root = self.create_def_with_parent(None, CRATE_NODE_ID, DefPathData::CrateRoot);
7143
assert_eq!(root, CRATE_DEF_INDEX);
7244
self.parent_def = Some(root);
73-
74-
self.create_def_with_parent(Some(CRATE_DEF_INDEX), DUMMY_NODE_ID, DefPathData::Misc);
75-
}
76-
77-
pub fn walk_item(&mut self, ii: &'a InlinedItem, krate: &'a hir::Crate) {
78-
self.hir_crate = Some(krate);
79-
ii.visit(self);
8045
}
8146

8247
fn create_def(&mut self, node_id: NodeId, data: DefPathData) -> DefIndex {
@@ -114,16 +79,6 @@ impl<'a> DefCollector<'a> {
11479
self.create_def(expr.id, DefPathData::Initializer);
11580
}
11681

117-
fn visit_hir_const_integer(&mut self, expr: &hir::Expr) {
118-
// FIXME(eddyb) Closures should have separate
119-
// function definition IDs and expression IDs.
120-
if let hir::ExprClosure(..) = expr.node {
121-
return;
122-
}
123-
124-
self.create_def(expr.id, DefPathData::Initializer);
125-
}
126-
12782
fn visit_macro_invoc(&mut self, id: NodeId, const_integer: bool) {
12883
if let Some(ref mut visit) = self.visit_macro_invoc {
12984
visit(MacroInvocationData {
@@ -324,169 +279,3 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
324279
}
325280
}
326281
}
327-
328-
// We walk the HIR rather than the AST when reading items from metadata.
329-
impl<'ast> Visitor<'ast> for DefCollector<'ast> {
330-
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'ast> {
331-
// note however that we override `visit_body` below
332-
NestedVisitorMap::None
333-
}
334-
335-
fn visit_body(&mut self, id: hir::ExprId) {
336-
if let Some(krate) = self.hir_crate {
337-
self.visit_expr(krate.expr(id));
338-
}
339-
}
340-
341-
fn visit_item(&mut self, i: &'ast hir::Item) {
342-
debug!("visit_item: {:?}", i);
343-
344-
// Pick the def data. This need not be unique, but the more
345-
// information we encapsulate into
346-
let def_data = match i.node {
347-
hir::ItemDefaultImpl(..) | hir::ItemImpl(..) =>
348-
DefPathData::Impl,
349-
hir::ItemEnum(..) | hir::ItemStruct(..) | hir::ItemUnion(..) |
350-
hir::ItemTrait(..) | hir::ItemExternCrate(..) | hir::ItemMod(..) |
351-
hir::ItemForeignMod(..) | hir::ItemTy(..) =>
352-
DefPathData::TypeNs(i.name.as_str()),
353-
hir::ItemStatic(..) | hir::ItemConst(..) | hir::ItemFn(..) =>
354-
DefPathData::ValueNs(i.name.as_str()),
355-
hir::ItemUse(..) => DefPathData::Misc,
356-
};
357-
let def = self.create_def(i.id, def_data);
358-
359-
self.with_parent(def, |this| {
360-
match i.node {
361-
hir::ItemEnum(ref enum_definition, _) => {
362-
for v in &enum_definition.variants {
363-
let variant_def_index =
364-
this.create_def(v.node.data.id(),
365-
DefPathData::EnumVariant(v.node.name.as_str()));
366-
367-
this.with_parent(variant_def_index, |this| {
368-
for field in v.node.data.fields() {
369-
this.create_def(field.id,
370-
DefPathData::Field(field.name.as_str()));
371-
}
372-
if let Some(ref expr) = v.node.disr_expr {
373-
this.visit_hir_const_integer(expr);
374-
}
375-
});
376-
}
377-
}
378-
hir::ItemStruct(ref struct_def, _) |
379-
hir::ItemUnion(ref struct_def, _) => {
380-
// If this is a tuple-like struct, register the constructor.
381-
if !struct_def.is_struct() {
382-
this.create_def(struct_def.id(),
383-
DefPathData::StructCtor);
384-
}
385-
386-
for field in struct_def.fields() {
387-
this.create_def(field.id, DefPathData::Field(field.name.as_str()));
388-
}
389-
}
390-
_ => {}
391-
}
392-
intravisit::walk_item(this, i);
393-
});
394-
}
395-
396-
fn visit_foreign_item(&mut self, foreign_item: &'ast hir::ForeignItem) {
397-
let def = self.create_def(foreign_item.id,
398-
DefPathData::ValueNs(foreign_item.name.as_str()));
399-
400-
self.with_parent(def, |this| {
401-
intravisit::walk_foreign_item(this, foreign_item);
402-
});
403-
}
404-
405-
fn visit_generics(&mut self, generics: &'ast hir::Generics) {
406-
for ty_param in generics.ty_params.iter() {
407-
self.create_def(ty_param.id, DefPathData::TypeParam(ty_param.name.as_str()));
408-
}
409-
410-
intravisit::walk_generics(self, generics);
411-
}
412-
413-
fn visit_trait_item(&mut self, ti: &'ast hir::TraitItem) {
414-
let def_data = match ti.node {
415-
hir::MethodTraitItem(..) | hir::ConstTraitItem(..) =>
416-
DefPathData::ValueNs(ti.name.as_str()),
417-
hir::TypeTraitItem(..) => DefPathData::TypeNs(ti.name.as_str()),
418-
};
419-
420-
let def = self.create_def(ti.id, def_data);
421-
self.with_parent(def, |this| {
422-
if let hir::ConstTraitItem(_, Some(ref expr)) = ti.node {
423-
this.create_def(expr.id, DefPathData::Initializer);
424-
}
425-
426-
intravisit::walk_trait_item(this, ti);
427-
});
428-
}
429-
430-
fn visit_impl_item(&mut self, ii: &'ast hir::ImplItem) {
431-
let def_data = match ii.node {
432-
hir::ImplItemKind::Method(..) | hir::ImplItemKind::Const(..) =>
433-
DefPathData::ValueNs(ii.name.as_str()),
434-
hir::ImplItemKind::Type(..) => DefPathData::TypeNs(ii.name.as_str()),
435-
};
436-
437-
let def = self.create_def(ii.id, def_data);
438-
self.with_parent(def, |this| {
439-
if let hir::ImplItemKind::Const(_, ref expr) = ii.node {
440-
this.create_def(expr.id, DefPathData::Initializer);
441-
}
442-
443-
intravisit::walk_impl_item(this, ii);
444-
});
445-
}
446-
447-
fn visit_pat(&mut self, pat: &'ast hir::Pat) {
448-
let parent_def = self.parent_def;
449-
450-
if let hir::PatKind::Binding(_, _, name, _) = pat.node {
451-
let def = self.create_def(pat.id, DefPathData::Binding(name.node.as_str()));
452-
self.parent_def = Some(def);
453-
}
454-
455-
intravisit::walk_pat(self, pat);
456-
self.parent_def = parent_def;
457-
}
458-
459-
fn visit_expr(&mut self, expr: &'ast hir::Expr) {
460-
let parent_def = self.parent_def;
461-
462-
if let hir::ExprRepeat(_, ref count) = expr.node {
463-
self.visit_hir_const_integer(count);
464-
}
465-
466-
if let hir::ExprClosure(..) = expr.node {
467-
let def = self.create_def(expr.id, DefPathData::ClosureExpr);
468-
self.parent_def = Some(def);
469-
}
470-
471-
intravisit::walk_expr(self, expr);
472-
self.parent_def = parent_def;
473-
}
474-
475-
fn visit_ty(&mut self, ty: &'ast hir::Ty) {
476-
if let hir::TyArray(_, ref length) = ty.node {
477-
self.visit_hir_const_integer(length);
478-
}
479-
if let hir::TyImplTrait(..) = ty.node {
480-
self.create_def(ty.id, DefPathData::ImplTrait);
481-
}
482-
intravisit::walk_ty(self, ty);
483-
}
484-
485-
fn visit_lifetime_def(&mut self, def: &'ast hir::LifetimeDef) {
486-
self.create_def(def.lifetime.id, DefPathData::LifetimeDef(def.lifetime.name.as_str()));
487-
}
488-
489-
fn visit_macro_def(&mut self, macro_def: &'ast hir::MacroDef) {
490-
self.create_def(macro_def.id, DefPathData::MacroDef(macro_def.name.as_str()));
491-
}
492-
}

0 commit comments

Comments
 (0)