Skip to content

Commit 82c868f

Browse files
committed
rustc: introduce DefId::as_local(self) -> Option<LocalDefId> and use it.
1 parent 01fe094 commit 82c868f

File tree

9 files changed

+30
-23
lines changed

9 files changed

+30
-23
lines changed

src/librustc/hir/def_id.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,23 @@ impl DefId {
139139
}
140140

141141
#[inline]
142-
pub fn assert_local(self) -> LocalDefId {
143-
assert!(self.is_local());
144-
LocalDefId {
145-
index: self.index,
142+
pub fn as_local(self) -> Option<LocalDefId> {
143+
if self.is_local() {
144+
Some(LocalDefId {
145+
index: self.index,
146+
})
147+
} else {
148+
None
146149
}
147150
}
148151

152+
#[inline]
153+
pub fn assert_local(self) -> LocalDefId {
154+
self.as_local().unwrap_or_else(|| {
155+
bug!("DefId::assert_local: `{:?}` isn't local", self)
156+
})
157+
}
158+
149159
pub fn describe_as_module(&self, tcx: TyCtxt<'_>) -> String {
150160
if self.is_local() && self.index == CRATE_DEF_INDEX {
151161
format!("top-level module")

src/librustc/hir/lowering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ impl<'a> LoweringContext<'a> {
975975
}
976976

977977
fn def_key(&mut self, id: DefId) -> DefKey {
978-
if id.is_local() {
978+
if let Some(id) = id.as_local() {
979979
self.resolver.definitions().def_key(id.index)
980980
} else {
981981
self.resolver.cstore().def_key(id)

src/librustc/hir/map/hir_id_validator.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
1+
use crate::hir::def_id::{DefIndex, CRATE_DEF_INDEX, LocalDefId};
22
use crate::hir::{self, intravisit, HirId, ItemLocalId};
33
use crate::hir::itemlikevisit::ItemLikeVisitor;
44
use rustc_data_structures::fx::FxHashSet;
@@ -119,7 +119,7 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> {
119119
self.error(|| format!(
120120
"ItemLocalIds not assigned densely in {}. \
121121
Max ItemLocalId = {}, missing IDs = {:?}; seens IDs = {:?}",
122-
self.hir_map.def_path(DefId::local(owner_def_index)).to_string_no_crate(),
122+
self.hir_map.def_path(LocalDefId { index: owner_def_index }).to_string_no_crate(),
123123
max,
124124
missing_items,
125125
self.hir_ids_seen
@@ -154,8 +154,8 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
154154
self.error(|| format!(
155155
"HirIdValidator: The recorded owner of {} is {} instead of {}",
156156
self.hir_map.node_to_string(hir_id),
157-
self.hir_map.def_path(DefId::local(hir_id.owner)).to_string_no_crate(),
158-
self.hir_map.def_path(DefId::local(owner)).to_string_no_crate()));
157+
self.hir_map.def_path(hir_id.owner_local_def_id()).to_string_no_crate(),
158+
self.hir_map.def_path(LocalDefId { index: owner }).to_string_no_crate()));
159159
}
160160

161161
self.hir_ids_seen.insert(hir_id.local_id);

src/librustc/hir/map/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,19 +246,17 @@ impl<'hir> Map<'hir> {
246246
self.definitions
247247
}
248248

249-
pub fn def_key(&self, def_id: DefId) -> DefKey {
250-
assert!(def_id.is_local());
249+
pub fn def_key(&self, def_id: LocalDefId) -> DefKey {
251250
self.definitions.def_key(def_id.index)
252251
}
253252

254253
pub fn def_path_from_hir_id(&self, id: HirId) -> Option<DefPath> {
255254
self.opt_local_def_id(id).map(|def_id| {
256-
self.def_path(def_id)
255+
self.def_path(def_id.assert_local())
257256
})
258257
}
259258

260-
pub fn def_path(&self, def_id: DefId) -> DefPath {
261-
assert!(def_id.is_local());
259+
pub fn def_path(&self, def_id: LocalDefId) -> DefPath {
262260
self.definitions.def_path(def_id.index)
263261
}
264262

src/librustc/ich/hcx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<'a> StableHashingContext<'a> {
131131

132132
#[inline]
133133
pub fn def_path_hash(&self, def_id: DefId) -> DefPathHash {
134-
if def_id.is_local() {
134+
if let Some(def_id) = def_id.as_local() {
135135
self.definitions.def_path_hash(def_id.index)
136136
} else {
137137
self.cstore.def_path_hash(def_id)

src/librustc/ty/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ impl<'tcx> TyCtxt<'tcx> {
13431343
}
13441344

13451345
pub fn def_key(self, id: DefId) -> hir_map::DefKey {
1346-
if id.is_local() {
1346+
if let Some(id) = id.as_local() {
13471347
self.hir().def_key(id)
13481348
} else {
13491349
self.cstore.def_key(id)
@@ -1356,7 +1356,7 @@ impl<'tcx> TyCtxt<'tcx> {
13561356
/// Note that if `id` is not local to this crate, the result will
13571357
/// be a non-local `DefPath`.
13581358
pub fn def_path(self, id: DefId) -> hir_map::DefPath {
1359-
if id.is_local() {
1359+
if let Some(id) = id.as_local() {
13601360
self.hir().def_path(id)
13611361
} else {
13621362
self.cstore.def_path(id)
@@ -1375,7 +1375,7 @@ impl<'tcx> TyCtxt<'tcx> {
13751375

13761376
#[inline]
13771377
pub fn def_path_hash(self, def_id: DefId) -> hir_map::DefPathHash {
1378-
if def_id.is_local() {
1378+
if let Some(def_id) = def_id.as_local() {
13791379
self.hir().definitions().def_path_hash(def_id.index)
13801380
} else {
13811381
self.cstore.def_path_hash(def_id)

src/librustc_mir/borrow_check/nll/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>(
258258
// Dump facts if requested.
259259
let polonius_output = all_facts.and_then(|all_facts| {
260260
if infcx.tcx.sess.opts.debugging_opts.nll_facts {
261-
let def_path = infcx.tcx.hir().def_path(def_id);
261+
let def_path = infcx.tcx.def_path(def_id);
262262
let dir_path =
263263
PathBuf::from("nll-facts").join(def_path.to_filename_friendly_no_crate());
264264
all_facts.write_to_dir(dir_path, location_table).unwrap();

src/librustc_passes/entry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct EntryContext<'a, 'tcx> {
3333
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for EntryContext<'a, 'tcx> {
3434
fn visit_item(&mut self, item: &'tcx Item) {
3535
let def_id = self.map.local_def_id(item.hir_id);
36-
let def_key = self.map.def_key(def_id);
36+
let def_key = self.map.def_key(def_id.assert_local());
3737
let at_root = def_key.parent == Some(CRATE_DEF_INDEX);
3838
find_item(item, self, at_root);
3939
}

src/librustc_typeck/check/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,9 +645,8 @@ pub struct InheritedBuilder<'tcx> {
645645

646646
impl Inherited<'_, 'tcx> {
647647
pub fn build(tcx: TyCtxt<'tcx>, def_id: DefId) -> InheritedBuilder<'tcx> {
648-
let hir_id_root = if def_id.is_local() {
649-
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
650-
DefId::local(hir_id.owner)
648+
let hir_id_root = if let Some(def_id) = def_id.as_local() {
649+
tcx.hir().local_def_id_to_hir_id(def_id).owner_def_id()
651650
} else {
652651
def_id
653652
};

0 commit comments

Comments
 (0)