Skip to content

Commit

Permalink
Auto merge of #66131 - eddyb:local-def-id, r=<try>
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Nov 6, 2019
2 parents 3f0e164 + f1a113e commit 5e1cb0a
Show file tree
Hide file tree
Showing 42 changed files with 301 additions and 316 deletions.
14 changes: 7 additions & 7 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

use crate::mir;
use crate::mir::interpret::GlobalId;
use crate::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX};
use crate::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LocalDefId};
use crate::hir::map::DefPathHash;
use crate::hir::HirId;

Expand Down Expand Up @@ -447,9 +447,9 @@ impl RecoverKey<'tcx> for DefId {
}
}

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

Expand Down Expand Up @@ -501,15 +501,15 @@ impl<'tcx> DepNodeParams<'tcx> for DefId {
}
}

impl<'tcx> DepNodeParams<'tcx> for DefIndex {
impl<'tcx> DepNodeParams<'tcx> for LocalDefId {
const CAN_RECONSTRUCT_QUERY_KEY: bool = true;

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

fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
tcx.def_path_str(DefId::local(*self))
self.to_def_id().to_debug_str(tcx)
}
}

Expand Down Expand Up @@ -565,7 +565,7 @@ impl<'tcx> DepNodeParams<'tcx> for HirId {
local_id,
} = *self;

let def_path_hash = tcx.def_path_hash(DefId::local(owner));
let def_path_hash = tcx.def_path_hash(owner.to_def_id());
let local_id = Fingerprint::from_smaller_hash(local_id.as_u32().into());

def_path_hash.0.combine(local_id)
Expand Down
31 changes: 20 additions & 11 deletions src/librustc/hir/def_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,21 @@ impl DefId {
}

#[inline]
pub fn to_local(self) -> LocalDefId {
LocalDefId::from_def_id(self)
pub fn as_local(self) -> Option<LocalDefId> {
if self.is_local() {
Some(LocalDefId {
index: self.index,
})
} else {
None
}
}

#[inline]
pub fn assert_local(self) -> LocalDefId {
self.as_local().unwrap_or_else(|| {
bug!("DefId::assert_local: `{:?}` isn't local", self)
})
}

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

impl LocalDefId {
#[inline]
pub fn from_def_id(def_id: DefId) -> LocalDefId {
assert!(def_id.is_local());
LocalDefId(def_id.index)
}

#[inline]
pub fn to_def_id(self) -> DefId {
DefId {
krate: LOCAL_CRATE,
index: self.0
index: self.index
}
}
}
Expand Down
Loading

0 comments on commit 5e1cb0a

Please sign in to comment.