From 16e25f0ea3cb93cac85f6107ce62a5d4adbb3e84 Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Sun, 3 Nov 2019 14:36:59 +0200 Subject: [PATCH] rustc: use LocalDefId instead of DefIndex in hir::map::definitions. --- src/librustc/hir/map/collector.rs | 27 ++- src/librustc/hir/map/definitions.rs | 158 +++++++++--------- src/librustc/hir/map/mod.rs | 21 ++- src/librustc/ich/hcx.rs | 2 +- src/librustc/ty/context.rs | 11 +- src/librustc/ty/mod.rs | 6 +- src/librustc_ast_lowering/lib.rs | 23 +-- src/librustc_metadata/creader.rs | 4 +- src/librustc_metadata/rmeta/encoder.rs | 4 +- .../borrow_check/type_check/mod.rs | 4 +- src/librustc_resolve/def_collector.rs | 8 +- src/librustc_resolve/lib.rs | 14 +- 12 files changed, 137 insertions(+), 145 deletions(-) diff --git a/src/librustc/hir/map/collector.rs b/src/librustc/hir/map/collector.rs index 286de7703abb1..de0cc61118fed 100644 --- a/src/librustc/hir/map/collector.rs +++ b/src/librustc/hir/map/collector.rs @@ -98,7 +98,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { definitions: &'a definitions::Definitions, mut hcx: StableHashingContext<'a>, ) -> NodeCollector<'a, 'hir> { - let root_mod_def_path_hash = definitions.def_path_hash(CRATE_DEF_INDEX); + let root_mod_def_path_hash = + definitions.def_path_hash(LocalDefId { local_def_index: CRATE_DEF_INDEX }); let mut hir_body_nodes = Vec::new(); @@ -244,8 +245,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { assert_eq!(self.definitions.node_to_hir_id(node_id), hir_id); if hir_id.owner != self.current_dep_node_owner { - let node_str = match self.definitions.opt_def_index(node_id) { - Some(def_index) => self.definitions.def_path(def_index).to_string_no_crate(), + let node_str = match self.definitions.opt_local_def_id(node_id) { + Some(def_id) => self.definitions.def_path(def_id).to_string_no_crate(), None => format!("{:?}", node), }; @@ -261,11 +262,9 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?}){}", self.source_map.span_to_string(span), node_str, - self.definitions - .def_path(self.current_dep_node_owner.local_def_index) - .to_string_no_crate(), + self.definitions.def_path(self.current_dep_node_owner).to_string_no_crate(), self.current_dep_node_owner, - self.definitions.def_path(hir_id.owner.local_def_index).to_string_no_crate(), + self.definitions.def_path(hir_id.owner).to_string_no_crate(), hir_id.owner, forgot_str, ) @@ -293,7 +292,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { ) { let prev_owner = self.current_dep_node_owner; - let def_path_hash = self.definitions.def_path_hash(dep_node_owner.local_def_index); + let def_path_hash = self.definitions.def_path_hash(dep_node_owner); let hash = hash_body(&mut self.hcx, def_path_hash, item_like, &mut self.hir_body_nodes); @@ -342,8 +341,8 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { fn visit_item(&mut self, i: &'hir Item<'hir>) { debug!("visit_item: {:?}", i); debug_assert_eq!( - i.hir_id.owner.local_def_index, - self.definitions.opt_def_index(self.definitions.hir_to_node_id(i.hir_id)).unwrap() + i.hir_id.owner, + self.definitions.opt_local_def_id(self.definitions.hir_to_node_id(i.hir_id)).unwrap() ); self.with_dep_node_owner(i.hir_id.owner, i, |this, hash| { this.insert_with_hash(i.span, i.hir_id, Node::Item(i), hash); @@ -374,8 +373,8 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) { debug_assert_eq!( - ti.hir_id.owner.local_def_index, - self.definitions.opt_def_index(self.definitions.hir_to_node_id(ti.hir_id)).unwrap() + ti.hir_id.owner, + self.definitions.opt_local_def_id(self.definitions.hir_to_node_id(ti.hir_id)).unwrap() ); self.with_dep_node_owner(ti.hir_id.owner, ti, |this, hash| { this.insert_with_hash(ti.span, ti.hir_id, Node::TraitItem(ti), hash); @@ -388,8 +387,8 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) { debug_assert_eq!( - ii.hir_id.owner.local_def_index, - self.definitions.opt_def_index(self.definitions.hir_to_node_id(ii.hir_id)).unwrap() + ii.hir_id.owner, + self.definitions.opt_local_def_id(self.definitions.hir_to_node_id(ii.hir_id)).unwrap() ); self.with_dep_node_owner(ii.hir_id.owner, ii, |this, hash| { this.insert_with_hash(ii.span, ii.hir_id, Node::ImplItem(ii), hash); diff --git a/src/librustc/hir/map/definitions.rs b/src/librustc/hir/map/definitions.rs index 42ccf7e72504b..aa4742ea891bb 100644 --- a/src/librustc/hir/map/definitions.rs +++ b/src/librustc/hir/map/definitions.rs @@ -5,11 +5,10 @@ //! expressions) that are mostly just leftovers. use rustc_ast::ast; -use rustc_ast::node_id::NodeMap; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::stable_hasher::StableHasher; use rustc_hir as hir; -use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_index::vec::IndexVec; use rustc_session::CrateDisambiguator; use rustc_span::hygiene::ExpnId; @@ -78,25 +77,29 @@ impl DefPathTable { #[derive(Clone, Default)] pub struct Definitions { table: DefPathTable, - node_to_def_index: NodeMap, - def_index_to_node: IndexVec, - pub(super) node_to_hir_id: IndexVec, - /// The reverse mapping of `node_to_hir_id`. - pub(super) hir_to_node_id: FxHashMap, + def_id_to_span: IndexVec, + + // FIXME(eddyb) don't go through `ast::NodeId` to convert between `HirId` + // and `LocalDefId` - ideally all `LocalDefId`s would be HIR owners. + node_id_to_def_id: FxHashMap, + def_id_to_node_id: IndexVec, + + pub(super) node_id_to_hir_id: IndexVec, + /// The reverse mapping of `node_id_to_hir_id`. + pub(super) hir_id_to_node_id: FxHashMap, /// If `ExpnId` is an ID of some macro expansion, /// then `DefId` is the normal module (`mod`) in which the expanded macro was defined. parent_modules_of_macro_defs: FxHashMap, - /// Item with a given `DefIndex` was defined during macro expansion with ID `ExpnId`. - expansions_that_defined: FxHashMap, - next_disambiguator: FxHashMap<(DefIndex, DefPathData), u32>, - def_index_to_span: FxHashMap, + /// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`. + expansions_that_defined: FxHashMap, + next_disambiguator: FxHashMap<(LocalDefId, DefPathData), u32>, /// When collecting definitions from an AST fragment produced by a macro invocation `ExpnId` /// we know what parent node that fragment should be attached to thanks to this table. - invocation_parents: FxHashMap, + invocation_parents: FxHashMap, /// Indices of unnamed struct or variant fields with unresolved attributes. - placeholder_field_indices: NodeMap, + placeholder_field_indices: FxHashMap, } /// A unique identifier that we can use to lookup a definition @@ -296,13 +299,13 @@ impl Definitions { self.table.index_to_key.len() } - pub fn def_key(&self, index: DefIndex) -> DefKey { - self.table.def_key(index) + pub fn def_key(&self, id: LocalDefId) -> DefKey { + self.table.def_key(id.local_def_index) } #[inline(always)] - pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash { - self.table.def_path_hash(index) + pub fn def_path_hash(&self, id: LocalDefId) -> DefPathHash { + self.table.def_path_hash(id.local_def_index) } /// Returns the path from the crate root to `index`. The root @@ -310,29 +313,27 @@ impl Definitions { /// empty vector for the crate root). For an inlined item, this /// will be the path of the item in the external crate (but the /// path will begin with the path to the external crate). - pub fn def_path(&self, index: DefIndex) -> DefPath { - DefPath::make(LOCAL_CRATE, index, |p| self.def_key(p)) + pub fn def_path(&self, id: LocalDefId) -> DefPath { + DefPath::make(LOCAL_CRATE, id.local_def_index, |index| { + self.def_key(LocalDefId { local_def_index: index }) + }) } #[inline] - pub fn opt_def_index(&self, node: ast::NodeId) -> Option { - self.node_to_def_index.get(&node).copied() - } - - #[inline] - pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option { - self.opt_def_index(node).map(DefId::local) + pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option { + self.node_id_to_def_id.get(&node).copied() } + // FIXME(eddyb) this function can and should return `LocalDefId`. #[inline] pub fn local_def_id(&self, node: ast::NodeId) -> DefId { - self.opt_local_def_id(node).unwrap() + self.opt_local_def_id(node).unwrap().to_def_id() } #[inline] pub fn as_local_node_id(&self, def_id: DefId) -> Option { - if def_id.krate == LOCAL_CRATE { - let node_id = self.def_index_to_node[def_id.index]; + if let Some(def_id) = def_id.as_local() { + let node_id = self.def_id_to_node_id[def_id]; if node_id != ast::DUMMY_NODE_ID { return Some(node_id); } @@ -342,39 +343,36 @@ impl Definitions { #[inline] pub fn as_local_hir_id(&self, def_id: DefId) -> Option { - if def_id.krate == LOCAL_CRATE { - let hir_id = self.def_index_to_hir_id(def_id.index); + if let Some(def_id) = def_id.as_local() { + let hir_id = self.local_def_id_to_hir_id(def_id); if hir_id != hir::DUMMY_HIR_ID { Some(hir_id) } else { None } } else { None } } + // FIXME(eddyb) rename to `hir_id_to_node_id`. #[inline] pub fn hir_to_node_id(&self, hir_id: hir::HirId) -> ast::NodeId { - self.hir_to_node_id[&hir_id] + self.hir_id_to_node_id[&hir_id] } + // FIXME(eddyb) rename to `node_id_to_hir_id`. #[inline] pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId { - self.node_to_hir_id[node_id] + self.node_id_to_hir_id[node_id] } #[inline] - pub fn def_index_to_hir_id(&self, def_index: DefIndex) -> hir::HirId { - let node_id = self.def_index_to_node[def_index]; - self.node_to_hir_id[node_id] + pub fn local_def_id_to_hir_id(&self, id: LocalDefId) -> hir::HirId { + let node_id = self.def_id_to_node_id[id]; + self.node_id_to_hir_id[node_id] } - /// Retrieves the span of the given `DefId` if `DefId` is in the local crate, the span exists - /// and it's not `DUMMY_SP`. + /// Retrieves the span of the given `DefId` if `DefId` is in the local crate. #[inline] pub fn opt_span(&self, def_id: DefId) -> Option { - if def_id.krate == LOCAL_CRATE { - self.def_index_to_span.get(&def_id.index).copied() - } else { - None - } + if let Some(def_id) = def_id.as_local() { Some(self.def_id_to_span[def_id]) } else { None } } /// Adds a root definition (no parent) and a few other reserved definitions. @@ -382,7 +380,7 @@ impl Definitions { &mut self, crate_name: &str, crate_disambiguator: CrateDisambiguator, - ) -> DefIndex { + ) -> LocalDefId { let key = DefKey { parent: None, disambiguated_data: DisambiguatedDefPathData { @@ -395,36 +393,38 @@ impl Definitions { let def_path_hash = key.compute_stable_hash(parent_hash); // Create the definition. - let root_index = self.table.allocate(key, def_path_hash); - assert_eq!(root_index, CRATE_DEF_INDEX); - assert!(self.def_index_to_node.is_empty()); - self.def_index_to_node.push(ast::CRATE_NODE_ID); - self.node_to_def_index.insert(ast::CRATE_NODE_ID, root_index); - self.set_invocation_parent(ExpnId::root(), root_index); + let root = LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) }; + assert_eq!(root.local_def_index, CRATE_DEF_INDEX); + + assert_eq!(self.def_id_to_node_id.push(ast::CRATE_NODE_ID), root); + assert_eq!(self.def_id_to_span.push(rustc_span::DUMMY_SP), root); + + self.node_id_to_def_id.insert(ast::CRATE_NODE_ID, root); + self.set_invocation_parent(ExpnId::root(), root); - root_index + root } /// Adds a definition with a parent definition. pub fn create_def_with_parent( &mut self, - parent: DefIndex, + parent: LocalDefId, node_id: ast::NodeId, data: DefPathData, expn_id: ExpnId, span: Span, - ) -> DefIndex { + ) -> LocalDefId { debug!( "create_def_with_parent(parent={:?}, node_id={:?}, data={:?})", parent, node_id, data ); assert!( - !self.node_to_def_index.contains_key(&node_id), + !self.node_id_to_def_id.contains_key(&node_id), "adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}", node_id, data, - self.table.def_key(self.node_to_def_index[&node_id]) + self.table.def_key(self.node_id_to_def_id[&node_id].local_def_index), ); // The root node must be created with `create_root_def()`. @@ -439,59 +439,55 @@ impl Definitions { }; let key = DefKey { - parent: Some(parent), + parent: Some(parent.local_def_index), disambiguated_data: DisambiguatedDefPathData { data, disambiguator }, }; - let parent_hash = self.table.def_path_hash(parent); + 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); // Create the definition. - let index = self.table.allocate(key, def_path_hash); - assert_eq!(index.index(), self.def_index_to_node.len()); - self.def_index_to_node.push(node_id); + let def_id = LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) }; - // Some things for which we allocate `DefIndex`es don't correspond to + assert_eq!(self.def_id_to_node_id.push(node_id), def_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 `DefIndex`. + // we don't need a mapping from `NodeId` to `LocalDefId`. if node_id != ast::DUMMY_NODE_ID { - debug!("create_def_with_parent: def_index_to_node[{:?} <-> {:?}", index, node_id); - self.node_to_def_index.insert(node_id, index); + debug!("create_def_with_parent: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id); + self.node_id_to_def_id.insert(node_id, def_id); } if expn_id != ExpnId::root() { - self.expansions_that_defined.insert(index, expn_id); - } - - // The span is added if it isn't dummy. - if !span.is_dummy() { - self.def_index_to_span.insert(index, span); + self.expansions_that_defined.insert(def_id, expn_id); } - index + def_id } /// Initializes the `ast::NodeId` to `HirId` mapping once it has been generated during /// AST to HIR lowering. pub fn init_node_id_to_hir_id_mapping(&mut self, mapping: IndexVec) { assert!( - self.node_to_hir_id.is_empty(), + self.node_id_to_hir_id.is_empty(), "trying to initialize `NodeId` -> `HirId` mapping twice" ); - self.node_to_hir_id = mapping; + self.node_id_to_hir_id = mapping; - // Build the reverse mapping of `node_to_hir_id`. - self.hir_to_node_id = self - .node_to_hir_id + // Build the reverse mapping of `node_id_to_hir_id`. + self.hir_id_to_node_id = self + .node_id_to_hir_id .iter_enumerated() .map(|(node_id, &hir_id)| (hir_id, node_id)) .collect(); } - pub fn expansion_that_defined(&self, index: DefIndex) -> ExpnId { - self.expansions_that_defined.get(&index).copied().unwrap_or(ExpnId::root()) + pub fn expansion_that_defined(&self, id: LocalDefId) -> ExpnId { + self.expansions_that_defined.get(&id).copied().unwrap_or(ExpnId::root()) } pub fn parent_module_of_macro_def(&self, expn_id: ExpnId) -> DefId { @@ -502,13 +498,13 @@ impl Definitions { self.parent_modules_of_macro_defs.insert(expn_id, module); } - pub fn invocation_parent(&self, invoc_id: ExpnId) -> DefIndex { + pub fn invocation_parent(&self, invoc_id: ExpnId) -> LocalDefId { self.invocation_parents[&invoc_id] } - pub fn set_invocation_parent(&mut self, invoc_id: ExpnId, parent: DefIndex) { + pub fn set_invocation_parent(&mut self, invoc_id: ExpnId, parent: LocalDefId) { let old_parent = self.invocation_parents.insert(invoc_id, parent); - assert!(old_parent.is_none(), "parent `DefIndex` is reset for an invocation"); + assert!(old_parent.is_none(), "parent `LocalDefId` is reset for an invocation"); } pub fn placeholder_field_index(&self, node_id: ast::NodeId) -> usize { diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 47114feb5e707..36cb19c6c37c7 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -9,7 +9,7 @@ use crate::ty::TyCtxt; use rustc_ast::ast::{self, Name, NodeId}; use rustc_data_structures::svh::Svh; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, LOCAL_CRATE}; +use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE}; use rustc_hir::intravisit; use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_hir::print::Nested; @@ -189,7 +189,7 @@ impl<'hir> Map<'hir> { } pub fn def_key(&self, def_id: LocalDefId) -> DefKey { - self.tcx.definitions.def_key(def_id.local_def_index) + self.tcx.definitions.def_key(def_id) } pub fn def_path_from_hir_id(&self, id: HirId) -> Option { @@ -197,9 +197,10 @@ impl<'hir> Map<'hir> { } pub fn def_path(&self, def_id: LocalDefId) -> DefPath { - self.tcx.definitions.def_path(def_id.local_def_index) + self.tcx.definitions.def_path(def_id) } + // FIXME(eddyb) this function can and should return `LocalDefId`. #[inline] pub fn local_def_id_from_node_id(&self, node: NodeId) -> DefId { self.opt_local_def_id_from_node_id(node).unwrap_or_else(|| { @@ -212,6 +213,7 @@ impl<'hir> Map<'hir> { }) } + // FIXME(eddyb) this function can and should return `LocalDefId`. #[inline] pub fn local_def_id(&self, hir_id: HirId) -> DefId { self.opt_local_def_id(hir_id).unwrap_or_else(|| { @@ -226,12 +228,12 @@ impl<'hir> Map<'hir> { #[inline] pub fn opt_local_def_id(&self, hir_id: HirId) -> Option { let node_id = self.hir_to_node_id(hir_id); - self.tcx.definitions.opt_local_def_id(node_id) + self.opt_local_def_id_from_node_id(node_id) } #[inline] pub fn opt_local_def_id_from_node_id(&self, node: NodeId) -> Option { - self.tcx.definitions.opt_local_def_id(node) + Some(self.tcx.definitions.opt_local_def_id(node)?.to_def_id()) } #[inline] @@ -254,14 +256,9 @@ impl<'hir> Map<'hir> { self.tcx.definitions.node_to_hir_id(node_id) } - #[inline] - pub fn def_index_to_hir_id(&self, def_index: DefIndex) -> HirId { - self.tcx.definitions.def_index_to_hir_id(def_index) - } - #[inline] pub fn local_def_id_to_hir_id(&self, def_id: LocalDefId) -> HirId { - self.tcx.definitions.def_index_to_hir_id(def_id.local_def_index) + self.tcx.definitions.local_def_id_to_hir_id(def_id) } pub fn def_kind(&self, hir_id: HirId) -> Option { @@ -404,6 +401,7 @@ impl<'hir> Map<'hir> { parent } + // FIXME(eddyb) this function can and should return `LocalDefId`. pub fn body_owner_def_id(&self, id: BodyId) -> DefId { self.local_def_id(self.body_owner(id)) } @@ -776,6 +774,7 @@ impl<'hir> Map<'hir> { scope } + // FIXME(eddyb) this function can and should return `LocalDefId`. pub fn get_parent_did(&self, id: HirId) -> DefId { self.local_def_id(self.get_parent_item(id)) } diff --git a/src/librustc/ich/hcx.rs b/src/librustc/ich/hcx.rs index 34b41c32dab5c..ba3763f6ee0bd 100644 --- a/src/librustc/ich/hcx.rs +++ b/src/librustc/ich/hcx.rs @@ -132,7 +132,7 @@ impl<'a> StableHashingContext<'a> { #[inline] pub fn local_def_path_hash(&self, def_id: LocalDefId) -> DefPathHash { - self.definitions.def_path_hash(def_id.local_def_index) + self.definitions.def_path_hash(def_id) } #[inline] diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index 71265b2476fa1..742d57fb58a51 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -1288,7 +1288,7 @@ impl<'tcx> TyCtxt<'tcx> { #[inline] pub fn def_path_hash(self, def_id: DefId) -> hir_map::DefPathHash { if let Some(def_id) = def_id.as_local() { - self.definitions.def_path_hash(def_id.local_def_index) + self.definitions.def_path_hash(def_id) } else { self.cstore.def_path_hash(def_id) } @@ -2753,18 +2753,15 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) { }; providers.lookup_stability = |tcx, id| { - assert_eq!(id.krate, LOCAL_CRATE); - let id = tcx.hir().definitions().def_index_to_hir_id(id.index); + let id = tcx.hir().local_def_id_to_hir_id(id.expect_local()); tcx.stability().local_stability(id) }; providers.lookup_const_stability = |tcx, id| { - assert_eq!(id.krate, LOCAL_CRATE); - let id = tcx.hir().definitions().def_index_to_hir_id(id.index); + let id = tcx.hir().local_def_id_to_hir_id(id.expect_local()); tcx.stability().local_const_stability(id) }; providers.lookup_deprecation_entry = |tcx, id| { - assert_eq!(id.krate, LOCAL_CRATE); - let id = tcx.hir().definitions().def_index_to_hir_id(id.index); + let id = tcx.hir().local_def_id_to_hir_id(id.expect_local()); tcx.stability().local_deprecation_entry(id) }; providers.extern_mod_stmt_cnum = |tcx, id| { diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index ac680fc756807..9e3853c51af13 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -3096,9 +3096,9 @@ impl<'tcx> TyCtxt<'tcx> { } fn expansion_that_defined(self, scope: DefId) -> ExpnId { - match scope.krate { - LOCAL_CRATE => self.hir().definitions().expansion_that_defined(scope.index), - _ => ExpnId::root(), + match scope.as_local() { + Some(scope) => self.hir().definitions().expansion_that_defined(scope), + None => ExpnId::root(), } } diff --git a/src/librustc_ast_lowering/lib.rs b/src/librustc_ast_lowering/lib.rs index 7e9563833b3c3..dcedcd51f5063 100644 --- a/src/librustc_ast_lowering/lib.rs +++ b/src/librustc_ast_lowering/lib.rs @@ -412,7 +412,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { UseTreeKind::Simple(_, id1, id2) => { for &id in &[id1, id2] { self.lctx.resolver.definitions().create_def_with_parent( - owner.local_def_index, + owner, id, DefPathData::Misc, ExpnId::root(), @@ -642,15 +642,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { debug_assert!(local_id != HIR_ID_COUNTER_LOCKED); *local_id_counter += 1; - let owner = this - .resolver - .definitions() - .opt_local_def_id(owner) - .expect( - "you forgot to call `create_def_with_parent` or are lowering node-IDs \ - that do not belong to the current owner", - ) - .expect_local(); + let owner = this.resolver.definitions().opt_local_def_id(owner).expect( + "you forgot to call `create_def_with_parent` or are lowering node-IDs \ + that do not belong to the current owner", + ); hir::HirId { owner, local_id: hir::ItemLocalId::from_u32(local_id) } }) @@ -777,7 +772,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Add a definition for the in-band lifetime def. self.resolver.definitions().create_def_with_parent( - parent_def_id.local_def_index, + parent_def_id, node_id, DefPathData::LifetimeNs(str_name), ExpnId::root(), @@ -1056,7 +1051,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.definitions().create_def_with_parent( - parent_def_id.local_def_index, + parent_def_id, impl_trait_node_id, DefPathData::ImplTrait, ExpnId::root(), @@ -1122,7 +1117,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Add a definition for the in-band const def. self.resolver.definitions().create_def_with_parent( - parent_def_id.local_def_index, + parent_def_id, node_id, DefPathData::AnonConst, ExpnId::root(), @@ -1560,7 +1555,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let hir_id = self.context.lower_node_id_with_owner(def_node_id, self.opaque_ty_id); self.context.resolver.definitions().create_def_with_parent( - self.parent.local_def_index, + self.parent, def_node_id, DefPathData::LifetimeNs(name.ident().name), ExpnId::root(), diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index 9616e1249dc98..f20cdfcba15ca 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -870,11 +870,11 @@ impl<'a> CrateLoader<'a> { let cnum = self.resolve_crate(name, item.span, dep_kind, None); let def_id = definitions.opt_local_def_id(item.id).unwrap(); - let path_len = definitions.def_path(def_id.index).data.len(); + let path_len = definitions.def_path(def_id).data.len(); self.update_extern_crate( cnum, ExternCrate { - src: ExternCrateSource::Extern(def_id), + src: ExternCrateSource::Extern(def_id.to_def_id()), span: item.span, path_len, dependency_of: LOCAL_CRATE, diff --git a/src/librustc_metadata/rmeta/encoder.rs b/src/librustc_metadata/rmeta/encoder.rs index f4f6fd43e30f0..6280fd62de9a8 100644 --- a/src/librustc_metadata/rmeta/encoder.rs +++ b/src/librustc_metadata/rmeta/encoder.rs @@ -1438,8 +1438,8 @@ impl EncodeContext<'tcx> { .into_iter() .map(|(trait_def_id, mut impls)| { // Bring everything into deterministic order for hashing - impls.sort_by_cached_key(|&def_index| { - tcx.hir().definitions().def_path_hash(def_index) + impls.sort_by_cached_key(|&index| { + tcx.hir().definitions().def_path_hash(LocalDefId { local_def_index: index }) }); TraitImpls { diff --git a/src/librustc_mir/borrow_check/type_check/mod.rs b/src/librustc_mir/borrow_check/type_check/mod.rs index d98abc57bfd49..05d4fc8880e40 100644 --- a/src/librustc_mir/borrow_check/type_check/mod.rs +++ b/src/librustc_mir/borrow_check/type_check/mod.rs @@ -2008,7 +2008,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { &traits::Obligation::new( ObligationCause::new( span, - self.tcx().hir().def_index_to_hir_id(self.mir_def_id.index), + self.tcx() + .hir() + .local_def_id_to_hir_id(self.mir_def_id.expect_local()), traits::ObligationCauseCode::RepeatVec(should_suggest), ), self.param_env, diff --git a/src/librustc_resolve/def_collector.rs b/src/librustc_resolve/def_collector.rs index 16359cc743767..505cd331a2509 100644 --- a/src/librustc_resolve/def_collector.rs +++ b/src/librustc_resolve/def_collector.rs @@ -4,7 +4,7 @@ use rustc_ast::ast::*; use rustc_ast::token::{self, Token}; use rustc_ast::visit::{self, FnKind}; use rustc_expand::expand::AstFragment; -use rustc_hir::def_id::DefIndex; +use rustc_hir::def_id::LocalDefId; use rustc_span::hygiene::ExpnId; use rustc_span::symbol::{kw, sym}; use rustc_span::Span; @@ -21,18 +21,18 @@ crate fn collect_definitions( /// Creates `DefId`s for nodes in the AST. struct DefCollector<'a> { definitions: &'a mut Definitions, - parent_def: DefIndex, + parent_def: LocalDefId, expansion: ExpnId, } impl<'a> DefCollector<'a> { - fn create_def(&mut self, node_id: NodeId, data: DefPathData, span: Span) -> DefIndex { + 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.definitions.create_def_with_parent(parent_def, node_id, data, self.expansion, span) } - fn with_parent(&mut self, parent_def: DefIndex, f: F) { + fn with_parent(&mut self, parent_def: LocalDefId, f: F) { let orig_parent_def = std::mem::replace(&mut self.parent_def, parent_def); f(self); self.parent_def = orig_parent_def; diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 8164998d55fd7..4a2a2a296faea 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -37,7 +37,7 @@ use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder}; use rustc_expand::base::SyntaxExtension; use rustc_hir::def::Namespace::*; use rustc_hir::def::{self, CtorOf, DefKind, NonMacroAttrKind, PartialRes}; -use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX}; use rustc_hir::PrimTy::{self, Bool, Char, Float, Int, Str, Uint}; use rustc_hir::{GlobMap, TraitMap}; use rustc_metadata::creader::{CStore, CrateLoader}; @@ -1015,9 +1015,9 @@ impl<'a> AsMut> for Resolver<'a> { impl<'a, 'b> DefIdTree for &'a Resolver<'b> { fn parent(self, id: DefId) -> Option { - match id.krate { - LOCAL_CRATE => self.definitions.def_key(id.index).parent, - _ => self.cstore().def_key(id).parent, + match id.as_local() { + Some(id) => self.definitions.def_key(id).parent, + None => self.cstore().def_key(id).parent, } .map(|index| DefId { index, ..id }) } @@ -1027,7 +1027,11 @@ impl<'a, 'b> DefIdTree for &'a Resolver<'b> { /// the resolver is no longer needed as all the relevant information is inline. impl rustc_ast_lowering::Resolver for Resolver<'_> { fn def_key(&mut self, id: DefId) -> DefKey { - if id.is_local() { self.definitions().def_key(id.index) } else { self.cstore().def_key(id) } + if let Some(id) = id.as_local() { + self.definitions().def_key(id) + } else { + self.cstore().def_key(id) + } } fn item_generics_num_lifetimes(&self, def_id: DefId, sess: &Session) -> usize {