Introduce LocalDefId which provides a type-level guarantee that the DefId is from the local crate.
This commit is contained in:
parent
2f50e626e1
commit
24e54ddefa
11 changed files with 98 additions and 31 deletions
|
|
@ -92,6 +92,7 @@ impl serialize::UseSpecializedDecodable for CrateNum {
|
|||
/// don't have to care about these ranges.
|
||||
newtype_index!(DefIndex
|
||||
{
|
||||
ENCODABLE = custom
|
||||
DEBUG_FORMAT = custom,
|
||||
|
||||
/// The start of the "high" range of DefIndexes.
|
||||
|
|
@ -208,13 +209,20 @@ impl fmt::Debug for DefId {
|
|||
|
||||
impl DefId {
|
||||
/// Make a local `DefId` with the given index.
|
||||
#[inline]
|
||||
pub fn local(index: DefIndex) -> DefId {
|
||||
DefId { krate: LOCAL_CRATE, index: index }
|
||||
}
|
||||
|
||||
pub fn is_local(&self) -> bool {
|
||||
#[inline]
|
||||
pub fn is_local(self) -> bool {
|
||||
self.krate == LOCAL_CRATE
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn to_local(self) -> LocalDefId {
|
||||
LocalDefId::from_def_id(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl serialize::UseSpecializedEncodable for DefId {
|
||||
|
|
@ -242,3 +250,33 @@ impl serialize::UseSpecializedDecodable for DefId {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||
pub struct LocalDefId(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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for LocalDefId {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.to_def_id().fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl serialize::UseSpecializedEncodable for LocalDefId {}
|
||||
impl serialize::UseSpecializedDecodable for LocalDefId {}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ pub use self::definitions::{Definitions, DefKey, DefPath, DefPathData,
|
|||
|
||||
use dep_graph::{DepGraph, DepNode, DepKind, DepNodeIndex};
|
||||
|
||||
use hir::def_id::{CRATE_DEF_INDEX, DefId, DefIndexAddressSpace};
|
||||
use hir::def_id::{CRATE_DEF_INDEX, DefId, LocalDefId, DefIndexAddressSpace};
|
||||
|
||||
use syntax::abi::Abi;
|
||||
use syntax::ast::{self, Name, NodeId, CRATE_NODE_ID};
|
||||
|
|
@ -359,6 +359,16 @@ impl<'hir> Map<'hir> {
|
|||
self.definitions.as_local_node_id(DefId::local(def_index)).unwrap()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn local_def_id_to_hir_id(&self, def_id: LocalDefId) -> HirId {
|
||||
self.definitions.def_index_to_hir_id(def_id.to_def_id().index)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn local_def_id_to_node_id(&self, def_id: LocalDefId) -> NodeId {
|
||||
self.definitions.as_local_node_id(def_id.to_def_id()).unwrap()
|
||||
}
|
||||
|
||||
fn entry_count(&self) -> usize {
|
||||
self.map.len()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@
|
|||
|
||||
use hir;
|
||||
use hir::map::DefPathHash;
|
||||
use hir::def_id::{DefId, CrateNum, CRATE_DEF_INDEX};
|
||||
use hir::def_id::{DefId, LocalDefId, CrateNum, CRATE_DEF_INDEX};
|
||||
use ich::{StableHashingContext, NodeIdHashingMode};
|
||||
use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey,
|
||||
StableHasher, StableHasherResult};
|
||||
|
|
@ -38,6 +38,24 @@ impl<'gcx> ToStableHashKey<StableHashingContext<'gcx>> for DefId {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'gcx> HashStable<StableHashingContext<'gcx>> for LocalDefId {
|
||||
#[inline]
|
||||
fn hash_stable<W: StableHasherResult>(&self,
|
||||
hcx: &mut StableHashingContext<'gcx>,
|
||||
hasher: &mut StableHasher<W>) {
|
||||
hcx.def_path_hash(self.to_def_id()).hash_stable(hcx, hasher);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gcx> ToStableHashKey<StableHashingContext<'gcx>> for LocalDefId {
|
||||
type KeyType = DefPathHash;
|
||||
|
||||
#[inline]
|
||||
fn to_stable_hash_key(&self, hcx: &StableHashingContext<'gcx>) -> DefPathHash {
|
||||
hcx.def_path_hash(self.to_def_id())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'gcx> HashStable<StableHashingContext<'gcx>> for CrateNum {
|
||||
#[inline]
|
||||
fn hash_stable<W: StableHasherResult>(&self,
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ use self::TrackMatchMode::*;
|
|||
use self::OverloadedCallType::*;
|
||||
|
||||
use hir::def::Def;
|
||||
use hir::def_id::{DefId};
|
||||
use hir::def_id::DefId;
|
||||
use infer::InferCtxt;
|
||||
use middle::mem_categorization as mc;
|
||||
use middle::region;
|
||||
|
|
@ -915,7 +915,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
|
|||
let closure_def_id = self.tcx().hir.local_def_id(closure_expr.id);
|
||||
let upvar_id = ty::UpvarId {
|
||||
var_id: var_hir_id,
|
||||
closure_expr_id: closure_def_id.index
|
||||
closure_expr_id: closure_def_id.to_local(),
|
||||
};
|
||||
let upvar_capture = self.mc.tables.upvar_capture(upvar_id);
|
||||
let cmt_var = return_if_err!(self.cat_captured_var(closure_expr.id,
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ pub use self::Note::*;
|
|||
use self::Aliasability::*;
|
||||
|
||||
use middle::region;
|
||||
use hir::def_id::{DefId, DefIndex};
|
||||
use hir::def_id::{DefId, LocalDefId};
|
||||
use hir::map as hir_map;
|
||||
use infer::InferCtxt;
|
||||
use hir::def::{Def, CtorKind};
|
||||
|
|
@ -191,7 +191,7 @@ pub type cmt<'tcx> = Rc<cmt_<'tcx>>;
|
|||
|
||||
pub enum ImmutabilityBlame<'tcx> {
|
||||
ImmLocal(ast::NodeId),
|
||||
ClosureEnv(DefIndex),
|
||||
ClosureEnv(LocalDefId),
|
||||
LocalDeref(ast::NodeId),
|
||||
AdtFieldDeref(&'tcx ty::AdtDef, &'tcx ty::FieldDef)
|
||||
}
|
||||
|
|
@ -758,11 +758,11 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
|
|||
}
|
||||
};
|
||||
|
||||
let closure_expr_def_index = self.tcx.hir.local_def_id(fn_node_id).index;
|
||||
let closure_expr_def_id = self.tcx.hir.local_def_id(fn_node_id);
|
||||
let var_hir_id = self.tcx.hir.node_to_hir_id(var_id);
|
||||
let upvar_id = ty::UpvarId {
|
||||
var_id: var_hir_id,
|
||||
closure_expr_id: closure_expr_def_index
|
||||
closure_expr_id: closure_expr_def_id.to_local(),
|
||||
};
|
||||
|
||||
let var_ty = self.node_ty(var_hir_id)?;
|
||||
|
|
@ -837,7 +837,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
|
|||
// The environment of a closure is guaranteed to
|
||||
// outlive any bindings introduced in the body of the
|
||||
// closure itself.
|
||||
scope: DefId::local(upvar_id.closure_expr_id),
|
||||
scope: upvar_id.closure_expr_id.to_def_id(),
|
||||
bound_region: ty::BrEnv
|
||||
}));
|
||||
|
||||
|
|
|
|||
|
|
@ -769,7 +769,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for TypeckTables<'gcx> {
|
|||
};
|
||||
let closure_def_id = DefId {
|
||||
krate: local_id_root.krate,
|
||||
index: closure_expr_id,
|
||||
index: closure_expr_id.to_def_id().index,
|
||||
};
|
||||
(hcx.def_path_hash(var_owner_def_id),
|
||||
var_id.local_id,
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ pub use self::fold::TypeFoldable;
|
|||
|
||||
use hir::{map as hir_map, FreevarMap, TraitMap};
|
||||
use hir::def::{Def, CtorKind, ExportMap};
|
||||
use hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
|
||||
use hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
|
||||
use hir::map::DefPathData;
|
||||
use ich::StableHashingContext;
|
||||
use middle::const_val::ConstVal;
|
||||
|
|
@ -573,7 +573,7 @@ impl<T> Slice<T> {
|
|||
#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
||||
pub struct UpvarId {
|
||||
pub var_id: hir::HirId,
|
||||
pub closure_expr_id: DefIndex,
|
||||
pub closure_expr_id: LocalDefId,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable, Copy)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue