Skip to content

Commit 42b2adf

Browse files
committed
rustc: introduce DefId::as_local(self) -> Option<LocalDefId> and use it.
1 parent 0c69279 commit 42b2adf

File tree

8 files changed

+36
-25
lines changed

8 files changed

+36
-25
lines changed

src/librustc/hir/map/hir_id_validator.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::ty::TyCtxt;
33
use rustc_data_structures::fx::FxHashSet;
44
use rustc_data_structures::sync::{par_iter, Lock, ParallelIterator};
55
use rustc_hir as hir;
6-
use rustc_hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
6+
use rustc_hir::def_id::{DefIndex, LocalDefId, CRATE_DEF_INDEX};
77
use rustc_hir::intravisit;
88
use rustc_hir::itemlikevisit::ItemLikeVisitor;
99
use rustc_hir::{HirId, ItemLocalId};
@@ -113,14 +113,18 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> {
113113
missing_items.push(format!(
114114
"[local_id: {}, owner: {}]",
115115
local_id,
116-
self.hir_map.def_path(DefId::local(owner_def_index)).to_string_no_crate()
116+
self.hir_map
117+
.def_path(LocalDefId { local_def_index: owner_def_index })
118+
.to_string_no_crate()
117119
));
118120
}
119121
self.error(|| {
120122
format!(
121123
"ItemLocalIds not assigned densely in {}. \
122124
Max ItemLocalId = {}, missing IDs = {:?}; seens IDs = {:?}",
123-
self.hir_map.def_path(DefId::local(owner_def_index)).to_string_no_crate(),
125+
self.hir_map
126+
.def_path(LocalDefId { local_def_index: owner_def_index })
127+
.to_string_no_crate(),
124128
max,
125129
missing_items,
126130
self.hir_ids_seen
@@ -159,8 +163,10 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
159163
format!(
160164
"HirIdValidator: The recorded owner of {} is {} instead of {}",
161165
self.hir_map.node_to_string(hir_id),
162-
self.hir_map.def_path(DefId::local(hir_id.owner)).to_string_no_crate(),
163-
self.hir_map.def_path(DefId::local(owner)).to_string_no_crate()
166+
self.hir_map.def_path(hir_id.owner_local_def_id()).to_string_no_crate(),
167+
self.hir_map
168+
.def_path(LocalDefId { local_def_index: owner })
169+
.to_string_no_crate()
164170
)
165171
});
166172
}

src/librustc/hir/map/mod.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,16 @@ impl<'hir> Map<'hir> {
188188
&self.tcx.definitions
189189
}
190190

