rustc: introduce DefId::as_local(self) -> Option<LocalDefId> and use it.

This commit is contained in:
Eduard-Mihai Burtescu 2019-11-03 20:48:08 +02:00
parent 0c692797d7
commit 42b2adfab0
8 changed files with 36 additions and 25 deletions

View file

@ -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()
)
});
}

View file

@ -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<DefPath> {
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]

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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();

View file

@ -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);
}

View file

@ -163,10 +163,14 @@ impl DefId {
self.krate == LOCAL_CRATE
}
#[inline]
pub fn as_local(self) -> Option<LocalDefId> {
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 {

View file

@ -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
};