Skip to content

Commit

Permalink
Always create a root definition when creating a new Definitions obj…
Browse files Browse the repository at this point in the history
…ect.
  • Loading branch information
marmeladema committed Jun 21, 2020
1 parent f60513e commit 1d3f49f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 31 deletions.
14 changes: 7 additions & 7 deletions src/librustc_ast_lowering/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub trait Resolver {

fn local_def_id(&self, node: NodeId) -> LocalDefId;

fn create_def_with_parent(
fn create_def(
&mut self,
parent: LocalDefId,
node_id: ast::NodeId,
Expand Down Expand Up @@ -449,7 +449,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
match tree.kind {
UseTreeKind::Simple(_, id1, id2) => {
for &id in &[id1, id2] {
self.lctx.resolver.create_def_with_parent(
self.lctx.resolver.create_def(
owner,
id,
DefPathData::Misc,
Expand Down Expand Up @@ -696,7 +696,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

*local_id_counter += 1;
let owner = this.resolver.opt_local_def_id(owner).expect(
"you forgot to call `create_def_with_parent` or are lowering node-IDs \
"you forgot to call `create_def` or are lowering node-IDs \
that do not belong to the current owner",
);

Expand Down Expand Up @@ -824,7 +824,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
};

// Add a definition for the in-band lifetime def.
self.resolver.create_def_with_parent(
self.resolver.create_def(
parent_def_id,
node_id,
DefPathData::LifetimeNs(str_name),
Expand Down Expand Up @@ -1112,7 +1112,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

let impl_trait_node_id = self.resolver.next_node_id();
let parent_def_id = self.current_hir_id_owner.last().unwrap().0;
self.resolver.create_def_with_parent(
self.resolver.create_def(
parent_def_id,
impl_trait_node_id,
DefPathData::ImplTrait,
Expand Down Expand Up @@ -1178,7 +1178,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let node_id = self.resolver.next_node_id();

// Add a definition for the in-band const def.
self.resolver.create_def_with_parent(
self.resolver.create_def(
parent_def_id,
node_id,
DefPathData::AnonConst,
Expand Down Expand Up @@ -1644,7 +1644,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let def_node_id = self.context.resolver.next_node_id();
let hir_id =
self.context.lower_node_id_with_owner(def_node_id, self.opaque_ty_id);
self.context.resolver.create_def_with_parent(
self.context.resolver.create_def(
self.parent,
def_node_id,
DefPathData::LifetimeNs(name.ident().name),
Expand Down
40 changes: 23 additions & 17 deletions src/librustc_hir/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ impl DefPathTable {
}

/// The definition table containing node definitions.
/// It holds the `DefPathTable` for local `DefId`s/`DefPath`s and it also stores a
/// mapping from `NodeId`s to local `DefId`s.
#[derive(Clone, Default)]
/// It holds the `DefPathTable` for `LocalDefId`s/`DefPath`s.
/// It also stores mappings to convert `LocalDefId`s to/from `HirId`s.
#[derive(Clone)]
pub struct Definitions {
table: DefPathTable,

Expand Down Expand Up @@ -328,11 +328,7 @@ impl Definitions {
}

/// Adds a root definition (no parent) and a few other reserved definitions.
pub fn create_root_def(
&mut self,
crate_name: &str,
crate_disambiguator: CrateDisambiguator,
) -> LocalDefId {
pub fn new(crate_name: &str, crate_disambiguator: CrateDisambiguator) -> Definitions {
let key = DefKey {
parent: None,
disambiguated_data: DisambiguatedDefPathData {
Expand All @@ -344,24 +340,34 @@ impl Definitions {
let parent_hash = DefKey::root_parent_stable_hash(crate_name, crate_disambiguator);
let def_path_hash = key.compute_stable_hash(parent_hash);

// Create the definition.
let root = LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) };
// Create the root definition.
let mut table = DefPathTable::default();
let root = LocalDefId { local_def_index: table.allocate(key, def_path_hash) };
assert_eq!(root.local_def_index, CRATE_DEF_INDEX);

root
Definitions {
table,
def_id_to_hir_id: Default::default(),
hir_id_to_def_id: Default::default(),
expansions_that_defined: Default::default(),
next_disambiguator: Default::default(),
parent_modules_of_macro_defs: Default::default(),
}
}

/// Retrieves the root definition.
pub fn get_root_def(&self) -> LocalDefId {
LocalDefId { local_def_index: CRATE_DEF_INDEX }
}

/// Adds a definition with a parent definition.
pub fn create_def_with_parent(
pub fn create_def(
&mut self,
parent: LocalDefId,
data: DefPathData,
expn_id: ExpnId,
) -> LocalDefId {
debug!(
"create_def_with_parent(parent={:?}, data={:?}, expn_id={:?})",
parent, data, expn_id
);
debug!("create_def(parent={:?}, data={:?}, expn_id={:?})", parent, data, expn_id);

// The root node must be created with `create_root_def()`.
assert!(data != DefPathData::CrateRoot);
Expand All @@ -382,7 +388,7 @@ impl Definitions {
let parent_hash = self.table.def_path_hash(parent.local_def_index);
let def_path_hash = key.compute_stable_hash(parent_hash);

debug!("create_def_with_parent: after disambiguation, key = {:?}", key);
debug!("create_def: after disambiguation, key = {:?}", key);

// Create the definition.
let def_id = LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) };
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,10 @@ pub fn create_global_ctxt<'tcx>(
arena: &'tcx WorkerLocal<Arena<'tcx>>,
) -> QueryContext<'tcx> {
let sess = &compiler.session();
let defs: &'tcx Definitions = arena.alloc(mem::take(&mut resolver_outputs.definitions));
let defs: &'tcx Definitions = arena.alloc(mem::replace(
&mut resolver_outputs.definitions,
Definitions::new(crate_name, sess.local_crate_disambiguator()),
));

let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_resolve/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'a, 'b> DefCollector<'a, 'b> {
fn create_def(&mut self, node_id: NodeId, data: DefPathData, span: Span) -> LocalDefId {
let parent_def = self.parent_def;
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
self.resolver.create_def_with_parent(parent_def, node_id, data, self.expansion, span)
self.resolver.create_def(parent_def, node_id, data, self.expansion, span)
}

fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: LocalDefId, f: F) {
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ impl ResolverAstLowering for Resolver<'_> {
}

/// Adds a definition with a parent definition.
fn create_def_with_parent(
fn create_def(
&mut self,
parent: LocalDefId,
node_id: ast::NodeId,
Expand All @@ -1142,15 +1142,15 @@ impl ResolverAstLowering for Resolver<'_> {
self.definitions.def_key(self.node_id_to_def_id[&node_id]),
);

let def_id = self.definitions.create_def_with_parent(parent, data, expn_id);
let def_id = self.definitions.create_def(parent, data, expn_id);

assert_eq!(self.def_id_to_span.push(span), def_id);

// Some things for which we allocate `LocalDefId`s don't correspond to
// anything in the AST, so they don't have a `NodeId`. For these cases
// we don't need a mapping from `NodeId` to `LocalDefId`.
if node_id != ast::DUMMY_NODE_ID {
debug!("create_def_with_parent: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
self.node_id_to_def_id.insert(node_id, def_id);
}
assert_eq!(self.def_id_to_node_id.push(node_id), def_id);
Expand Down Expand Up @@ -1187,8 +1187,8 @@ impl<'a> Resolver<'a> {
let mut module_map = FxHashMap::default();
module_map.insert(LocalDefId { local_def_index: CRATE_DEF_INDEX }, graph_root);

let mut definitions = Definitions::default();
let root = definitions.create_root_def(crate_name, session.local_crate_disambiguator());
let definitions = Definitions::new(crate_name, session.local_crate_disambiguator());
let root = definitions.get_root_def();

let mut def_id_to_span = IndexVec::default();
assert_eq!(def_id_to_span.push(rustc_span::DUMMY_SP), root);
Expand Down

0 comments on commit 1d3f49f

Please sign in to comment.