191-
pub fn def_key(&self, def_id: DefId) -> DefKey {
192-
assert!(def_id.is_local());
193-
self.tcx.definitions.def_key(def_id.index)
191+
pub fn def_key(&self, def_id: LocalDefId) -> DefKey {
192+
self.tcx.definitions.def_key(def_id.local_def_index)
194193
}
195194

196195
pub fn def_path_from_hir_id(&self, id: HirId) -> Option<DefPath> {
197-
self.opt_local_def_id(id).map(|def_id| self.def_path(def_id))
196+
self.opt_local_def_id(id).map(|def_id| self.def_path(def_id.expect_local()))
198197
}
199198

200-
pub fn def_path(&self, def_id: DefId) -> DefPath {
201-
assert!(def_id.is_local());
202-
self.tcx.definitions.def_path(def_id.index)
199+
pub fn def_path(&self, def_id: LocalDefId) -> DefPath {
200+
self.tcx.definitions.def_path(def_id.local_def_index)
203201
}
204202

205203
#[inline]

src/librustc/ich/hcx.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ impl<'a> StableHashingContext<'a> {
123123

124124
#[inline]
125125
pub fn def_path_hash(&self, def_id: DefId) -> DefPathHash {
126-
if def_id.is_local() {
127-
self.definitions.def_path_hash(def_id.index)
126+
if let Some(def_id) = def_id.as_local() {
127+
self.definitions.def_path_hash(def_id.local_def_index)
128128
} else {
129129
self.cstore.def_path_hash(def_id)
130130
}

src/librustc/ty/context.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,7 @@ impl<'tcx> TyCtxt<'tcx> {
12611261
}
12621262

12631263
pub fn def_key(self, id: DefId) -> hir_map::DefKey {
1264-
if id.is_local() { self.hir().def_key(id) } else { self.cstore.def_key(id) }
1264+
if let Some(id) = id.as_local() { self.hir().def_key(id) } else { self.cstore.def_key(id) }
12651265
}
12661266

12671267
/// Converts a `DefId` into its fully expanded `DefPath` (every
@@ -1270,7 +1270,11 @@ impl<'tcx> TyCtxt<'tcx> {
12701270
/// Note that if `id` is not local to this crate, the result will
12711271
/// be a non-local `DefPath`.
12721272
pub fn def_path(self, id: DefId) -> hir_map::DefPath {
1273-
if id.is_local() { self.hir().def_path(id) } else { self.cstore.def_path(id) }
1273+
if let Some(id) = id.as_local() {
1274+
self.hir().def_path(id)
1275+
} else {
1276+
self.cstore.def_path(id)
1277+
}
12741278
}
12751279

12761280
/// Returns whether or not the crate with CrateNum 'cnum'
@@ -1281,8 +1285,8 @@ impl<'tcx> TyCtxt<'tcx> {
12811285

12821286
#[inline]
12831287
pub fn def_path_hash(self, def_id: DefId) -> hir_map::DefPathHash {
1284-
if def_id.is_local() {
1285-
self.definitions.def_path_hash(def_id.index)
1288+
if let Some(def_id) = def_id.as_local() {
1289+
self.definitions.def_path_hash(def_id.local_def_index)
12861290
} else {
12871291
self.cstore.def_path_hash(def_id)
12881292
}

src/librustc_mir/borrow_check/nll.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
272272
// Dump facts if requested.
273273
let polonius_output = all_facts.and_then(|all_facts| {
274274
if infcx.tcx.sess.opts.debugging_opts.nll_facts {
275-
let def_path = infcx.tcx.hir().def_path(def_id);
275+
let def_path = infcx.tcx.def_path(def_id);
276276
let dir_path =
277277
PathBuf::from("nll-facts").join(def_path.to_filename_friendly_no_crate());
278278
all_facts.write_to_dir(dir_path, location_table).unwrap();

src/librustc_passes/entry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct EntryContext<'a, 'tcx> {
3434
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for EntryContext<'a, 'tcx> {
3535
fn visit_item(&mut self, item: &'tcx Item<'tcx>) {
3636
let def_id = self.map.local_def_id(item.hir_id);
37-
let def_key = self.map.def_key(def_id);
37+
let def_key = self.map.def_key(def_id.expect_local());
3838
let at_root = def_key.parent == Some(CRATE_DEF_INDEX);
3939
find_item(item, self, at_root);
4040
}

src/librustc_span/def_id.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,14 @@ impl DefId {
163163
self.krate == LOCAL_CRATE
164164
}
165165

166+
#[inline]
167+
pub fn as_local(self) -> Option<LocalDefId> {
168+
if self.is_local() { Some(LocalDefId { local_def_index: self.index }) } else { None }
169+
}
170+
166171
#[inline]
167172
pub fn expect_local(self) -> LocalDefId {
168-
assert!(self.is_local());
169-
LocalDefId { local_def_index: self.index }
173+
self.as_local().unwrap_or_else(|| panic!("DefId::expect_local: `{:?}` isn't local", self))
170174
}
171175

172176
pub fn is_top_level_module(self) -> bool {

src/librustc_typeck/check/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -638,9 +638,8 @@ pub struct InheritedBuilder<'tcx> {
638638

639639
impl Inherited<'_, 'tcx> {
640640
pub fn build(tcx: TyCtxt<'tcx>, def_id: DefId) -> InheritedBuilder<'tcx> {
641-
let hir_id_root = if def_id.is_local() {
642-
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
643-
DefId::local(hir_id.owner)
641+
let hir_id_root = if let Some(def_id) = def_id.as_local() {
642+
tcx.hir().local_def_id_to_hir_id(def_id).owner_def_id()
644643
} else {
645644
def_id
646645
};

0 commit comments

Comments
 (0)