Skip to content

[WIP] rustc: push LocalDefId and/or assert_local calls farther back. #66132

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 10 commits into from
Closed
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