Skip to content

Commit 5e1cb0a

Browse files
committed
Auto merge of #66131 - eddyb:local-def-id, r=<try>
rustc: use LocalDefId instead of DefIndex where possible. That is, wherever `DefIndex` always referred to a "def" in the local crate, I replaced it with `LocalDefId`. While `LocalDefId` already existed, it wasn't used a lot, but I hope I'm on the right track. Unresolved questions: * should `LocalDefId` implement `rustc_index::Idx`? * this would get rid of a couple more `DefIndex` uses * should `LocalDefId` be encoded/decoded as just a `DefIndex`? * right now it's a bit messy, `LocalDefId` encodes/decodes like `DefId` * should `DefId::assert_local` be named something else, like `expect_local`? * what do we do with `tcx.hir().local_def_id(...)`? * right now it returns `DefId`, but changing it in this PR would be noisy * ideally it would return `LocalDefId` to spare the caller of `.assert_local()` r? @michaelwoerister cc @nikomatsakis @petrochenkov @Zoxc
2 parents 3f0e164 + f1a113e commit 5e1cb0a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+301
-316
lines changed

src/librustc/dep_graph/dep_node.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
5252
use crate::mir;
5353
use crate::mir::interpret::GlobalId;
54-
use crate::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX};
54+
use crate::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LocalDefId};
5555
use crate::hir::map::DefPathHash;
5656
use crate::hir::HirId;
5757

@@ -447,9 +447,9 @@ impl RecoverKey<'tcx> for DefId {
447447
}
448448
}
449449

450-
impl RecoverKey<'tcx> for DefIndex {
450+
impl RecoverKey<'tcx> for LocalDefId {
451451
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
452-
dep_node.extract_def_id(tcx).map(|id| id.index)
452+
dep_node.extract_def_id(tcx).map(|id| id.assert_local())
453453
}
454454
}
455455

@@ -501,15 +501,15 @@ impl<'tcx> DepNodeParams<'tcx> for DefId {
501501
}
502502
}
503503

504-
impl<'tcx> DepNodeParams<'tcx> for DefIndex {
504+
impl<'tcx> DepNodeParams<'tcx> for LocalDefId {
505505
const CAN_RECONSTRUCT_QUERY_KEY: bool = true;
506506

507507
fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
508-
tcx.hir().definitions().def_path_hash(*self).0
508+
self.to_def_id().to_fingerprint(tcx)
509509
}
510510

511511
fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
512-
tcx.def_path_str(DefId::local(*self))
512+
self.to_def_id().to_debug_str(tcx)
513513
}
514514
}
515515

@@ -565,7 +565,7 @@ impl<'tcx> DepNodeParams<'tcx> for HirId {
565565
local_id,
566566
} = *self;
567567

568-
let def_path_hash = tcx.def_path_hash(DefId::local(owner));
568+
let def_path_hash = tcx.def_path_hash(owner.to_def_id());
569569
let local_id = Fingerprint::from_smaller_hash(local_id.as_u32().into());
570570

571571
def_path_hash.0.combine(local_id)

src/librustc/hir/def_id.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,21 @@ impl DefId {
139139
}
140140

141141
#[inline]
142-
pub fn to_local(self) -> LocalDefId {
143-
LocalDefId::from_def_id(self)
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
149+
}
150+
}
151+
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+
})
144157
}
145158

146159
pub fn describe_as_module(&self, tcx: TyCtxt<'_>) -> String {
@@ -161,21 +174,17 @@ impl rustc_serialize::UseSpecializedDecodable for DefId {}
161174
/// few cases where we know that only DefIds from the local crate are expected
162175
/// and a DefId from a different crate would signify a bug somewhere. This
163176
/// is when LocalDefId comes in handy.
164-
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
165-
pub struct LocalDefId(DefIndex);
177+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
178+
pub struct LocalDefId {
179+
pub index: DefIndex,
180+
}
166181

167182
impl LocalDefId {
168-
#[inline]
169-
pub fn from_def_id(def_id: DefId) -> LocalDefId {
170-
assert!(def_id.is_local());
171-
LocalDefId(def_id.index)
172-
}
173-
174183
#[inline]
175184
pub fn to_def_id(self) -> DefId {
176185
DefId {
177186
krate: LOCAL_CRATE,
178-
index: self.0
187+
index: self.index
179188
}
180189
}
181190
}

0 commit comments

Comments
 (0)