From 42b2adfab0e77c4662021badcf765a445681a12e Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Sun, 3 Nov 2019 20:48:08 +0200 Subject: [PATCH] rustc: introduce DefId::as_local(self) -> Option and use it. --- src/librustc/hir/map/hir_id_validator.rs | 16 +++++++++++----- src/librustc/hir/map/mod.rs | 12 +++++------- src/librustc/ich/hcx.rs | 4 ++-- src/librustc/ty/context.rs | 12 ++++++++---- src/librustc_mir/borrow_check/nll.rs | 2 +- src/librustc_passes/entry.rs | 2 +- src/librustc_span/def_id.rs | 8 ++++++-- src/librustc_typeck/check/mod.rs | 5 ++--- 8 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/librustc/hir/map/hir_id_validator.rs b/src/librustc/hir/map/hir_id_validator.rs index 796f48954726..cc3e11d9af99 100644 --- a/src/librustc/hir/map/hir_id_validator.rs +++ b/src/librustc/hir/map/hir_id_validator.rs @@ -3,7 +3,7 @@ use crate::ty::TyCtxt; use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::sync::{par_iter, Lock, ParallelIterator}; use rustc_hir as hir; -use rustc_hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX}; +use rustc_hir::def_id::{DefIndex, LocalDefId, CRATE_DEF_INDEX}; use rustc_hir::intravisit; use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_hir::{HirId, ItemLocalId}; @@ -113,14 +113,18 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> { missing_items.push(format!( "[local_id: {}, owner: {}]", local_id, - self.hir_map.def_path(DefId::local(owner_def_index)).to_string_no_crate() + self.hir_map + .def_path(LocalDefId { local_def_index: owner_def_index }) + .to_string_no_crate() )); } 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(LocalDefId { local_def_index: owner_def_index }) + .to_string_no_crate(), max, missing_items, self.hir_ids_seen @@ -159,8 +163,10 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> { 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_local_def_id()).to_string_no_crate(), + self.hir_map + .def_path(LocalDefId { local_def_index: owner }) + .to_string_no_crate() ) }); } diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 636044069e4d..dbe23edd8a46 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -188,18 +188,16 @@ impl<'hir> Map<'hir> { &self.tcx.definitions } - pub fn def_key(&self, def_id: DefId) -> DefKey { - assert!(def_id.is_local()); - self.tcx.definitions.def_key(def_id.index) + pub fn def_key(&self, def_id: LocalDefId) -> DefKey { + self.tcx.definitions.def_key(def_id.local_def_index) } 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.opt_local_def_id(id).map(|def_id| self.def_path(def_id.expect_local())) } - pub fn def_path(&self, def_id: DefId) -> DefPath { - assert!(def_id.is_local()); - self.tcx.definitions.def_path(def_id.index) + pub fn def_path(&self, def_id: LocalDefId) -> DefPath { + self.tcx.definitions.def_path(def_id.local_def_index) } #[inline] diff --git a/src/librustc/ich/hcx.rs b/src/librustc/ich/hcx.rs index 8fd86b3232d0..09654478791e 100644 --- a/src/librustc/ich/hcx.rs +++ b/src/librustc/ich/hcx.rs @@ -123,8 +123,8 @@ 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.local_def_index) } else { self.cstore.def_path_hash(def_id) } diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index 9f5197f3db68..78e7d1f3bd83 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -1261,7 +1261,7 @@ impl<'tcx> TyCtxt<'tcx> { } pub fn def_key(self, id: DefId) -> hir_map::DefKey { - if id.is_local() { self.hir().def_key(id) } else { self.cstore.def_key(id) } + if let Some(id) = id.as_local() { self.hir().def_key(id) } else { self.cstore.def_key(id) } } /// Converts a `DefId` into its fully expanded `DefPath` (every @@ -1270,7 +1270,11 @@ 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() { self.hir().def_path(id) } else { self.cstore.def_path(id) } + if let Some(id) = id.as_local() { + self.hir().def_path(id) + } else { + self.cstore.def_path(id) + } } /// Returns whether or not the crate with CrateNum 'cnum' @@ -1281,8 +1285,8 @@ impl<'tcx> TyCtxt<'tcx> { #[inline] pub fn def_path_hash(self, def_id: DefId) -> hir_map::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.local_def_index) } else { self.cstore.def_path_hash(def_id) } diff --git a/src/librustc_mir/borrow_check/nll.rs b/src/librustc_mir/borrow_check/nll.rs index ba1b322524e8..077ed49ed2ca 100644 --- a/src/librustc_mir/borrow_check/nll.rs +++ b/src/librustc_mir/borrow_check/nll.rs @@ -272,7 +272,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_passes/entry.rs b/src/librustc_passes/entry.rs index 598d6bb3c483..7e0d0bfe9aba 100644 --- a/src/librustc_passes/entry.rs +++ b/src/librustc_passes/entry.rs @@ -34,7 +34,7 @@ struct EntryContext<'a, 'tcx> { impl<'a, 'tcx> ItemLikeVisitor<'tcx> for EntryContext<'a, 'tcx> { fn visit_item(&mut self, item: &'tcx Item<'tcx>) { 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.expect_local()); let at_root = def_key.parent == Some(CRATE_DEF_INDEX); find_item(item, self, at_root); } diff --git a/src/librustc_span/def_id.rs b/src/librustc_span/def_id.rs index 1aaec66722a7..f8570b981623 100644 --- a/src/librustc_span/def_id.rs +++ b/src/librustc_span/def_id.rs @@ -163,10 +163,14 @@ impl DefId { self.krate == LOCAL_CRATE } + #[inline] + pub fn as_local(self) -> Option { + if self.is_local() { Some(LocalDefId { local_def_index: self.index }) } else { None } + } + #[inline] pub fn expect_local(self) -> LocalDefId { - assert!(self.is_local()); - LocalDefId { local_def_index: self.index } + self.as_local().unwrap_or_else(|| panic!("DefId::expect_local: `{:?}` isn't local", self)) } pub fn is_top_level_module(self) -> bool { diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index bfb0d25dea20..e6d492110fb2 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -638,9 +638,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_def_id() } else { def_id };