diff --git a/src/librustc/dep_graph/dep_node.rs b/src/librustc/dep_graph/dep_node.rs index cea790375fc3d..96ceb2ef33f4d 100644 --- a/src/librustc/dep_graph/dep_node.rs +++ b/src/librustc/dep_graph/dep_node.rs @@ -51,7 +51,7 @@ use crate::mir; use crate::mir::interpret::GlobalId; -use crate::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX}; +use crate::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LocalDefId}; use crate::hir::map::DefPathHash; use crate::hir::HirId; @@ -447,9 +447,9 @@ impl RecoverKey<'tcx> for DefId { } } -impl RecoverKey<'tcx> for DefIndex { +impl RecoverKey<'tcx> for LocalDefId { fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option { - dep_node.extract_def_id(tcx).map(|id| id.index) + dep_node.extract_def_id(tcx).map(|id| id.assert_local()) } } @@ -501,15 +501,15 @@ impl<'tcx> DepNodeParams<'tcx> for DefId { } } -impl<'tcx> DepNodeParams<'tcx> for DefIndex { +impl<'tcx> DepNodeParams<'tcx> for LocalDefId { const CAN_RECONSTRUCT_QUERY_KEY: bool = true; fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint { - tcx.hir().definitions().def_path_hash(*self).0 + self.to_def_id().to_fingerprint(tcx) } fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String { - tcx.def_path_str(DefId::local(*self)) + self.to_def_id().to_debug_str(tcx) } } @@ -565,7 +565,7 @@ impl<'tcx> DepNodeParams<'tcx> for HirId { local_id, } = *self; - let def_path_hash = tcx.def_path_hash(DefId::local(owner)); + let def_path_hash = tcx.def_path_hash(owner.to_def_id()); let local_id = Fingerprint::from_smaller_hash(local_id.as_u32().into()); def_path_hash.0.combine(local_id) diff --git a/src/librustc/hir/def_id.rs b/src/librustc/hir/def_id.rs index 13200b38f2cda..7d24f69158bc3 100644 --- a/src/librustc/hir/def_id.rs +++ b/src/librustc/hir/def_id.rs @@ -139,8 +139,21 @@ impl DefId { } #[inline] - pub fn to_local(self) -> LocalDefId { - LocalDefId::from_def_id(self) + pub fn as_local(self) -> Option { + if self.is_local() { + Some(LocalDefId { + index: self.index, + }) + } else { + None + } + } + + #[inline] + pub fn assert_local(self) -> LocalDefId { + self.as_local().unwrap_or_else(|| { + bug!("DefId::assert_local: `{:?}` isn't local", self) + }) } pub fn describe_as_module(&self, tcx: TyCtxt<'_>) -> String { @@ -161,21 +174,17 @@ impl rustc_serialize::UseSpecializedDecodable for DefId {} /// few cases where we know that only DefIds from the local crate are expected /// and a DefId from a different crate would signify a bug somewhere. This /// is when LocalDefId comes in handy. -#[derive(Clone, Copy, PartialEq, Eq, Hash)] -pub struct LocalDefId(DefIndex); +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct LocalDefId { + pub index: DefIndex, +} impl LocalDefId { - #[inline] - pub fn from_def_id(def_id: DefId) -> LocalDefId { - assert!(def_id.is_local()); - LocalDefId(def_id.index) - } - #[inline] pub fn to_def_id(self) -> DefId { DefId { krate: LOCAL_CRATE, - index: self.0 + index: self.index } } } diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index c8bb35202f518..42d50c4867d9d 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -39,7 +39,7 @@ use crate::dep_graph::DepGraph; use crate::hir::{self, ParamName}; use crate::hir::HirVec; use crate::hir::map::{DefKey, DefPathData, Definitions}; -use crate::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX}; +use crate::hir::def_id::{DefId, CRATE_DEF_INDEX, LocalDefId}; use crate::hir::def::{Namespace, Res, DefKind, PartialRes, PerNS}; use crate::hir::{GenericArg, ConstArg}; use crate::hir::ptr::P; @@ -149,7 +149,7 @@ pub struct LoweringContext<'a> { type_def_lifetime_params: DefIdMap, - current_hir_id_owner: Vec<(DefIndex, u32)>, + current_hir_id_owner: Vec<(LocalDefId, u32)>, item_local_id_counters: NodeMap, node_id_to_hir_id: IndexVec, @@ -273,7 +273,9 @@ pub fn lower_crate( anonymous_lifetime_mode: AnonymousLifetimeMode::PassThrough, type_def_lifetime_params: Default::default(), current_module: hir::CRATE_HIR_ID, - current_hir_id_owner: vec![(CRATE_DEF_INDEX, 0)], + current_hir_id_owner: vec![ + (LocalDefId { index: CRATE_DEF_INDEX }, 0), + ], item_local_id_counters: Default::default(), node_id_to_hir_id: IndexVec::new(), generator_kind: None, @@ -397,7 +399,7 @@ impl<'a> LoweringContext<'a> { fn allocate_use_tree_hir_id_counters( &mut self, tree: &UseTree, - owner: DefIndex, + owner: LocalDefId, ) { match tree.kind { UseTreeKind::Simple(_, id1, id2) => { @@ -416,7 +418,10 @@ impl<'a> LoweringContext<'a> { UseTreeKind::Nested(ref trees) => { for &(ref use_tree, id) in trees { let hir_id = self.lctx.allocate_hir_id_counter(id); - self.allocate_use_tree_hir_id_counters(use_tree, hir_id.owner); + self.allocate_use_tree_hir_id_counters( + use_tree, + hir_id.owner, + ); } } } @@ -454,7 +459,8 @@ impl<'a> LoweringContext<'a> { | ItemKind::TyAlias(_, ref generics) | ItemKind::OpaqueTy(_, ref generics) | ItemKind::Trait(_, _, ref generics, ..) => { - let def_id = self.lctx.resolver.definitions().local_def_id(item.id); + let def_id = self.lctx.resolver.definitions().local_def_id(item.id) + .assert_local(); let count = generics .params .iter() @@ -463,10 +469,13 @@ impl<'a> LoweringContext<'a> { _ => false, }) .count(); - self.lctx.type_def_lifetime_params.insert(def_id, count); + self.lctx.type_def_lifetime_params.insert(def_id.to_def_id(), count); } ItemKind::Use(ref use_tree) => { - self.allocate_use_tree_hir_id_counters(use_tree, hir_id.owner); + self.allocate_use_tree_hir_id_counters( + use_tree, + hir_id.owner, + ); } _ => {} } @@ -611,12 +620,12 @@ impl<'a> LoweringContext<'a> { let counter = self.item_local_id_counters .insert(owner, HIR_ID_COUNTER_LOCKED) .unwrap_or_else(|| panic!("no `item_local_id_counters` entry for {:?}", owner)); - let def_index = self.resolver.definitions().opt_def_index(owner).unwrap(); - self.current_hir_id_owner.push((def_index, counter)); + let def_id = self.resolver.definitions().local_def_id(owner).assert_local(); + self.current_hir_id_owner.push((def_id, counter)); let ret = f(self); - let (new_def_index, new_counter) = self.current_hir_id_owner.pop().unwrap(); + let (new_def_id, new_counter) = self.current_hir_id_owner.pop().unwrap(); - debug_assert!(def_index == new_def_index); + debug_assert!(def_id == new_def_id); debug_assert!(new_counter >= counter); let prev = self.item_local_id_counters @@ -634,12 +643,12 @@ impl<'a> LoweringContext<'a> { /// properly. Calling the method twice with the same `NodeId` is fine though. fn lower_node_id(&mut self, ast_node_id: NodeId) -> hir::HirId { self.lower_node_id_generic(ast_node_id, |this| { - let &mut (def_index, ref mut local_id_counter) = + let &mut (owner, ref mut local_id_counter) = this.current_hir_id_owner.last_mut().unwrap(); let local_id = *local_id_counter; *local_id_counter += 1; hir::HirId { - owner: def_index, + owner, local_id: hir::ItemLocalId::from_u32(local_id), } }) @@ -659,15 +668,15 @@ impl<'a> LoweringContext<'a> { debug_assert!(local_id != HIR_ID_COUNTER_LOCKED); *local_id_counter += 1; - let def_index = this + let owner = this .resolver .definitions() - .opt_def_index(owner) + .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: def_index, + owner, local_id: hir::ItemLocalId::from_u32(local_id), } }) @@ -744,7 +753,7 @@ impl<'a> LoweringContext<'a> { /// parameter while `f` is running (and restored afterwards). fn collect_in_band_defs( &mut self, - parent_id: DefId, + parent_def_id: LocalDefId, anonymous_lifetime_mode: AnonymousLifetimeMode, f: F, ) -> (Vec, T) @@ -768,7 +777,7 @@ impl<'a> LoweringContext<'a> { let params = lifetimes_to_define .into_iter() .map(|(span, hir_name)| self.lifetime_to_generic_param( - span, hir_name, parent_id.index, + span, hir_name, parent_def_id, )) .chain(in_band_ty_params.into_iter()) .collect(); @@ -781,7 +790,7 @@ impl<'a> LoweringContext<'a> { &mut self, span: Span, hir_name: ParamName, - parent_index: DefIndex, + parent_def_id: LocalDefId, ) -> hir::GenericParam { let node_id = self.sess.next_node_id(); @@ -805,7 +814,7 @@ impl<'a> LoweringContext<'a> { // Add a definition for the in-band lifetime def. self.resolver.definitions().create_def_with_parent( - parent_index, + parent_def_id, node_id, DefPathData::LifetimeNs(str_name), ExpnId::root(), @@ -890,7 +899,7 @@ impl<'a> LoweringContext<'a> { fn add_in_band_defs( &mut self, generics: &Generics, - parent_id: DefId, + parent_def_id: LocalDefId, anonymous_lifetime_mode: AnonymousLifetimeMode, f: F, ) -> (hir::Generics, T) @@ -900,7 +909,7 @@ impl<'a> LoweringContext<'a> { let (in_band_defs, (mut lowered_generics, res)) = self.with_in_scope_lifetime_defs( &generics.params, |this| { - this.collect_in_band_defs(parent_id, anonymous_lifetime_mode, |this| { + this.collect_in_band_defs(parent_def_id, anonymous_lifetime_mode, |this| { let mut params = Vec::new(); // Note: it is necessary to lower generics *before* calling `f`. // When lowering `async fn`, there's a final step when lowering @@ -975,8 +984,8 @@ impl<'a> LoweringContext<'a> { } fn def_key(&mut self, id: DefId) -> DefKey { - if id.is_local() { - self.resolver.definitions().def_key(id.index) + if let Some(id) = id.as_local() { + self.resolver.definitions().def_key(id) } else { self.resolver.cstore().def_key(id) } @@ -1103,9 +1112,9 @@ impl<'a> LoweringContext<'a> { // constructing the HIR for `impl bounds...` and then lowering that. let impl_trait_node_id = self.sess.next_node_id(); - let parent_def_index = self.current_hir_id_owner.last().unwrap().0; + let parent_def_id = self.current_hir_id_owner.last().unwrap().0; self.resolver.definitions().create_def_with_parent( - parent_def_index, + parent_def_id, impl_trait_node_id, DefPathData::ImplTrait, ExpnId::root(), @@ -1288,11 +1297,11 @@ impl<'a> LoweringContext<'a> { } ImplTraitContext::Universal(in_band_ty_params) => { // Add a definition for the in-band `Param`. - let def_index = self + let def_id = self .resolver .definitions() - .opt_def_index(def_node_id) - .unwrap(); + .local_def_id(def_node_id) + .assert_local(); let hir_bounds = self.lower_param_bounds( bounds, @@ -1317,7 +1326,7 @@ impl<'a> LoweringContext<'a> { None, P(hir::Path { span, - res: Res::Def(DefKind::TyParam, DefId::local(def_index)), + res: Res::Def(DefKind::TyParam, def_id.to_def_id()), segments: hir_vec![hir::PathSegment::from_ident(ident)], }), )) @@ -1383,11 +1392,11 @@ impl<'a> LoweringContext<'a> { None, ); - let opaque_ty_def_index = self + let opaque_ty_def_id = self .resolver .definitions() - .opt_def_index(opaque_ty_node_id) - .unwrap(); + .local_def_id(opaque_ty_node_id) + .assert_local(); self.allocate_hir_id_counter(opaque_ty_node_id); @@ -1395,7 +1404,7 @@ impl<'a> LoweringContext<'a> { let (lifetimes, lifetime_defs) = self.lifetimes_from_impl_trait_bounds( opaque_ty_node_id, - opaque_ty_def_index, + opaque_ty_def_id, &hir_bounds, ); @@ -1422,7 +1431,7 @@ impl<'a> LoweringContext<'a> { origin: hir::OpaqueTyOrigin::FnReturn, }; - trace!("lower_opaque_impl_trait: {:#?}", opaque_ty_def_index); + trace!("lower_opaque_impl_trait: {:#?}", opaque_ty_def_id); let opaque_ty_id = lctx.generate_opaque_type( opaque_ty_node_id, opaque_ty_item, @@ -1467,14 +1476,14 @@ impl<'a> LoweringContext<'a> { fn lifetimes_from_impl_trait_bounds( &mut self, opaque_ty_id: NodeId, - parent_index: DefIndex, + parent_def_id: LocalDefId, bounds: &hir::GenericBounds, ) -> (HirVec, HirVec) { debug!( "lifetimes_from_impl_trait_bounds(opaque_ty_id={:?}, \ - parent_index={:?}, \ + parent_def_id={:?}, \ bounds={:#?})", - opaque_ty_id, parent_index, bounds, + opaque_ty_id, parent_def_id, bounds, ); // This visitor walks over `impl Trait` bounds and creates defs for all lifetimes that @@ -1482,7 +1491,7 @@ impl<'a> LoweringContext<'a> { // E.g., `'a`, `'b`, but not `'c` in `impl for<'c> SomeTrait<'a, 'b, 'c>`. struct ImplTraitLifetimeCollector<'r, 'a> { context: &'r mut LoweringContext<'a>, - parent: DefIndex, + parent: LocalDefId, opaque_ty_id: NodeId, collect_elided_lifetimes: bool, currently_bound_lifetimes: Vec, @@ -1619,7 +1628,7 @@ impl<'a> LoweringContext<'a> { let mut lifetime_collector = ImplTraitLifetimeCollector { context: self, - parent: parent_index, + parent: parent_def_id, opaque_ty_id, collect_elided_lifetimes: true, currently_bound_lifetimes: Vec::new(), @@ -2072,14 +2081,14 @@ impl<'a> LoweringContext<'a> { visitor.visit_ty(ty); } } - let parent_def_id = DefId::local(self.current_hir_id_owner.last().unwrap().0); + let parent_def_id = self.current_hir_id_owner.last().unwrap().0; (hir::Local { hir_id: self.lower_node_id(l.id), ty: l.ty .as_ref() .map(|t| self.lower_ty(t, if self.sess.features_untracked().impl_trait_in_bindings { - ImplTraitContext::OpaqueTy(Some(parent_def_id)) + ImplTraitContext::OpaqueTy(Some(parent_def_id.to_def_id())) } else { ImplTraitContext::Disallowed(ImplTraitPosition::Binding) } @@ -2252,11 +2261,11 @@ impl<'a> LoweringContext<'a> { None, ); - let opaque_ty_def_index = self + let opaque_ty_def_id = self .resolver .definitions() - .opt_def_index(opaque_ty_node_id) - .unwrap(); + .local_def_id(opaque_ty_node_id) + .assert_local(); self.allocate_hir_id_counter(opaque_ty_node_id); @@ -2349,7 +2358,7 @@ impl<'a> LoweringContext<'a> { lifetime_params .iter().cloned() .map(|(span, hir_name)| { - this.lifetime_to_generic_param(span, hir_name, opaque_ty_def_index) + this.lifetime_to_generic_param(span, hir_name, opaque_ty_def_id) }) .collect(); @@ -2367,7 +2376,7 @@ impl<'a> LoweringContext<'a> { origin: hir::OpaqueTyOrigin::AsyncFn, }; - trace!("exist ty from async fn def index: {:#?}", opaque_ty_def_index); + trace!("exist ty from async fn def id: {:#?}", opaque_ty_def_id); let opaque_ty_id = this.generate_opaque_type( opaque_ty_node_id, opaque_ty_item, diff --git a/src/librustc/hir/lowering/item.rs b/src/librustc/hir/lowering/item.rs index f1b999cdd6f0f..78acbb527a57b 100644 --- a/src/librustc/hir/lowering/item.rs +++ b/src/librustc/hir/lowering/item.rs @@ -7,7 +7,7 @@ use super::ParamMode; use crate::hir::{self, HirVec}; use crate::hir::ptr::P; -use crate::hir::def_id::DefId; +use crate::hir::def_id::LocalDefId; use crate::hir::def::{Res, DefKind}; use crate::util::nodemap::NodeMap; @@ -306,7 +306,7 @@ impl LoweringContext<'_> { ) } ItemKind::Fn(ref decl, header, ref generics, ref body) => { - let fn_def_id = self.resolver.definitions().local_def_id(id); + let fn_def_id = self.resolver.definitions().local_def_id(id).assert_local(); self.with_new_scopes(|this| { this.current_item = Some(ident.span); @@ -322,7 +322,7 @@ impl LoweringContext<'_> { AnonymousLifetimeMode::PassThrough, |this, idty| this.lower_fn_decl( &decl, - Some((fn_def_id, idty)), + Some((fn_def_id.to_def_id(), idty)), true, header.asyncness.node.opt_return_id() ), @@ -388,7 +388,7 @@ impl LoweringContext<'_> { ref ty, ref impl_items, ) => { - let def_id = self.resolver.definitions().local_def_id(id); + let def_id = self.resolver.definitions().local_def_id(id).assert_local(); // Lower the "impl header" first. This ordering is important // for in-band lifetimes! Consider `'a` here: @@ -699,7 +699,7 @@ impl LoweringContext<'_> { } fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem { - let def_id = self.resolver.definitions().local_def_id(i.id); + let def_id = self.resolver.definitions().local_def_id(i.id).assert_local(); hir::ForeignItem { hir_id: self.lower_node_id(i.id), ident: i.ident, @@ -808,7 +808,7 @@ impl LoweringContext<'_> { } fn lower_trait_item(&mut self, i: &TraitItem) -> hir::TraitItem { - let trait_item_def_id = self.resolver.definitions().local_def_id(i.id); + let trait_item_def_id = self.resolver.definitions().local_def_id(i.id).assert_local(); let (generics, kind) = match i.kind { TraitItemKind::Const(ref ty, ref default) => ( @@ -892,7 +892,7 @@ impl LoweringContext<'_> { } fn lower_impl_item(&mut self, i: &ImplItem) -> hir::ImplItem { - let impl_item_def_id = self.resolver.definitions().local_def_id(i.id); + let impl_item_def_id = self.resolver.definitions().local_def_id(i.id).assert_local(); let (generics, kind) = match i.kind { ImplItemKind::Const(ref ty, ref expr) => ( @@ -1260,7 +1260,7 @@ impl LoweringContext<'_> { &mut self, generics: &Generics, sig: &MethodSig, - fn_def_id: DefId, + fn_def_id: LocalDefId, impl_trait_return_allow: bool, is_async: Option, ) -> (hir::Generics, hir::MethodSig) { @@ -1271,7 +1271,7 @@ impl LoweringContext<'_> { AnonymousLifetimeMode::PassThrough, |this, idty| this.lower_fn_decl( &sig.decl, - Some((fn_def_id, idty)), + Some((fn_def_id.to_def_id(), idty)), impl_trait_return_allow, is_async, ), diff --git a/src/librustc/hir/map/collector.rs b/src/librustc/hir/map/collector.rs index b0fa844c81881..d49d44a7ddd04 100644 --- a/src/librustc/hir/map/collector.rs +++ b/src/librustc/hir/map/collector.rs @@ -34,7 +34,7 @@ pub(super) struct NodeCollector<'a, 'hir> { // These fields keep track of the currently relevant DepNodes during // the visitor's traversal. - current_dep_node_owner: DefIndex, + current_dep_node_owner: LocalDefId, current_signature_dep_index: DepNodeIndex, current_full_dep_index: DepNodeIndex, currently_in_body: bool, @@ -105,7 +105,9 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { hir_to_node_id: &'a FxHashMap, 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 { + index: CRATE_DEF_INDEX, + }); let mut hir_body_nodes = Vec::new(); @@ -153,7 +155,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { parent_node: hir::CRATE_HIR_ID, current_signature_dep_index: root_mod_sig_dep_index, current_full_dep_index: root_mod_full_dep_index, - current_dep_node_owner: CRATE_DEF_INDEX, + current_dep_node_owner: LocalDefId { index: CRATE_DEF_INDEX }, currently_in_body: false, dep_graph, definitions, @@ -227,7 +229,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>) { debug!("hir_map: {:?} => {:?}", id, entry); - let local_map = &mut self.map[id.owner]; + let local_map = &mut self.map[id.owner.index]; let i = id.local_id.as_u32() as usize; let len = local_map.len(); if i >= len { @@ -254,9 +256,9 @@ 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) }; @@ -300,7 +302,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { fn with_dep_node_owner HashStable>, F: FnOnce(&mut Self)>(&mut self, - dep_node_owner: DefIndex, + dep_node_owner: LocalDefId, item_like: &T, f: F) { let prev_owner = self.current_dep_node_owner; @@ -369,8 +371,10 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { fn visit_item(&mut self, i: &'hir Item) { debug!("visit_item: {:?}", i); - debug_assert_eq!(i.hir_id.owner, - self.definitions.opt_def_index(self.hir_to_node_id[&i.hir_id]).unwrap()); + debug_assert_eq!( + i.hir_id.owner, + self.definitions.opt_local_def_id(self.hir_to_node_id[&i.hir_id]).unwrap(), + ); self.with_dep_node_owner(i.hir_id.owner, i, |this| { this.insert(i.span, i.hir_id, Node::Item(i)); this.with_parent(i.hir_id, |this| { @@ -399,8 +403,10 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { } fn visit_trait_item(&mut self, ti: &'hir TraitItem) { - debug_assert_eq!(ti.hir_id.owner, - self.definitions.opt_def_index(self.hir_to_node_id[&ti.hir_id]).unwrap()); + debug_assert_eq!( + ti.hir_id.owner, + self.definitions.opt_local_def_id(self.hir_to_node_id[&ti.hir_id]).unwrap(), + ); self.with_dep_node_owner(ti.hir_id.owner, ti, |this| { this.insert(ti.span, ti.hir_id, Node::TraitItem(ti)); @@ -411,8 +417,10 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { } fn visit_impl_item(&mut self, ii: &'hir ImplItem) { - debug_assert_eq!(ii.hir_id.owner, - self.definitions.opt_def_index(self.hir_to_node_id[&ii.hir_id]).unwrap()); + debug_assert_eq!( + ii.hir_id.owner, + self.definitions.opt_local_def_id(self.hir_to_node_id[&ii.hir_id]).unwrap(), + ); self.with_dep_node_owner(ii.hir_id.owner, ii, |this| { this.insert(ii.span, ii.hir_id, Node::ImplItem(ii)); @@ -531,10 +539,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { } fn visit_macro_def(&mut self, macro_def: &'hir MacroDef) { - let node_id = self.hir_to_node_id[¯o_def.hir_id]; - let def_index = self.definitions.opt_def_index(node_id).unwrap(); - - self.with_dep_node_owner(def_index, macro_def, |this| { + self.with_dep_node_owner(macro_def.hir_id.owner, macro_def, |this| { this.insert(macro_def.span, macro_def.hir_id, Node::MacroDef(macro_def)); }); } diff --git a/src/librustc/hir/map/def_collector.rs b/src/librustc/hir/map/def_collector.rs index e9970e30bf9e5..1f7b195b3cb08 100644 --- a/src/librustc/hir/map/def_collector.rs +++ b/src/librustc/hir/map/def_collector.rs @@ -1,5 +1,5 @@ use crate::hir::map::definitions::*; -use crate::hir::def_id::DefIndex; +use crate::hir::def_id::LocalDefId; use syntax::ast::*; use syntax::visit; @@ -11,7 +11,7 @@ use syntax_pos::Span; /// Creates `DefId`s for nodes in the AST. pub struct DefCollector<'a> { definitions: &'a mut Definitions, - parent_def: DefIndex, + parent_def: LocalDefId, expansion: ExpnId, } @@ -25,13 +25,13 @@ impl<'a> DefCollector<'a> { node_id: NodeId, data: DefPathData, span: Span) - -> DefIndex { + -> 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/hir/map/definitions.rs b/src/librustc/hir/map/definitions.rs index 1e444e8a5b84e..8e64c93b0b587 100644 --- a/src/librustc/hir/map/definitions.rs +++ b/src/librustc/hir/map/definitions.rs @@ -5,7 +5,7 @@ //! expressions) that are mostly just leftovers. use crate::hir; -use crate::hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE, CRATE_DEF_INDEX}; +use crate::hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE, LocalDefId, CRATE_DEF_INDEX}; use crate::ich::Fingerprint; use crate::session::CrateDisambiguator; use crate::util::nodemap::NodeMap; @@ -91,19 +91,20 @@ impl DefPathTable { #[derive(Clone, Default)] pub struct Definitions { table: DefPathTable, - node_to_def_index: NodeMap, + node_to_def_id: NodeMap, def_index_to_node: IndexVec, pub(super) node_to_hir_id: IndexVec, /// 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>, + /// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`. + expansions_that_defined: FxHashMap, + next_disambiguator: FxHashMap<(LocalDefId, DefPathData), u32>, + // FIXME(eddyb) replace this `FxHashMap` with an `IndexVec`. def_index_to_span: FxHashMap, /// 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. pub(super) placeholder_field_indices: NodeMap, } @@ -335,13 +336,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.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.index) } /// Returns the path from the crate root to `index`. The root @@ -349,28 +350,23 @@ 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)) - } - - #[inline] - pub fn opt_def_index(&self, node: ast::NodeId) -> Option { - self.node_to_def_index.get(&node).copied() + pub fn def_path(&self, id: LocalDefId) -> DefPath { + DefPath::make(LOCAL_CRATE, id.index, |index| self.def_key(LocalDefId { index })) } #[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_to_def_id.get(&node).copied() } #[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 { + if let Some(def_id) = def_id.as_local() { let node_id = self.def_index_to_node[def_id.index]; if node_id != ast::DUMMY_NODE_ID { return Some(node_id); @@ -381,8 +377,8 @@ 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 { @@ -399,8 +395,8 @@ impl Definitions { } #[inline] - pub fn def_index_to_hir_id(&self, def_index: DefIndex) -> hir::HirId { - let node_id = self.def_index_to_node[def_index]; + pub fn local_def_id_to_hir_id(&self, id: LocalDefId) -> hir::HirId { + let node_id = self.def_index_to_node[id.index]; self.node_to_hir_id[node_id] } @@ -408,7 +404,7 @@ impl Definitions { /// and it's not `DUMMY_SP`. #[inline] pub fn opt_span(&self, def_id: DefId) -> Option { - if def_id.krate == LOCAL_CRATE { + if let Some(def_id) = def_id.as_local() { self.def_index_to_span.get(&def_id.index).copied() } else { None @@ -419,7 +415,7 @@ impl Definitions { pub fn create_root_def(&mut self, crate_name: &str, crate_disambiguator: CrateDisambiguator) - -> DefIndex { + -> LocalDefId { let key = DefKey { parent: None, disambiguated_data: DisambiguatedDefPathData { @@ -433,32 +429,34 @@ 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); + let root = LocalDefId { + 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); + self.node_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), + assert!(!self.node_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_to_def_id[&node_id].index)); // The root node must be created with `create_root_def()`. assert!(data != DefPathData::CrateRoot); @@ -472,40 +470,43 @@ impl Definitions { }; let key = DefKey { - parent: Some(parent), + parent: Some(parent.index), disambiguated_data: DisambiguatedDefPathData { data, disambiguator } }; - let parent_hash = self.table.def_path_hash(parent); + let parent_hash = self.table.def_path_hash(parent.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()); + let def_id = LocalDefId { + index: self.table.allocate(key, def_path_hash), + }; + assert_eq!(def_id.index.index(), self.def_index_to_node.len()); self.def_index_to_node.push(node_id); // Some things for which we allocate `DefIndex`es 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`. 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_index_to_node[{:?}] <-> {:?}", + def_id.index, node_id); + self.node_to_def_id.insert(node_id, def_id); } if expn_id != ExpnId::root() { - self.expansions_that_defined.insert(index, expn_id); + self.expansions_that_defined.insert(def_id, expn_id); } // The span is added if it isn't dummy. if !span.is_dummy() { - self.def_index_to_span.insert(index, span); + self.def_index_to_span.insert(def_id.index, span); } - index + def_id } /// Initializes the `ast::NodeId` to `HirId` mapping once it has been generated during @@ -517,8 +518,8 @@ impl Definitions { self.node_to_hir_id = mapping; } - 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 { @@ -529,13 +530,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"); } } diff --git a/src/librustc/hir/map/hir_id_validator.rs b/src/librustc/hir/map/hir_id_validator.rs index b66c2ce1178a4..d7d01a625443e 100644 --- a/src/librustc/hir/map/hir_id_validator.rs +++ b/src/librustc/hir/map/hir_id_validator.rs @@ -1,4 +1,4 @@ -use crate::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX}; +use crate::hir::def_id::{CRATE_DEF_INDEX, LocalDefId}; use crate::hir::{self, intravisit, HirId, ItemLocalId}; use crate::hir::itemlikevisit::ItemLikeVisitor; use rustc_data_structures::fx::FxHashSet; @@ -29,7 +29,7 @@ pub fn check_crate(hir_map: &hir::map::Map<'_>) { struct HirIdValidator<'a, 'hir> { hir_map: &'a hir::map::Map<'hir>, - owner_def_index: Option, + owner: Option, hir_ids_seen: FxHashSet, errors: &'a Lock>, } @@ -45,7 +45,7 @@ impl<'a, 'hir> OuterVisitor<'a, 'hir> { -> HirIdValidator<'a, 'hir> { HirIdValidator { hir_map, - owner_def_index: None, + owner: None, hir_ids_seen: Default::default(), errors: self.errors, } @@ -79,12 +79,12 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> { fn check)>(&mut self, hir_id: HirId, walk: F) { - assert!(self.owner_def_index.is_none()); - let owner_def_index = self.hir_map.local_def_id(hir_id).index; - self.owner_def_index = Some(owner_def_index); + assert!(self.owner.is_none()); + let owner = self.hir_map.local_def_id(hir_id).assert_local(); + self.owner = Some(owner); walk(self); - if owner_def_index == CRATE_DEF_INDEX { + if owner.index == CRATE_DEF_INDEX { return; } @@ -106,7 +106,7 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> { for local_id in missing { let hir_id = HirId { - owner: owner_def_index, + owner, local_id: ItemLocalId::from_u32(local_id), }; @@ -119,13 +119,13 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> { self.error(|| format!( "ItemLocalIds not assigned densely in {}. \ Max ItemLocalId = {}, missing IDs = {:?}; seens IDs = {:?}", - self.hir_map.def_path(DefId::local(owner_def_index)).to_string_no_crate(), + self.hir_map.def_path(owner).to_string_no_crate(), max, missing_items, self.hir_ids_seen .iter() .map(|&local_id| HirId { - owner: owner_def_index, + owner, local_id, }) .map(|h| format!("({:?} {})", h, self.hir_map.node_to_string(h))) @@ -142,7 +142,7 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> { } fn visit_id(&mut self, hir_id: HirId) { - let owner = self.owner_def_index.expect("no owner_def_index"); + let owner = self.owner.expect("no owner"); if hir_id == hir::DUMMY_HIR_ID { self.error(|| format!("HirIdValidator: HirId {:?} is invalid", @@ -154,8 +154,8 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> { self.error(|| format!( "HirIdValidator: The recorded owner of {} is {} instead of {}", self.hir_map.node_to_string(hir_id), - self.hir_map.def_path(DefId::local(hir_id.owner)).to_string_no_crate(), - self.hir_map.def_path(DefId::local(owner)).to_string_no_crate())); + self.hir_map.def_path(hir_id.owner).to_string_no_crate(), + self.hir_map.def_path(owner).to_string_no_crate())); } self.hir_ids_seen.insert(hir_id.local_id); diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index acadd77cc36c0..d68be875ea6f0 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -222,7 +222,7 @@ impl<'map> Iterator for ParentHirIterator<'map> { impl<'hir> Map<'hir> { #[inline] fn lookup(&self, id: HirId) -> Option<&Entry<'hir>> { - let local_map = self.map.get(id.owner)?; + let local_map = self.map.get(id.owner.index)?; local_map.get(id.local_id)?.as_ref() } @@ -246,20 +246,18 @@ impl<'hir> Map<'hir> { self.definitions } - pub fn def_key(&self, def_id: DefId) -> DefKey { - assert!(def_id.is_local()); - self.definitions.def_key(def_id.index) + pub fn def_key(&self, def_id: LocalDefId) -> DefKey { + self.definitions.def_key(def_id) } pub fn def_path_from_hir_id(&self, id: HirId) -> Option { self.opt_local_def_id(id).map(|def_id| { - self.def_path(def_id) + self.def_path(def_id.assert_local()) }) } - pub fn def_path(&self, def_id: DefId) -> DefPath { - assert!(def_id.is_local()); - self.definitions.def_path(def_id.index) + pub fn def_path(&self, def_id: LocalDefId) -> DefPath { + self.definitions.def_path(def_id) } #[inline] @@ -282,12 +280,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.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.definitions.opt_local_def_id(node) + Some(self.definitions.opt_local_def_id(node)?.to_def_id()) } #[inline] @@ -310,14 +308,9 @@ impl<'hir> Map<'hir> { self.definitions.node_to_hir_id(node_id) } - #[inline] - pub fn def_index_to_hir_id(&self, def_index: DefIndex) -> HirId { - self.definitions.def_index_to_hir_id(def_index) - } - #[inline] pub fn local_def_id_to_hir_id(&self, def_id: LocalDefId) -> HirId { - self.definitions.def_index_to_hir_id(def_id.to_def_id().index) + self.definitions.local_def_id_to_hir_id(def_id) } pub fn def_kind(&self, hir_id: HirId) -> Option { @@ -468,8 +461,7 @@ impl<'hir> Map<'hir> { pub fn maybe_body_owned_by(&self, hir_id: HirId) -> Option { if let Some(entry) = self.find_entry(hir_id) { if self.dep_graph.is_fully_enabled() { - let hir_id_owner = hir_id.owner; - let def_path_hash = self.definitions.def_path_hash(hir_id_owner); + let def_path_hash = self.definitions.def_path_hash(hir_id.owner); self.dep_graph.read(def_path_hash.to_dep_node(DepKind::HirBody)); } @@ -541,7 +533,9 @@ impl<'hir> Map<'hir> { /// invoking `krate.attrs` because it registers a tighter /// dep-graph access. pub fn krate_attrs(&self) -> &'hir [ast::Attribute] { - let def_path_hash = self.definitions.def_path_hash(CRATE_DEF_INDEX); + let def_path_hash = self.definitions.def_path_hash(LocalDefId { + index: CRATE_DEF_INDEX, + }); self.dep_graph.read(def_path_hash.to_dep_node(DepKind::Hir)); &self.forest.krate.attrs @@ -648,8 +642,7 @@ impl<'hir> Map<'hir> { /// which can happen if the ID is not in the map itself or is just weird). pub fn get_parent_node(&self, hir_id: HirId) -> HirId { if self.dep_graph.is_fully_enabled() { - let hir_id_owner = hir_id.owner; - let def_path_hash = self.definitions.def_path_hash(hir_id_owner); + let def_path_hash = self.definitions.def_path_hash(hir_id.owner); self.dep_graph.read(def_path_hash.to_dep_node(DepKind::HirBody)); } @@ -1036,7 +1029,7 @@ impl<'hir> Map<'hir> { local_map.iter_enumerated().filter_map(move |(i, entry)| entry.map(move |_| { // Reconstruct the `HirId` based on the 3 indices we used to find it. HirId { - owner, + owner: LocalDefId { index: owner }, local_id: i, } })) diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index 0edc41e6b4881..f1ed88ba9711d 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -64,7 +64,7 @@ pub mod ptr; pub mod upvars; /// Uniquely identifies a node in the HIR of the current crate. It is -/// composed of the `owner`, which is the `DefIndex` of the directly enclosing +/// composed of the `owner`, which is the `LocalDefId` of the directly enclosing /// `hir::Item`, `hir::TraitItem`, or `hir::ImplItem` (i.e., the closest "item-like"), /// and the `local_id` which is unique within the given owner. /// @@ -75,20 +75,10 @@ pub mod upvars; /// the code base. #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)] pub struct HirId { - pub owner: DefIndex, + pub owner: LocalDefId, pub local_id: ItemLocalId, } -impl HirId { - pub fn owner_def_id(self) -> DefId { - DefId::local(self.owner) - } - - pub fn owner_local_def_id(self) -> LocalDefId { - LocalDefId::from_def_id(DefId::local(self.owner)) - } -} - impl rustc_serialize::UseSpecializedEncodable for HirId { fn default_encode(&self, s: &mut S) -> Result<(), S::Error> { let HirId { @@ -96,7 +86,9 @@ impl rustc_serialize::UseSpecializedEncodable for HirId { local_id, } = *self; - owner.encode(s)?; + // HACK(eddyb) this avoids `LocalDefId` because that encodes as a `DefId`, + // maybe try to fix that? (but it's unclear if anything relies on it) + owner.index.encode(s)?; local_id.encode(s)?; Ok(()) } @@ -104,7 +96,9 @@ impl rustc_serialize::UseSpecializedEncodable for HirId { impl rustc_serialize::UseSpecializedDecodable for HirId { fn default_decode(d: &mut D) -> Result { - let owner = DefIndex::decode(d)?; + // HACK(eddyb) this avoids `LocalDefId` because that encodes as a `DefId`, + // maybe try to fix that? (but it's unclear if anything relies on it) + let owner = LocalDefId { index: DefIndex::decode(d)? }; let local_id = ItemLocalId::decode(d)?; Ok(HirId { @@ -143,12 +137,12 @@ pub use self::item_local_id_inner::ItemLocalId; /// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_INDEX`. pub const CRATE_HIR_ID: HirId = HirId { - owner: CRATE_DEF_INDEX, + owner: LocalDefId { index: CRATE_DEF_INDEX }, local_id: ItemLocalId::from_u32_const(0) }; pub const DUMMY_HIR_ID: HirId = HirId { - owner: CRATE_DEF_INDEX, + owner: LocalDefId { index: CRATE_DEF_INDEX }, local_id: DUMMY_ITEM_LOCAL_ID, }; diff --git a/src/librustc/ich/hcx.rs b/src/librustc/ich/hcx.rs index a5b131520c243..80f70a1c05a59 100644 --- a/src/librustc/ich/hcx.rs +++ b/src/librustc/ich/hcx.rs @@ -1,5 +1,5 @@ use crate::hir; -use crate::hir::def_id::{DefId, DefIndex}; +use crate::hir::def_id::{DefId, LocalDefId}; use crate::hir::map::DefPathHash; use crate::hir::map::definitions::Definitions; use crate::ich::{self, CachingSourceMapView, Fingerprint}; @@ -131,16 +131,16 @@ impl<'a> StableHashingContext<'a> { #[inline] pub fn def_path_hash(&self, def_id: DefId) -> DefPathHash { - if def_id.is_local() { - self.definitions.def_path_hash(def_id.index) + if let Some(def_id) = def_id.as_local() { + self.definitions.def_path_hash(def_id) } else { self.cstore.def_path_hash(def_id) } } #[inline] - pub fn local_def_path_hash(&self, def_index: DefIndex) -> DefPathHash { - self.definitions.def_path_hash(def_index) + pub fn local_def_path_hash(&self, def_id: LocalDefId) -> DefPathHash { + self.definitions.def_path_hash(def_id) } #[inline] diff --git a/src/librustc/ich/impls_hir.rs b/src/librustc/ich/impls_hir.rs index c0255e5b8a481..92cc5343b1819 100644 --- a/src/librustc/ich/impls_hir.rs +++ b/src/librustc/ich/impls_hir.rs @@ -327,22 +327,6 @@ impl<'a> ToStableHashKey> for hir::BodyId { } } -impl<'a> HashStable> for hir::def_id::DefIndex { - - fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { - hcx.local_def_path_hash(*self).hash_stable(hcx, hasher); - } -} - -impl<'a> ToStableHashKey> for hir::def_id::DefIndex { - type KeyType = DefPathHash; - - #[inline] - fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> DefPathHash { - hcx.local_def_path_hash(*self) - } -} - impl<'a> HashStable> for crate::middle::lang_items::LangItem { fn hash_stable(&self, _: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { ::std::hash::Hash::hash(self, hasher); @@ -374,9 +358,10 @@ impl<'a> ToStableHashKey> for hir::TraitCandidate { import_ids, } = self; - let import_keys = import_ids.iter().map(|node_id| hcx.node_to_hir_id(*node_id)) - .map(|hir_id| (hcx.local_def_path_hash(hir_id.owner), - hir_id.local_id)).collect(); + let import_keys = import_ids.iter() + .map(|node_id| hcx.node_to_hir_id(*node_id)) + .map(|hir_id| (hcx.local_def_path_hash(hir_id.owner), hir_id.local_id)) + .collect(); (hcx.def_path_hash(*def_id), import_keys) } } diff --git a/src/librustc/lint/internal.rs b/src/librustc/lint/internal.rs index a08722e940295..da6a21fceccd3 100644 --- a/src/librustc/lint/internal.rs +++ b/src/librustc/lint/internal.rs @@ -132,7 +132,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyTyKind { } } TyKind::Rptr(_, MutTy { ty: inner_ty, mutbl: Mutability::MutImmutable }) => { - if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner_def_id()) { + if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner.to_def_id()) { if cx.tcx.impl_trait_ref(impl_did).is_some() { return; } diff --git a/src/librustc/middle/expr_use_visitor.rs b/src/librustc/middle/expr_use_visitor.rs index bb7ac5d8dbe1a..807a283a2aac1 100644 --- a/src/librustc/middle/expr_use_visitor.rs +++ b/src/librustc/middle/expr_use_visitor.rs @@ -622,7 +622,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> { for &var_id in upvars.keys() { let upvar_id = ty::UpvarId { var_path: ty::UpvarPath { hir_id: var_id }, - closure_expr_id: closure_def_id.to_local(), + closure_expr_id: closure_def_id.assert_local(), }; let upvar_capture = self.mc.tables.upvar_capture(upvar_id); let cmt_var = return_if_err!(self.cat_captured_var(closure_expr.hir_id, diff --git a/src/librustc/middle/mem_categorization.rs b/src/librustc/middle/mem_categorization.rs index cbf336fdbe2f3..fc6a1bcbd36de 100644 --- a/src/librustc/middle/mem_categorization.rs +++ b/src/librustc/middle/mem_categorization.rs @@ -59,7 +59,7 @@ pub use self::Note::*; use self::Aliasability::*; use crate::middle::region; -use crate::hir::def_id::{DefId, LocalDefId}; +use crate::hir::def_id::DefId; use crate::hir::Node; use crate::infer::InferCtxt; use crate::hir::def::{CtorOf, Res, DefKind, CtorKind}; @@ -723,7 +723,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> { let closure_expr_def_id = self.body_owner; let fn_hir_id = self.tcx.hir().local_def_id_to_hir_id( - LocalDefId::from_def_id(closure_expr_def_id), + closure_expr_def_id.assert_local(), ); let ty = self.node_ty(fn_hir_id)?; let kind = match ty.kind { @@ -747,7 +747,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> { let upvar_id = ty::UpvarId { var_path: ty::UpvarPath { hir_id: var_id }, - closure_expr_id: closure_expr_def_id.to_local(), + closure_expr_id: closure_expr_def_id.assert_local(), }; let var_ty = self.node_ty(var_id)?; diff --git a/src/librustc/middle/reachable.rs b/src/librustc/middle/reachable.rs index 8be64bf64b5e9..4809e3198c4e6 100644 --- a/src/librustc/middle/reachable.rs +++ b/src/librustc/middle/reachable.rs @@ -50,7 +50,7 @@ fn method_might_be_inlined( impl_item: &hir::ImplItem, impl_src: DefId, ) -> bool { - let codegen_fn_attrs = tcx.codegen_fn_attrs(impl_item.hir_id.owner_def_id()); + let codegen_fn_attrs = tcx.codegen_fn_attrs(impl_item.hir_id.owner.to_def_id()); let generics = tcx.generics_of(tcx.hir().local_def_id(impl_item.hir_id)); if codegen_fn_attrs.requests_inline() || generics.requires_monomorphization(tcx) { return true diff --git a/src/librustc/middle/region.rs b/src/librustc/middle/region.rs index 9ff205228a566..7bf6cf28c46f2 100644 --- a/src/librustc/middle/region.rs +++ b/src/librustc/middle/region.rs @@ -672,7 +672,7 @@ impl<'tcx> ScopeTree { region scope tree for {:?} / {:?}", param_owner, self.root_parent.map(|id| tcx.hir().local_def_id(id)), - self.root_body.map(|hir_id| DefId::local(hir_id.owner)))); + self.root_body.map(|hir_id| hir_id.owner))); } // The trait/impl lifetime is in scope for the method's body. diff --git a/src/librustc/middle/resolve_lifetime.rs b/src/librustc/middle/resolve_lifetime.rs index 488d6332f7e39..79f5cedd48fc7 100644 --- a/src/librustc/middle/resolve_lifetime.rs +++ b/src/librustc/middle/resolve_lifetime.rs @@ -342,19 +342,16 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) { resolve_lifetimes, named_region_map: |tcx, id| { - let id = LocalDefId::from_def_id(DefId::local(id)); // (*) tcx.resolve_lifetimes(LOCAL_CRATE).defs.get(&id) }, is_late_bound_map: |tcx, id| { - let id = LocalDefId::from_def_id(DefId::local(id)); // (*) tcx.resolve_lifetimes(LOCAL_CRATE) .late_bound .get(&id) }, object_lifetime_defaults_map: |tcx, id| { - let id = LocalDefId::from_def_id(DefId::local(id)); // (*) tcx.resolve_lifetimes(LOCAL_CRATE) .object_lifetime_defaults .get(&id) @@ -378,18 +375,18 @@ fn resolve_lifetimes(tcx: TyCtxt<'_>, for_krate: CrateNum) -> &ResolveLifetimes let mut rl = ResolveLifetimes::default(); for (hir_id, v) in named_region_map.defs { - let map = rl.defs.entry(hir_id.owner_local_def_id()).or_default(); + let map = rl.defs.entry(hir_id.owner).or_default(); map.insert(hir_id.local_id, v); } for hir_id in named_region_map.late_bound { let map = rl.late_bound - .entry(hir_id.owner_local_def_id()) + .entry(hir_id.owner) .or_default(); map.insert(hir_id.local_id); } for (hir_id, v) in named_region_map.object_lifetime_defaults { let map = rl.object_lifetime_defaults - .entry(hir_id.owner_local_def_id()) + .entry(hir_id.owner) .or_default(); map.insert(hir_id.local_id, v); } diff --git a/src/librustc/query/mod.rs b/src/librustc/query/mod.rs index 12ae2c3201547..32fe8a78a1559 100644 --- a/src/librustc/query/mod.rs +++ b/src/librustc/query/mod.rs @@ -3,7 +3,7 @@ use crate::ty::query::queries; use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt}; use crate::ty::subst::SubstsRef; use crate::dep_graph::{RecoverKey,DepKind, DepNode, SerializedDepNodeIndex}; -use crate::hir::def_id::{CrateNum, DefId, DefIndex}; +use crate::hir::def_id::{CrateNum, DefId, LocalDefId}; use crate::mir; use crate::mir::interpret::GlobalId; use crate::traits; @@ -672,7 +672,7 @@ rustc_queries! { no_force desc { "computing whether impls specialize one another" } } - query in_scope_traits_map(_: DefIndex) + query in_scope_traits_map(_: LocalDefId) -> Option<&'tcx FxHashMap>> { eval_always desc { "traits in scope at a block" } @@ -797,15 +797,15 @@ rustc_queries! { query resolve_lifetimes(_: CrateNum) -> &'tcx ResolveLifetimes { desc { "resolving lifetimes" } } - query named_region_map(_: DefIndex) -> + query named_region_map(_: LocalDefId) -> Option<&'tcx FxHashMap> { desc { "looking up a named region" } } - query is_late_bound_map(_: DefIndex) -> + query is_late_bound_map(_: LocalDefId) -> Option<&'tcx FxHashSet> { desc { "testing if a region is late bound" } } - query object_lifetime_defaults_map(_: DefIndex) + query object_lifetime_defaults_map(_: LocalDefId) -> Option<&'tcx FxHashMap>> { desc { "looking up lifetime defaults for a region" } } diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index 0906d9ebd8e7f..3db94fbb30f20 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -10,7 +10,7 @@ use crate::session::config::CrateType; use crate::middle; use crate::hir::{self, TraitCandidate, HirId, ItemKind, ItemLocalId, Node}; use crate::hir::def::{Res, DefKind, Export}; -use crate::hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE}; +use crate::hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId}; use crate::hir::map as hir_map; use crate::hir::map::DefPathHash; use crate::lint::{self, Lint}; @@ -205,12 +205,12 @@ fn validate_hir_id_for_typeck_tables(local_id_root: Option, hir_id: hir::HirId, mut_access: bool) { if let Some(local_id_root) = local_id_root { - if hir_id.owner != local_id_root.index { + if hir_id.owner.to_def_id() != local_id_root { ty::tls::with(|tcx| { bug!("node {} with HirId::owner {:?} cannot be placed in \ TypeckTables with local_id_root {:?}", tcx.hir().node_to_string(hir_id), - DefId::local(hir_id.owner), + hir_id.owner, local_id_root) }); } @@ -793,11 +793,11 @@ impl<'a, 'tcx> HashStable> for TypeckTables<'tcx> { let var_owner_def_id = DefId { krate: local_id_root.krate, - index: var_path.hir_id.owner, + index: var_path.hir_id.owner.index, }; let closure_def_id = DefId { krate: local_id_root.krate, - index: closure_expr_id.to_def_id().index, + index: closure_expr_id.index, }; (hcx.def_path_hash(var_owner_def_id), var_path.hir_id.local_id, @@ -1054,7 +1054,7 @@ pub struct GlobalCtxt<'tcx> { /// Map indicating what traits are in scope for places where this /// is relevant; generated by resolve. - trait_map: FxHashMap>>, @@ -1343,7 +1343,7 @@ impl<'tcx> TyCtxt<'tcx> { } pub fn def_key(self, id: DefId) -> hir_map::DefKey { - if id.is_local() { + if let Some(id) = id.as_local() { self.hir().def_key(id) } else { self.cstore.def_key(id) @@ -1356,7 +1356,7 @@ impl<'tcx> TyCtxt<'tcx> { /// Note that if `id` is not local to this crate, the result will /// be a non-local `DefPath`. pub fn def_path(self, id: DefId) -> hir_map::DefPath { - if id.is_local() { + if let Some(id) = id.as_local() { self.hir().def_path(id) } else { self.cstore.def_path(id) @@ -1375,8 +1375,8 @@ impl<'tcx> TyCtxt<'tcx> { #[inline] pub fn def_path_hash(self, def_id: DefId) -> hir_map::DefPathHash { - if def_id.is_local() { - self.hir().definitions().def_path_hash(def_id.index) + if let Some(def_id) = def_id.as_local() { + self.hir().definitions().def_path_hash(def_id) } else { self.cstore.def_path_hash(def_id) } @@ -3004,13 +3004,11 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) { tcx.arena.alloc(stability::Index::new(tcx)) }; 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.assert_local()); tcx.stability().local_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.assert_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 1b1cc423fd457..cac4697675200 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -3124,9 +3124,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/ty/query/keys.rs b/src/librustc/ty/query/keys.rs index a9e0a5d6ab564..868d381bdff0b 100644 --- a/src/librustc/ty/query/keys.rs +++ b/src/librustc/ty/query/keys.rs @@ -1,7 +1,7 @@ //! Defines the set of legal keys that can be used in queries. use crate::infer::canonical::Canonical; -use crate::hir::def_id::{CrateNum, DefId, LOCAL_CRATE, DefIndex}; +use crate::hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId}; use crate::traits; use crate::ty::{self, Ty, TyCtxt}; use crate::ty::subst::SubstsRef; @@ -62,12 +62,12 @@ impl Key for CrateNum { } } -impl Key for DefIndex { +impl Key for LocalDefId { fn query_crate(&self) -> CrateNum { - LOCAL_CRATE + self.to_def_id().query_crate() } - fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { - DUMMY_SP + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { + self.to_def_id().default_span(tcx) } } diff --git a/src/librustc/ty/query/mod.rs b/src/librustc/ty/query/mod.rs index 9b15ad560b5d2..f804ad6598310 100644 --- a/src/librustc/ty/query/mod.rs +++ b/src/librustc/ty/query/mod.rs @@ -1,5 +1,5 @@ use crate::dep_graph::{self, DepNode}; -use crate::hir::def_id::{CrateNum, DefId, DefIndex}; +use crate::hir::def_id::{CrateNum, DefId, LocalDefId}; use crate::hir::def::{DefKind, Export}; use crate::hir::{self, TraitCandidate, ItemLocalId, CodegenFnAttrs}; use crate::infer::canonical::{self, Canonical}; diff --git a/src/librustc/ty/query/on_disk_cache.rs b/src/librustc/ty/query/on_disk_cache.rs index 4031eb6219432..6aa42a151c2bf 100644 --- a/src/librustc/ty/query/on_disk_cache.rs +++ b/src/librustc/ty/query/on_disk_cache.rs @@ -658,7 +658,7 @@ impl<'a, 'tcx> SpecializedDecoder for CacheDecoder<'a, 'tcx> { impl<'a, 'tcx> SpecializedDecoder for CacheDecoder<'a, 'tcx> { #[inline] fn specialized_decode(&mut self) -> Result { - Ok(LocalDefId::from_def_id(DefId::decode(self)?)) + Ok(DefId::decode(self)?.assert_local()) } } @@ -667,13 +667,14 @@ impl<'a, 'tcx> SpecializedDecoder for CacheDecoder<'a, 'tcx> { // Load the `DefPathHash` which is what we encoded the `DefIndex` as. let def_path_hash = DefPathHash::decode(self)?; - // Use the `DefPathHash` to map to the current `DefId`. - let def_id = self.tcx() - .def_path_hash_to_def_id - .as_ref() - .unwrap()[&def_path_hash]; - - debug_assert!(def_id.is_local()); + // Use the `DefPathHash` to map to the current `LocalDefId`. + // FIXME(eddyb) make this cheaper by having `LocalDefId` instead of + // `DefId` in `def_path_hash_to_def_id`. + let owner = self.tcx() + .def_path_hash_to_def_id + .as_ref() + .unwrap()[&def_path_hash] + .assert_local(); // The `ItemLocalId` needs no remapping. let local_id = hir::ItemLocalId::decode(self)?; @@ -681,7 +682,7 @@ impl<'a, 'tcx> SpecializedDecoder for CacheDecoder<'a, 'tcx> { // Reconstruct the `HirId` and look up the corresponding `NodeId` in the // context of the current session. Ok(hir::HirId { - owner: def_id.index, + owner, local_id }) } diff --git a/src/librustc/ty/query/plumbing.rs b/src/librustc/ty/query/plumbing.rs index 538154b035ac6..d85c8315ba51e 100644 --- a/src/librustc/ty/query/plumbing.rs +++ b/src/librustc/ty/query/plumbing.rs @@ -1117,7 +1117,7 @@ macro_rules! define_provider_struct { /// /// When you implement a new query, it will likely have a corresponding new /// `DepKind`, and you'll have to support it here in `force_from_dep_node()`. As -/// a rule of thumb, if your query takes a `DefId` or `DefIndex` as sole parameter, +/// a rule of thumb, if your query takes a `DefId` or `LocalDefId` as sole parameter, /// then `force_from_dep_node()` should not fail for it. Otherwise, you can just /// add it to the "We don't have enough information to reconstruct..." group in /// the match below. diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index 483b1a40e44d6..ab897fe5a83ea 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -896,11 +896,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/decoder.rs b/src/librustc_metadata/decoder.rs index c5954e1ea1d98..40bbfa41b8b94 100644 --- a/src/librustc_metadata/decoder.rs +++ b/src/librustc_metadata/decoder.rs @@ -287,7 +287,7 @@ impl<'a, 'tcx> SpecializedDecoder for DecodeContext<'a, 'tcx> { impl<'a, 'tcx> SpecializedDecoder for DecodeContext<'a, 'tcx> { #[inline] fn specialized_decode(&mut self) -> Result { - self.specialized_decode().map(|i| LocalDefId::from_def_id(i)) + Ok(DefId::decode(self)?.assert_local()) } } diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index de00e9920e683..12aecf65ca60c 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -1398,7 +1398,7 @@ impl EncodeContext<'tcx> { let tcx = self.tcx; Some(self.lazy(tcx.hir().krate().items.values().filter_map(|item| { if item.attrs.iter().any(|attr| is_proc_macro_attr(attr)) { - Some(item.hir_id.owner) + Some(item.hir_id.owner.index) } else { None } @@ -1495,8 +1495,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 { index }) }); TraitImpls { diff --git a/src/librustc_mir/borrow_check/conflict_errors.rs b/src/librustc_mir/borrow_check/conflict_errors.rs index 0913d743328a7..5d6bb468c60a2 100644 --- a/src/librustc_mir/borrow_check/conflict_errors.rs +++ b/src/librustc_mir/borrow_check/conflict_errors.rs @@ -1272,7 +1272,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let escapes_from = if tcx.is_closure(self.mir_def_id) { let tables = tcx.typeck_tables_of(self.mir_def_id); - let mir_hir_id = tcx.hir().def_index_to_hir_id(self.mir_def_id.index); + let mir_hir_id = tcx.hir().local_def_id_to_hir_id(self.mir_def_id.assert_local()); match tables.node_type(mir_hir_id).kind { ty::Closure(..) => "closure", ty::Generator(..) => "generator", diff --git a/src/librustc_mir/borrow_check/nll/mod.rs b/src/librustc_mir/borrow_check/nll/mod.rs index b2e5751b902e7..bfd0b0b23293c 100644 --- a/src/librustc_mir/borrow_check/nll/mod.rs +++ b/src/librustc_mir/borrow_check/nll/mod.rs @@ -258,7 +258,7 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>( // Dump facts if requested. let polonius_output = all_facts.and_then(|all_facts| { if infcx.tcx.sess.opts.debugging_opts.nll_facts { - let def_path = infcx.tcx.hir().def_path(def_id); + let def_path = infcx.tcx.def_path(def_id); let dir_path = PathBuf::from("nll-facts").join(def_path.to_filename_friendly_no_crate()); all_facts.write_to_dir(dir_path, location_table).unwrap(); diff --git a/src/librustc_mir/borrow_check/nll/type_check/mod.rs b/src/librustc_mir/borrow_check/nll/type_check/mod.rs index 9f2f174553f03..628e6c2a09e31 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/mod.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/mod.rs @@ -1995,7 +1995,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.assert_local(), + ), traits::ObligationCauseCode::RepeatVec(should_suggest), ), self.param_env, diff --git a/src/librustc_mir/borrow_check/nll/universal_regions.rs b/src/librustc_mir/borrow_check/nll/universal_regions.rs index b876fd83ec983..ff11c7818baf9 100644 --- a/src/librustc_mir/borrow_check/nll/universal_regions.rs +++ b/src/librustc_mir/borrow_check/nll/universal_regions.rs @@ -784,10 +784,10 @@ fn for_each_late_bound_region_defined_on<'tcx>( fn_def_id: DefId, mut f: impl FnMut(ty::Region<'tcx>), ) { - if let Some(late_bounds) = tcx.is_late_bound_map(fn_def_id.index) { + if let Some(late_bounds) = tcx.is_late_bound_map(fn_def_id.assert_local()) { for late_bound in late_bounds.iter() { let hir_id = HirId { - owner: fn_def_id.index, + owner: fn_def_id.assert_local(), local_id: *late_bound, }; let name = tcx.hir().name(hir_id); diff --git a/src/librustc_mir/hair/cx/expr.rs b/src/librustc_mir/hair/cx/expr.rs index 7bb96661bb746..b4e19dbb1cf47 100644 --- a/src/librustc_mir/hair/cx/expr.rs +++ b/src/librustc_mir/hair/cx/expr.rs @@ -10,7 +10,6 @@ use rustc::ty::{self, AdtKind, Ty}; use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, AutoBorrowMutability, PointerCast}; use rustc::ty::subst::{InternalSubsts, SubstsRef}; use rustc::hir; -use rustc::hir::def_id::LocalDefId; use rustc::mir::BorrowKind; use syntax_pos::Span; @@ -984,7 +983,7 @@ fn convert_var( let closure_def_id = cx.body_owner; let upvar_id = ty::UpvarId { var_path: ty::UpvarPath {hir_id: var_hir_id}, - closure_expr_id: LocalDefId::from_def_id(closure_def_id), + closure_expr_id: closure_def_id.assert_local(), }; let var_ty = cx.tables().node_type(var_hir_id); @@ -1185,7 +1184,7 @@ fn capture_upvar<'tcx>( ) -> ExprRef<'tcx> { let upvar_id = ty::UpvarId { var_path: ty::UpvarPath { hir_id: var_hir_id }, - closure_expr_id: cx.tcx.hir().local_def_id(closure_expr.hir_id).to_local(), + closure_expr_id: cx.tcx.hir().local_def_id(closure_expr.hir_id).assert_local(), }; let upvar_capture = cx.tables().upvar_capture(upvar_id); let temp_lifetime = cx.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id); diff --git a/src/librustc_passes/entry.rs b/src/librustc_passes/entry.rs index 660047190608d..f2174689a2dcf 100644 --- a/src/librustc_passes/entry.rs +++ b/src/librustc_passes/entry.rs @@ -33,7 +33,7 @@ struct EntryContext<'a, 'tcx> { impl<'a, 'tcx> ItemLikeVisitor<'tcx> for EntryContext<'a, 'tcx> { fn visit_item(&mut self, item: &'tcx Item) { let def_id = self.map.local_def_id(item.hir_id); - let def_key = self.map.def_key(def_id); + let def_key = self.map.def_key(def_id.assert_local()); let at_root = def_key.parent == Some(CRATE_DEF_INDEX); find_item(item, self, at_root); } diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 5c996bffb9ad9..7a73da055f58e 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -26,7 +26,7 @@ use rustc::session::Session; use rustc::lint; use rustc::hir::def::{self, DefKind, PartialRes, CtorKind, CtorOf, NonMacroAttrKind, ExportMap}; use rustc::hir::def::Namespace::*; -use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, CrateNum, DefId}; +use rustc::hir::def_id::{CRATE_DEF_INDEX, CrateNum, DefId}; use rustc::hir::{TraitMap, GlobMap}; use rustc::ty::{self, DefIdTree, ResolverOutputs}; use rustc::util::nodemap::{NodeMap, NodeSet, FxHashMap, FxHashSet, DefIdMap}; @@ -1010,9 +1010,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 }) } } diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index 35870abbaefbd..3eb1fd3dd415a 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -1545,7 +1545,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } fn point_at_param_definition(&self, err: &mut DiagnosticBuilder<'_>, param: ty::ParamTy) { - let generics = self.tcx.generics_of(self.body_id.owner_def_id()); + let generics = self.tcx.generics_of(self.body_id.owner.to_def_id()); let generic_param = generics.type_param(¶m, self.tcx); if let ty::GenericParamDefKind::Type{synthetic: Some(..), ..} = generic_param.kind { return; diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 26f040810462f..3f199098f80de 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -645,9 +645,8 @@ pub struct InheritedBuilder<'tcx> { impl Inherited<'_, 'tcx> { pub fn build(tcx: TyCtxt<'tcx>, def_id: DefId) -> InheritedBuilder<'tcx> { - let hir_id_root = if def_id.is_local() { - let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); - DefId::local(hir_id.owner) + let hir_id_root = if let Some(def_id) = def_id.as_local() { + tcx.hir().local_def_id_to_hir_id(def_id).owner.to_def_id() } else { def_id }; @@ -967,7 +966,7 @@ fn typeck_tables_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::TypeckTables<'_> { // Consistency check our TypeckTables instance can hold all ItemLocalIds // it will need to hold. - assert_eq!(tables.local_id_root, Some(DefId::local(id.owner))); + assert_eq!(tables.local_id_root, Some(id.owner.to_def_id())); tables } diff --git a/src/librustc_typeck/check/upvar.rs b/src/librustc_typeck/check/upvar.rs index 97e59664df0c0..a5542ccc8e93b 100644 --- a/src/librustc_typeck/check/upvar.rs +++ b/src/librustc_typeck/check/upvar.rs @@ -133,7 +133,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { var_path: ty::UpvarPath { hir_id: var_hir_id, }, - closure_expr_id: LocalDefId::from_def_id(closure_def_id), + closure_expr_id: closure_def_id.assert_local(), }; debug!("seed upvar_id {:?}", upvar_id); // Adding the upvar Id to the list of Upvars, which will be added @@ -261,7 +261,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let upvar_ty = self.node_ty(var_hir_id); let upvar_id = ty::UpvarId { var_path: ty::UpvarPath { hir_id: var_hir_id }, - closure_expr_id: LocalDefId::from_def_id(closure_def_id), + closure_expr_id: closure_def_id.assert_local(), }; let capture = self.tables.borrow().upvar_capture(upvar_id); diff --git a/src/librustc_typeck/check/writeback.rs b/src/librustc_typeck/check/writeback.rs index 3d380a5f1826c..08c2a9d00a9d1 100644 --- a/src/librustc_typeck/check/writeback.rs +++ b/src/librustc_typeck/check/writeback.rs @@ -4,7 +4,6 @@ use crate::check::FnCtxt; use rustc::hir; -use rustc::hir::def_id::{DefId, DefIndex}; use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor}; use rustc::infer::InferCtxt; use rustc::ty::adjustment::{Adjust, Adjustment, PointerCast}; @@ -110,11 +109,11 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> { body: &'tcx hir::Body, rustc_dump_user_substs: bool, ) -> WritebackCx<'cx, 'tcx> { - let owner = body.id().hir_id; + let owner = body.id().hir_id.owner; WritebackCx { fcx, - tables: ty::TypeckTables::empty(Some(DefId::local(owner.owner))), + tables: ty::TypeckTables::empty(Some(owner.to_def_id())), body, rustc_dump_user_substs, } @@ -358,7 +357,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> { for (&id, &origin) in fcx_tables.closure_kind_origins().iter() { let hir_id = hir::HirId { - owner: common_local_id_root.index, + owner: common_local_id_root.assert_local(), local_id: id, }; self.tables @@ -390,7 +389,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> { let mut errors_buffer = Vec::new(); for (&local_id, c_ty) in fcx_tables.user_provided_types().iter() { let hir_id = hir::HirId { - owner: common_local_id_root.index, + owner: common_local_id_root.assert_local(), local_id, }; @@ -621,7 +620,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> { for (&local_id, fn_sig) in fcx_tables.liberated_fn_sigs().iter() { let hir_id = hir::HirId { - owner: common_local_id_root.index, + owner: common_local_id_root.assert_local(), local_id, }; let fn_sig = self.resolve(fn_sig, &hir_id); @@ -638,7 +637,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> { for (&local_id, ftys) in fcx_tables.fru_field_types().iter() { let hir_id = hir::HirId { - owner: common_local_id_root.index, + owner: common_local_id_root.assert_local(), local_id, }; let ftys = self.resolve(ftys, &hir_id); @@ -672,13 +671,6 @@ impl Locatable for Span { } } -impl Locatable for DefIndex { - fn to_span(&self, tcx: TyCtxt<'_>) -> Span { - let hir_id = tcx.hir().def_index_to_hir_id(*self); - tcx.hir().span(hir_id) - } -} - impl Locatable for hir::HirId { fn to_span(&self, tcx: TyCtxt<'_>) -> Span { tcx.hir().span(*self) diff --git a/src/test/mir-opt/inline-closure-borrows-arg.rs b/src/test/mir-opt/inline-closure-borrows-arg.rs index 491130b7c5d32..3ff148e0a55c4 100644 --- a/src/test/mir-opt/inline-closure-borrows-arg.rs +++ b/src/test/mir-opt/inline-closure-borrows-arg.rs @@ -1,3 +1,4 @@ +// ignore-tidy-linelength // compile-flags: -Z span_free_formats // Tests that MIR inliner can handle closure arguments, @@ -20,7 +21,7 @@ fn foo(_t: T, q: &i32) -> i32 { // ... // bb0: { // ... -// _3 = [closure@HirId { owner: DefIndex(4), local_id: 31 }]; +// _3 = [closure@HirId { owner: DefId(0:4 ~ inline_closure_borrows_arg[317d]::foo[0]), local_id: 31 }]; // ... // _4 = &_3; // ... diff --git a/src/test/mir-opt/inline-closure.rs b/src/test/mir-opt/inline-closure.rs index 7c0259b643a63..d860bee96848e 100644 --- a/src/test/mir-opt/inline-closure.rs +++ b/src/test/mir-opt/inline-closure.rs @@ -16,7 +16,7 @@ fn foo(_t: T, q: i32) -> i32 { // ... // bb0: { // ... -// _3 = [closure@HirId { owner: DefIndex(4), local_id: 15 }]; +// _3 = [closure@HirId { owner: DefId(0:4 ~ inline_closure[317d]::foo[0]), local_id: 15 }]; // ... // _4 = &_3; // ... diff --git a/src/test/mir-opt/retag.rs b/src/test/mir-opt/retag.rs index 96b848eb1d41c..7e3ff7fb9f09f 100644 --- a/src/test/mir-opt/retag.rs +++ b/src/test/mir-opt/retag.rs @@ -100,7 +100,7 @@ fn main() { // } // END rustc.main.EraseRegions.after.mir // START rustc.main-{{closure}}.EraseRegions.after.mir -// fn main::{{closure}}#0(_1: &[closure@HirId { owner: DefIndex(13), local_id: 72 }], _2: &i32) -> &i32 { +// fn main::{{closure}}#0(_1: &[closure@HirId { owner: DefId(0:13 ~ retag[317d]::main[0]), local_id: 72 }], _2: &i32) -> &i32 { // ... // bb0: { // Retag([fn entry] _1);